-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
246 lines (178 loc) · 6.13 KB
/
setup.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import fnmatch
import os
import sys
import warnings
import setuptools.command.build_py
import setuptools.command.install
import distutils.cmd
import distutils.log
if sys.version_info < (3, 7):
raise EnvironmentError('python >= 3.7 required')
warnings.filterwarnings(
'ignore',
message=r"Normalizing '[^'-]+-[^'-]+' to '[^'-]+\.[^'-]+'",
module=r'setuptools\.dist',
)
PROJECT = 'iceworm'
BASE_DIR = os.path.dirname(__file__)
ABOUT = {}
def _read_about():
with open(os.path.join(BASE_DIR, PROJECT, '__about__.py'), 'rb') as f:
src = f.read()
if sys.version_info[0] > 2:
src = src.decode('UTF-8')
exec(src, ABOUT)
_read_about()
EXCLUDED_STATIC_FILE_PATHS = [
'*.py',
'*/__pycache__/*',
'*/tests/*',
]
def _get_static_files(path):
return [
filepath
for (dirpath, dirnames, filenames) in os.walk(path, followlinks=True)
for filename in filenames
for filepath in [os.path.join(dirpath, filename)]
if not any(fnmatch.fnmatch(filepath, pat) for pat in EXCLUDED_STATIC_FILE_PATHS)
]
PACKAGE_DATA = [
] + _get_static_files(PROJECT)
INSTALL_REQUIRES = [
'Jinja2>=2.11',
'protobuf>=3.13',
'PyYAML>=5.3',
'SQLAlchemy>=1.3',
# @omnibus-dep@
'omnibus-all @ git+https://github.com/wrmsr/omnibus@05c441f24cb1f127ba724036b2cd0742e6ba236d',
]
EXTRAS_REQUIRE = {
}
EXT_MODULES = [
]
BREW_DEPS = {
'graphviz',
'libyaml',
}
class SysdepsCommand(distutils.cmd.Command):
description = 'install sysdeps'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
self.announce('Installing sysdeps')
if sys.platform == 'darwin':
import shutil
if not shutil.which('brew'):
self.announce('brew not found, skipping')
return
os.system(f'brew install {" ".join(BREW_DEPS)}')
class CyamlCommand(distutils.cmd.Command):
description = 'install cyaml'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
self.announce('Installing cyaml')
import tempfile
dp = tempfile.mkdtemp()
import os.path
fp = os.path.join(dp, 'cyaml.tar.gz')
import urllib.request
urllib.request.urlretrieve('https://pyyaml.org/download/pyyaml/PyYAML-5.3.1.tar.gz', fp)
import hashlib
sha = hashlib.sha1()
with open(fp, 'rb') as f:
sha.update(f.read())
digest = sha.hexdigest()
if digest != '3b20272e119990b2bbeb03815a1dd3f3e48af07e':
raise ValueError(f'Hash cyaml mismatch: {digest}')
os.system(
f'cd {dp} && '
f'{os.path.abspath(sys.executable)} -m pip install cyaml.tar.gz --global-option="--with-libyaml"'
)
class InstallCommand(setuptools.command.install.install):
user_options = setuptools.command.install.install.user_options + [
('cyaml', None, 'install cyaml'),
('sysdeps', None, 'install sysdeps'),
]
def initialize_options(self):
super().initialize_options()
self.sysdeps = 0
self.cyaml = 0
if sys.platform == 'darwin':
import subprocess
pid_chain = []
cur_pid = int(os.getpid())
while cur_pid != 1:
buf = subprocess.check_output(f'ps -o pid=,ppid=,command= -p {cur_pid}'.split(' ')).decode('utf-8')
pid_str, _, buf = buf.strip().partition(' ')
ppid_str, _, buf = buf.strip().partition(' ')
if int(pid_str) != cur_pid:
raise Exception(f'{pid_str} != {cur_pid}')
tup = (int(ppid_str), buf)
pid_chain.append(tup)
cur_pid = tup[0]
if len(pid_chain) >= 3:
p1 = pid_chain[1][1].split()
p2 = pid_chain[2][1].split()
if (
len(p1) >= 5 and
p1[0].lower().endswith('/python') and
p1[1:4] == ['-m', 'pip', 'install'] and
p1[4] != '--no-dependencies' and
len(p2) >= 3 and
p2[0].lower().endswith('/python') and
p2[1].lower().endswith('/pipx')
):
self.announce('activating pipx extras')
self.sysdeps = 1
self.cyaml = 1
def run(self):
if self.sysdeps:
self.run_command('sysdeps')
if self.cyaml:
self.run_command('cyaml')
super().run()
def new_build_py_find_package_modules(self, package, package_dir):
modules = old_build_py_find_package_modules(self, package, package_dir)
if package == PROJECT:
modules = [t for t in modules if t[1] != 'conftest']
return modules
old_build_py_find_package_modules = setuptools.command.build_py.build_py.find_package_modules # noqa
setuptools.command.build_py.build_py.find_package_modules = new_build_py_find_package_modules # noqa
if __name__ == '__main__':
setuptools.setup(
name=ABOUT['__title__'],
version=ABOUT['__version__'],
description=ABOUT['__description__'],
author=ABOUT['__author__'],
url=ABOUT['__url__'],
cmdclass={
'cyaml': CyamlCommand,
'install': InstallCommand,
'sysdeps': SysdepsCommand,
},
python_requires=ABOUT['__python_requires__'],
classifiers=ABOUT['__classifiers__'],
setup_requires=['setuptools'],
packages=setuptools.find_packages(
include=[PROJECT, PROJECT + '.*'],
exclude=['tests', '*.tests', '*.tests.*'],
),
py_modules=[PROJECT],
package_data={PROJECT: PACKAGE_DATA},
include_package_data=True,
entry_points={
'console_scripts': [
f'{PROJECT} = {PROJECT}.cli:main',
],
},
install_requires=INSTALL_REQUIRES,
extras_require=EXTRAS_REQUIRE,
ext_modules=EXT_MODULES,
)