2017-06-21 20:18:22 +03:00
|
|
|
#!/usr/bin/env python3
|
2016-09-18 12:59:12 +03:00
|
|
|
"""A setuptools based setup module.
|
|
|
|
|
|
|
|
See:
|
|
|
|
https://packaging.python.org/en/latest/distributing.html
|
|
|
|
https://github.com/pypa/sampleproject
|
2017-06-21 12:27:22 +03:00
|
|
|
|
|
|
|
Extra supported commands are:
|
2018-04-15 14:19:25 +03:00
|
|
|
* gen, to generate the classes required for Telethon to run or docs
|
2017-09-18 15:03:06 +03:00
|
|
|
* pypi, to generate sdist, bdist_wheel, and push to PyPi
|
2016-09-18 12:59:12 +03:00
|
|
|
"""
|
|
|
|
|
2018-04-28 11:19:55 +03:00
|
|
|
import itertools
|
2018-06-06 18:35:06 +03:00
|
|
|
import json
|
2018-04-15 14:19:25 +03:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import shutil
|
2016-09-18 12:59:12 +03:00
|
|
|
from codecs import open
|
2018-03-12 12:58:56 +03:00
|
|
|
from sys import argv, version_info
|
2016-09-18 12:59:12 +03:00
|
|
|
|
2016-11-30 00:29:42 +03:00
|
|
|
from setuptools import find_packages, setup
|
|
|
|
|
2017-06-21 12:27:22 +03:00
|
|
|
|
2017-09-29 13:38:53 +03:00
|
|
|
class TempWorkDir:
|
|
|
|
"""Switches the working directory to be the one on which this file lives,
|
|
|
|
while within the 'with' block.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.original = None
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.original = os.path.abspath(os.path.curdir)
|
|
|
|
os.chdir(os.path.abspath(os.path.dirname(__file__)))
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
os.chdir(self.original)
|
|
|
|
|
|
|
|
|
2018-04-15 14:19:25 +03:00
|
|
|
GENERATOR_DIR = 'telethon_generator'
|
|
|
|
LIBRARY_DIR = 'telethon'
|
2017-10-20 18:29:45 +03:00
|
|
|
|
2018-04-15 14:19:25 +03:00
|
|
|
ERRORS_IN_JSON = os.path.join(GENERATOR_DIR, 'data', 'errors.json')
|
|
|
|
ERRORS_IN_DESC = os.path.join(GENERATOR_DIR, 'data', 'error_descriptions')
|
|
|
|
ERRORS_OUT = os.path.join(LIBRARY_DIR, 'errors', 'rpc_error_list.py')
|
2017-10-20 18:29:45 +03:00
|
|
|
|
2018-06-06 18:35:06 +03:00
|
|
|
INVALID_BM_IN = os.path.join(GENERATOR_DIR, 'data', 'invalid_bot_methods.json')
|
|
|
|
|
2018-04-28 11:19:55 +03:00
|
|
|
TLOBJECT_IN_CORE_TL = os.path.join(GENERATOR_DIR, 'data', 'mtproto_api.tl')
|
|
|
|
TLOBJECT_IN_TL = os.path.join(GENERATOR_DIR, 'data', 'telegram_api.tl')
|
2018-04-15 14:19:25 +03:00
|
|
|
TLOBJECT_OUT = os.path.join(LIBRARY_DIR, 'tl')
|
|
|
|
IMPORT_DEPTH = 2
|
2017-09-29 13:40:03 +03:00
|
|
|
|
2018-04-15 14:19:25 +03:00
|
|
|
DOCS_IN_RES = os.path.join(GENERATOR_DIR, 'data', 'html')
|
|
|
|
DOCS_OUT = 'docs'
|
|
|
|
|
|
|
|
|
|
|
|
def generate(which):
|
|
|
|
from telethon_generator.parsers import parse_errors, parse_tl, find_layer
|
|
|
|
from telethon_generator.generators import\
|
|
|
|
generate_errors, generate_tlobjects, generate_docs, clean_tlobjects
|
|
|
|
|
2018-06-06 18:35:06 +03:00
|
|
|
with open(INVALID_BM_IN) as f:
|
2018-06-13 11:48:35 +03:00
|
|
|
invalid_bot_methods = set(json.load(f))
|
2018-06-06 18:35:06 +03:00
|
|
|
|
2018-04-15 14:19:25 +03:00
|
|
|
layer = find_layer(TLOBJECT_IN_TL)
|
2018-05-12 17:12:42 +03:00
|
|
|
errors = list(parse_errors(ERRORS_IN_JSON, ERRORS_IN_DESC))
|
|
|
|
tlobjects = list(itertools.chain(
|
2018-06-13 11:48:35 +03:00
|
|
|
parse_tl(TLOBJECT_IN_CORE_TL, layer, invalid_bot_methods),
|
|
|
|
parse_tl(TLOBJECT_IN_TL, layer, invalid_bot_methods)))
|
2018-04-15 14:19:25 +03:00
|
|
|
|
|
|
|
if not which:
|
2018-04-15 14:20:56 +03:00
|
|
|
which.extend(('tl', 'errors'))
|
2018-04-15 14:19:25 +03:00
|
|
|
|
|
|
|
clean = 'clean' in which
|
|
|
|
action = 'Cleaning' if clean else 'Generating'
|
|
|
|
if clean:
|
|
|
|
which.remove('clean')
|
|
|
|
|
|
|
|
if 'all' in which:
|
|
|
|
which.remove('all')
|
|
|
|
for x in ('tl', 'errors', 'docs'):
|
|
|
|
if x not in which:
|
|
|
|
which.append(x)
|
|
|
|
|
|
|
|
if 'tl' in which:
|
|
|
|
which.remove('tl')
|
|
|
|
print(action, 'TLObjects...')
|
|
|
|
if clean:
|
|
|
|
clean_tlobjects(TLOBJECT_OUT)
|
|
|
|
else:
|
|
|
|
generate_tlobjects(tlobjects, layer, IMPORT_DEPTH, TLOBJECT_OUT)
|
|
|
|
|
|
|
|
if 'errors' in which:
|
|
|
|
which.remove('errors')
|
|
|
|
print(action, 'RPCErrors...')
|
|
|
|
if clean:
|
|
|
|
if os.path.isfile(ERRORS_OUT):
|
|
|
|
os.remove(ERRORS_OUT)
|
|
|
|
else:
|
|
|
|
with open(ERRORS_OUT, 'w', encoding='utf-8') as file:
|
|
|
|
generate_errors(errors, file)
|
|
|
|
|
|
|
|
if 'docs' in which:
|
|
|
|
which.remove('docs')
|
|
|
|
print(action, 'documentation...')
|
|
|
|
if clean:
|
|
|
|
if os.path.isdir(DOCS_OUT):
|
|
|
|
shutil.rmtree(DOCS_OUT)
|
|
|
|
else:
|
|
|
|
generate_docs(tlobjects, errors, layer, DOCS_IN_RES, DOCS_OUT)
|
|
|
|
|
2018-06-13 11:48:35 +03:00
|
|
|
if 'json' in which:
|
|
|
|
which.remove('json')
|
|
|
|
print(action, 'JSON schema...')
|
|
|
|
mtproto = 'mtproto_api.json'
|
|
|
|
telegram = 'telegram_api.json'
|
|
|
|
if clean:
|
|
|
|
for x in (mtproto, telegram):
|
|
|
|
if os.path.isfile(x):
|
|
|
|
os.remove(x)
|
|
|
|
else:
|
|
|
|
def gen_json(fin, fout):
|
|
|
|
methods = []
|
|
|
|
constructors = []
|
|
|
|
for tl in parse_tl(fin, layer):
|
|
|
|
if tl.is_function:
|
|
|
|
methods.append(tl.to_dict())
|
|
|
|
else:
|
|
|
|
constructors.append(tl.to_dict())
|
|
|
|
what = {'constructors': constructors, 'methods': methods}
|
|
|
|
with open(fout, 'w') as f:
|
|
|
|
json.dump(what, f, indent=2)
|
|
|
|
|
|
|
|
gen_json(TLOBJECT_IN_CORE_TL, mtproto)
|
|
|
|
gen_json(TLOBJECT_IN_TL, telegram)
|
|
|
|
|
2018-04-15 14:19:25 +03:00
|
|
|
if which:
|
|
|
|
print('The following items were not understood:', which)
|
|
|
|
print(' Consider using only "tl", "errors" and/or "docs".')
|
|
|
|
print(' Using only "clean" will clean them. "all" to act on all.')
|
|
|
|
print(' For instance "gen tl errors".')
|
2017-09-29 13:40:03 +03:00
|
|
|
|
|
|
|
|
2017-09-29 13:38:53 +03:00
|
|
|
def main():
|
2018-04-15 14:19:25 +03:00
|
|
|
if len(argv) >= 2 and argv[1] == 'gen':
|
|
|
|
generate(argv[2:])
|
2017-06-21 12:27:22 +03:00
|
|
|
|
2017-09-18 15:03:06 +03:00
|
|
|
elif len(argv) >= 2 and argv[1] == 'pypi':
|
2017-11-29 14:34:15 +03:00
|
|
|
# (Re)generate the code to make sure we don't push without it
|
2018-04-15 16:43:18 +03:00
|
|
|
generate(['tl', 'errors'])
|
2017-11-29 14:34:15 +03:00
|
|
|
|
|
|
|
# Try importing the telethon module to assert it has no errors
|
|
|
|
try:
|
|
|
|
import telethon
|
|
|
|
except:
|
|
|
|
print('Packaging for PyPi aborted, importing the module failed.')
|
|
|
|
return
|
|
|
|
|
2017-09-19 11:16:41 +03:00
|
|
|
# Need python3.5 or higher, but Telethon is supposed to support 3.x
|
|
|
|
# Place it here since noone should be running ./setup.py pypi anyway
|
|
|
|
from subprocess import run
|
|
|
|
from shutil import rmtree
|
|
|
|
|
2017-09-18 15:03:06 +03:00
|
|
|
for x in ('build', 'dist', 'Telethon.egg-info'):
|
|
|
|
rmtree(x, ignore_errors=True)
|
|
|
|
run('python3 setup.py sdist', shell=True)
|
|
|
|
run('python3 setup.py bdist_wheel', shell=True)
|
|
|
|
run('twine upload dist/*', shell=True)
|
|
|
|
for x in ('build', 'dist', 'Telethon.egg-info'):
|
|
|
|
rmtree(x, ignore_errors=True)
|
|
|
|
|
2017-06-21 12:27:22 +03:00
|
|
|
else:
|
2018-04-15 14:19:25 +03:00
|
|
|
# e.g. install from GitHub
|
2018-04-23 21:52:27 +03:00
|
|
|
if os.path.isdir(GENERATOR_DIR):
|
2018-04-15 16:43:18 +03:00
|
|
|
generate(['tl', 'errors'])
|
2018-01-15 20:46:04 +03:00
|
|
|
|
2017-06-21 12:27:22 +03:00
|
|
|
# Get the long description from the README file
|
2017-09-29 13:38:53 +03:00
|
|
|
with open('README.rst', encoding='utf-8') as f:
|
2017-06-21 12:27:22 +03:00
|
|
|
long_description = f.read()
|
|
|
|
|
2017-10-28 13:21:07 +03:00
|
|
|
with open('telethon/version.py', encoding='utf-8') as f:
|
2018-02-24 11:25:08 +03:00
|
|
|
version = re.search(r"^__version__\s*=\s*'(.*)'.*$",
|
2017-10-28 13:21:07 +03:00
|
|
|
f.read(), flags=re.MULTILINE).group(1)
|
2017-06-21 12:27:22 +03:00
|
|
|
setup(
|
|
|
|
name='Telethon',
|
2017-09-29 13:40:03 +03:00
|
|
|
version=version,
|
2017-06-21 12:27:22 +03:00
|
|
|
description="Full-featured Telegram client library for Python 3",
|
|
|
|
long_description=long_description,
|
|
|
|
|
|
|
|
url='https://github.com/LonamiWebs/Telethon',
|
|
|
|
download_url='https://github.com/LonamiWebs/Telethon/releases',
|
|
|
|
|
|
|
|
author='Lonami Exo',
|
|
|
|
author_email='totufals@hotmail.com',
|
|
|
|
|
|
|
|
license='MIT',
|
|
|
|
|
2018-04-04 11:21:55 +03:00
|
|
|
# See https://stackoverflow.com/a/40300957/4759433
|
|
|
|
# -> https://www.python.org/dev/peps/pep-0345/#requires-python
|
|
|
|
# -> http://setuptools.readthedocs.io/en/latest/setuptools.html
|
|
|
|
python_requires='>=3.4',
|
|
|
|
|
2017-06-21 12:27:22 +03:00
|
|
|
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
|
|
|
|
classifiers=[
|
|
|
|
# 3 - Alpha
|
|
|
|
# 4 - Beta
|
|
|
|
# 5 - Production/Stable
|
|
|
|
'Development Status :: 3 - Alpha',
|
|
|
|
|
|
|
|
'Intended Audience :: Developers',
|
|
|
|
'Topic :: Communications :: Chat',
|
|
|
|
|
|
|
|
'License :: OSI Approved :: MIT License',
|
|
|
|
|
|
|
|
'Programming Language :: Python :: 3',
|
|
|
|
'Programming Language :: Python :: 3.4',
|
|
|
|
'Programming Language :: Python :: 3.5',
|
|
|
|
'Programming Language :: Python :: 3.6'
|
|
|
|
],
|
|
|
|
keywords='telegram api chat client library messaging mtproto',
|
|
|
|
packages=find_packages(exclude=[
|
|
|
|
'telethon_generator', 'telethon_tests', 'run_tests.py',
|
2018-03-04 13:23:18 +03:00
|
|
|
'try_telethon.py',
|
|
|
|
'telethon_generator/parser/__init__.py',
|
|
|
|
'telethon_generator/parser/source_builder.py',
|
|
|
|
'telethon_generator/parser/tl_object.py',
|
|
|
|
'telethon_generator/parser/tl_parser.py',
|
2017-06-21 12:27:22 +03:00
|
|
|
]),
|
2018-03-12 12:58:56 +03:00
|
|
|
install_requires=['pyaes', 'rsa',
|
2018-03-22 21:02:40 +03:00
|
|
|
'typing' if version_info < (3, 5, 2) else ""],
|
2018-02-17 14:14:23 +03:00
|
|
|
extras_require={
|
2018-03-17 19:38:16 +03:00
|
|
|
'cryptg': ['cryptg']
|
2018-02-17 14:14:23 +03:00
|
|
|
}
|
2017-06-21 12:27:22 +03:00
|
|
|
)
|
2017-09-29 13:38:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
with TempWorkDir(): # Could just use a try/finally but this is + reusable
|
|
|
|
main()
|