forked from swift-nav/libsbp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.py
316 lines (232 loc) · 9.91 KB
/
deploy.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
import os
import sys
import glob
import shutil
import platform
import tempfile
import subprocess
ALL_PY_VERSIONS = ["3.5", "3.6", "3.7", "3.8"]
SKIP_PY_VERS = os.environ.get("SKIP_PY_VERS", "").split(",")
if 'PYPI_USERNAME' not in os.environ:
print("\n!!! Please set PYPI_USERNAME in the environment !!!\n\n")
sys.exit(1)
PYPI_USERNAME = os.environ['PYPI_USERNAME']
if 'PYPI_PASSWORD' not in os.environ:
print("\n!!! Please set PYPI_PASSWORD in the environment !!!\n\n")
sys.exit(1)
PYPI_PASSWORD = os.environ['PYPI_PASSWORD']
if 'SBP_VERSION' not in os.environ:
print("\n!!! Please set SBP_VERSION in the environment !!!\n\n")
sys.exit(1)
SBP_VERSION = os.environ['SBP_VERSION']
USE_TEST_PYPI = bool(os.environ.get('USE_TEST_PYPI', None))
if not platform.machine().startswith("arm") and not shutil.which('conda'):
print("\n!!! Please install conda to deploy python !!!\n\n")
sys.exit(1)
script_dir = os.path.dirname(os.path.abspath(__file__))
repo_dir = os.path.join(script_dir, "..")
os.chdir(script_dir)
if platform.system() == "Linux" and platform.python_version().startswith("3.4"):
DASHDASH = ["--"]
else:
DASHDASH = []
os.environ['IS_RELEASED'] = 'y'
def twine_upload(conda_dir, wheel, py_version="3.7", use_conda=True):
if platform.machine().startswith("arm") and py_version in ALL_PY_VERSIONS:
cmd_prefix = ["/usr/local/bin/python{}".format(py_version), "-m"]
if use_conda:
raise RuntimeError("Conda with Python {} is not supported on ARM".format(py_version))
elif py_version in ALL_PY_VERSIONS:
cmd_prefix = ["/usr/bin/python3", "-m"]
if use_conda:
cmd_prefix = ["conda", "run", "-p", conda_dir] + DASHDASH
else:
raise RuntimeError("Unsupported Python version: {} (platform: {})".format(py_version, platform.machine()))
invoke = subprocess.check_call if not USE_TEST_PYPI else subprocess.call
ret = invoke(cmd_prefix + [
"twine", "upload", "-u", PYPI_USERNAME, "-p", PYPI_PASSWORD] + ([
"--repository-url", "https://test.pypi.org/legacy/"]
if USE_TEST_PYPI else []
) + [wheel])
if USE_TEST_PYPI and ret != 0:
print(">>> Warning: twine upload returned exit code {}".format(ret))
def build_wheel_native(conda_dir, deploy_dir, py_version):
print(">>> Installing native deps for: {}...".format(py_version))
py_version_prefix = "/usr/local"
py_version_suffix = py_version
if py_version not in ALL_PY_VERSIONS:
raise RuntimeError("Unsupported Python version")
python = "{}/bin/python{}".format(py_version_prefix, py_version_suffix)
subprocess.check_call(["apt-get", "update"])
if py_version.startswith("3."):
subprocess.check_call(["apt-get", "install", "-y",
"python3", "python3-pip", "python3-dev", "python3-setuptools"
])
else:
subprocess.check_call(["apt-get", "install", "-y",
"python", "python-pip", "python-dev", "python-setuptools"
])
subprocess.check_call([
python, "-m",
"pip", "install", "--upgrade", "pip"
])
subprocess.check_call([
python, "-m",
"pip", "install", "twine", "numpy", "cython", "wheel", "setuptools"
])
print(">>> Installing setup deps in Python {} environment...".format(py_version))
subprocess.check_call([
python, "-m",
"pip", "install", "--ignore-installed",
"-r", "test_requirements.txt"
])
suffix = "" if py_version.startswith("3.") else "27"
subprocess.check_call([
python, "-m",
"pip", "install", "--ignore-installed",
"-r", "requirements{}.txt".format(suffix),
"-r", "setup_requirements{}.txt".format(suffix),
])
run_bdist(conda_dir, deploy_dir, py_version,
py_version_prefix=py_version_prefix,
py_version_suffix=py_version_suffix,
use_conda=False)
def invoke_bdist(conda_dir, use_conda, py_version_prefix="/usr", py_version_suffix="3"):
cmd_prefix = ["{}/bin/python{}".format(py_version_prefix, py_version_suffix)]
if use_conda:
cmd_prefix = ["conda", "run", "-p", conda_dir] + DASHDASH + ["python"]
subprocess.check_call(cmd_prefix + [
"setup.py", "bdist_wheel"
])
def run_bdist(conda_dir,
deploy_dir,
py_version,
py_version_prefix="/usr",
py_version_suffix="3",
use_conda=True):
print(">>> Building staging area for deployment ...")
old_cwd = os.getcwd()
os.chdir(deploy_dir)
os.mkdir('module')
shutil.copytree(os.path.join(repo_dir, ".git"), ".git")
shutil.copy(os.path.join(script_dir, ".coveragerc"), "module/.coveragerc")
shutil.copy(os.path.join(script_dir, ".gitignore"), "module/.gitignore")
shutil.copy(os.path.join(script_dir, ".flake8"), "module/.flake8")
for dirent in glob.glob(os.path.join(script_dir, "*")):
_, leaf_name = os.path.split(dirent)
if os.path.isdir(dirent):
print('Copying (recursive) {}'.format(dirent))
shutil.copytree(dirent, os.path.join("module", leaf_name))
else:
print('Copying (non-recursive) {}'.format(dirent))
shutil.copy(dirent, os.path.join("module", leaf_name))
print(">>> Pruning ...")
if os.path.exists("module/docs/_build"):
shutil.rmtree("module/docs/_build")
for dirent in glob.glob("module/build/*"):
shutil.rmtree(dirent) if os.path.isdir(dirent) else os.unlink(dirent)
os.chdir("module")
print(">>> Staged to '{}'...'".format(deploy_dir))
print(">>> Building Python wheel ...")
invoke_bdist(conda_dir,
use_conda,
py_version_prefix=py_version_prefix,
py_version_suffix=py_version_suffix)
whl_pattern = "dist/sbp-{}-*.whl".format(SBP_VERSION)
print(">>> Uploading Python wheel (glob: {})...".format(whl_pattern))
wheels = glob.glob(whl_pattern)
if not wheels:
print("\n!!! No Python wheel (.whl) file found...\n\n")
sys.exit(1)
wheel = wheels[0]
print(">>> Found wheel (of {} matches): {}".format(len(wheels), wheel))
if platform.system() == "Linux" and platform.machine().startswith("x86"):
print(">>> Running 'auditwheel' against wheel: {}".format(wheel))
subprocess.check_call([
"python3", "-m",
"auditwheel", "repair", "-w", "dist", wheel
])
print(">>> Copying wheel {} to {}".format(wheel, old_cwd))
shutil.copy(wheel, old_cwd)
wheel = wheel.replace("-linux_x86_64", "-manylinux1_x86_64")
twine_upload(conda_dir, wheel, py_version, use_conda)
def build_wheel_conda(conda_dir, deploy_dir, py_version):
print(">>> Creating conda environment for Python version: {}...".format(py_version))
subprocess.check_call([
"conda", "create", "--yes", "-p", conda_dir,
"python={}".format(py_version)])
if platform.system() == 'Linux' and platform.machine() == 'x86_64':
subprocess.check_call([
"conda", "install", "--yes", "-p", conda_dir,
"gcc_linux-64", "gxx_linux-64"
])
print(">>> Installing build deps in Python {} conda environment...".format(py_version))
subprocess.check_call([
"conda", "install", "-p", conda_dir, "--yes",
"cython", "wheel", "setuptools"
])
subprocess.check_call([
"conda", "run", "-p", conda_dir] + DASHDASH + [
"python", "-m", "pip", "install", "--upgrade", "pip"
])
subprocess.check_call([
"conda", "run", "-p", conda_dir] + DASHDASH + [
"python", "-m", "pip", "install", "twine", "numpy"
])
if platform.system() == "Linux" and platform.machine().startswith("x86"):
subprocess.check_call([
"python3", "-m",
"pip", "install", "auditwheel"
])
print(">>> Installing setup deps in Python {} conda environment...".format(py_version))
subprocess.check_call([
"conda", "run", "-p", conda_dir] + DASHDASH + [
"python", "-m", "pip", "install", "--ignore-installed",
"-r", "setup_requirements.txt",
"-r", "test_requirements.txt",
])
suffix = "" if py_version.startswith("3.") else "27"
subprocess.check_call([
"conda", "run", "-p", conda_dir] + DASHDASH + [
"python", "-m", "pip", "install", "--ignore-installed",
"-r", "requirements{}.txt".format(suffix),
"-r", "setup_requirements{}.txt".format(suffix),
])
run_bdist(conda_dir, deploy_dir, py_version, use_conda=True)
def build_native_on_arm(py_version):
if platform.system() != "Linux":
return False
return platform.machine().startswith("arm")
def build_wheel(conda_dir, deploy_dir, py_version):
if build_native_on_arm(py_version):
build_wheel_native(conda_dir, deploy_dir, py_version)
else:
build_wheel_conda(conda_dir, deploy_dir, py_version)
def py_versions():
def _py_versions():
if os.environ.get('LIBSBP_BUILD_ANY', None):
return ["3.7"]
return ALL_PY_VERSIONS
for pyver in _py_versions():
if pyver in SKIP_PY_VERS:
continue
yield pyver
for py_version in py_versions():
print(">>> Building wheel for Python {}...".format(py_version))
conda_tmp_dir = tempfile.mkdtemp()
conda_dir = os.path.join(conda_tmp_dir, "conda")
deploy_dir = tempfile.mkdtemp()
try:
build_wheel(conda_dir, deploy_dir, py_version)
finally:
os.chdir(script_dir)
# Workaround a permission denied errors that happens on Windows
if platform.system() == "Windows":
subprocess.check_call(["rmdir", "/s", "/q", conda_dir], shell=True)
else:
subprocess.check_call(["rm", "-rf", conda_dir])
if platform.system() == "Windows":
subprocess.check_call(["rmdir", "/s", "/q", deploy_dir], shell=True)
else:
subprocess.check_call(["rm", "-rf", deploy_dir])