-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.py
More file actions
127 lines (101 loc) · 3.95 KB
/
Copy pathsetup.py
File metadata and controls
127 lines (101 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from setuptools import setup, Extension, Command, find_packages
import distutils.command.build_py as _build_py
import distutils.command.sdist as _sdist
import glob
import os
import re
VERSION_MAJOR = 0
VERSION_MINOR = 1
VERSION_PATCH = 0
VERSION_DEV = 0
IS_RELEASED = False
VERSION = "{}.{}.{}".format(VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH)
if not IS_RELEASED:
VERSION += '.dev{}'.format(VERSION_DEV)
PACKAGE_NAME = 'keyviserver'
def generate_protocols():
protos_root = '../proto'
output_dir = 'src/py/keyviserver/proto'
# protos only exist in a dev environment
if os.path.exists(protos_root):
protos = glob.glob(os.path.join(protos_root, '*'))
max_modification_time = max([os.path.getmtime(fn) for fn in protos])
if not os.path.exists(os.path.join(output_dir, '__init__.py')) or max_modification_time > os.path.getmtime(output_dir):
try:
from grpc_tools import protoc
except ImportError:
print("grpcio-tools not found.\n"
"Install it via pip (pip3 install grpcio-tools).")
exit(1)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
protoc.main((
'',
'-I' + protos_root,
'--python_out=' + output_dir,
'--grpc_python_out=' + output_dir,
*protos,
))
with open(os.path.join(output_dir, '__init__.py'), 'w') as fd:
fd.write('# generated by setup.py')
# Postprocess imports in generated code
imports_to_fix = [os.path.splitext(os.path.basename(x))[0] + '_pb2' for x in protos]
for root, _, files in os.walk(output_dir):
for filename in files:
if filename.endswith('.py'):
path = os.path.join(root, filename)
with open(path, 'r') as f:
code = f.read()
# fix imports
for import_to_fix in imports_to_fix:
code = re.sub(r'^import ' + import_to_fix, r'import keyviserver.proto.' + import_to_fix,
code, flags=re.MULTILINE)
with open(path, 'w') as f:
f.write(code)
def write_version_file():
here = os.path.abspath(os.path.dirname(__file__))
version_file_path = os.path.join(here, 'src/py/keyviserver/_version.py')
content = """
# THIS FILE IS GENERATED FROM KEYVISERVER SETUP.PY
__version__ = '{}'
""".format(VERSION)
with open(version_file_path, 'w') as f_out:
f_out.write(content)
install_requires = []
class build_py(_build_py.build_py):
def run(self):
generate_protocols()
_build_py.build_py.run(self)
class sdist(_sdist.sdist):
def run(self):
generate_protocols()
_sdist.sdist.run(self)
commands = {'sdist': sdist, 'build_py': build_py}
write_version_file()
setup(
name=PACKAGE_NAME,
version=VERSION,
description='Python bindings for keyviserver',
author='Hendrik Muhs',
author_email='hendrik.muhs@gmail.com',
license="ASL 2.0",
cmdclass=commands,
scripts=[],
packages=find_packages(where='src/py'),
package_dir={'': 'src/py'},
zip_safe=False,
url='http://keyvi.org',
download_url='https://github.com/KeyviDev/keyviserver/tarball/v{}'.format(VERSION),
keywords=['keyvi', 'key-value store'],
classifiers=[
'Programming Language :: Python',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Operating System :: OS Independent',
],
install_requires=install_requires,
)