forked from reef-technologies/B2_Command_Line_Tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
325 lines (270 loc) · 10.2 KB
/
noxfile.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
317
318
319
320
321
322
323
324
325
######################################################################
#
# File: noxfile.py
#
# Copyright 2020 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
import os
import platform
import subprocess
from glob import glob
import nox
CI = os.environ.get('CI') is not None
CD = CI and (os.environ.get('CD') is not None)
INSTALL_SDK_FROM = os.environ.get('INSTALL_SDK_FROM')
NO_STATICX = os.environ.get('NO_STATICX') is not None
NOX_PYTHONS = os.environ.get('NOX_PYTHONS')
PYTHON_VERSIONS = ['3.5', '3.6', '3.7', '3.8', '3.9', '3.10'
] if NOX_PYTHONS is None else NOX_PYTHONS.split(',')
PYTHON_DEFAULT_VERSION = PYTHON_VERSIONS[-1]
PY_PATHS = ['b2', 'test', 'noxfile.py', 'setup.py']
SYSTEM = platform.system().lower()
REQUIREMENTS_FORMAT = ['yapf==0.27']
REQUIREMENTS_LINT = ['yapf==0.27', 'pyflakes==2.4.0', 'pytest==6.2.5', 'liccheck==0.6.2']
REQUIREMENTS_TEST = [
"pytest==6.2.5;python_version>'3.5'",
"pytest==6.1.1;python_version=='3.5'",
"pytest-cov==3.0.0;python_version>'3.5'",
"pytest-cov==2.10.1;python_version=='3.5'",
]
REQUIREMENTS_BUILD = ['setuptools>=20.2']
REQUIREMENTS_BUNDLE = [
'pyinstaller==4.7.0',
"patchelf-wrapper==1.2.0;platform_system=='Linux'",
"staticx==0.13.5;platform_system=='Linux'",
]
OSX_BUNDLE_IDENTIFIER = 'com.backblaze.b2'
OSX_BUNDLE_ENTITLEMENTS = 'contrib/macos/entitlements.plist'
WINDOWS_TIMESTAMP_SERVER = 'http://timestamp.digicert.com'
WINDOWS_SIGNTOOL_PATH = 'C:/Program Files (x86)/Windows Kits/10/bin/10.0.17763.0/x86/signtool.exe'
nox.options.reuse_existing_virtualenvs = True
nox.options.sessions = [
'lint',
'test',
]
# In CI, use Python interpreter provided by GitHub Actions
if CI:
nox.options.force_venv_backend = 'none'
def install_myself(session, extras=None):
"""Install from the source."""
# In CI, install B2 SDK from the master branch
if CI and not CD and not INSTALL_SDK_FROM:
session.install('git+https://github.com/Backblaze/b2-sdk-python.git#egg=b2sdk')
arg = '.'
if extras:
arg += '[%s]' % ','.join(extras)
session.install('-e', arg)
if INSTALL_SDK_FROM:
cwd = os.getcwd()
os.chdir(INSTALL_SDK_FROM)
session.run('pip', 'uninstall', 'b2sdk', '-y')
session.run('python', 'setup.py', 'develop')
os.chdir(cwd)
@nox.session(name='format', python=PYTHON_DEFAULT_VERSION)
def format_(session):
"""Format the code."""
session.install(*REQUIREMENTS_FORMAT)
# TODO: incremental mode for yapf
session.run('yapf', '--in-place', '--parallel', '--recursive', *PY_PATHS)
# TODO: uncomment if we want to use isort and docformatter
# session.run('isort', *PY_PATHS)
# session.run(
# 'docformatter',
# '--in-place',
# '--recursive',
# '--wrap-summaries=100',
# '--wrap-descriptions=100',
# *PY_PATHS,
# )
@nox.session(python=PYTHON_DEFAULT_VERSION)
def lint(session):
"""Run linters."""
install_myself(session)
session.install(*REQUIREMENTS_LINT)
session.run('yapf', '--diff', '--parallel', '--recursive', *PY_PATHS)
# TODO: uncomment if we want to use isort and docformatter
# session.run('isort', '--check', *PY_PATHS)
# session.run(
# 'docformatter',
# '--check',
# '--recursive',
# '--wrap-summaries=100',
# '--wrap-descriptions=100',
# *PY_PATHS,
# )
# TODO: use flake8 instead of pyflakes
session.log('pyflakes b2')
output = subprocess.run('pyflakes b2', shell=True, check=False,
stdout=subprocess.PIPE).stdout.decode().strip()
excludes = ['__init__.py']
output = [l for l in output.splitlines() if all(x not in l for x in excludes)]
if output:
print('\n'.join(output))
session.error('pyflakes has failed')
# session.run('flake8', *PY_PATHS)
session.run('pytest', 'test/static')
session.run('liccheck', '-s', 'setup.cfg')
@nox.session(python=PYTHON_VERSIONS)
def unit(session):
"""Run unit tests."""
install_myself(session)
session.install(*REQUIREMENTS_TEST)
session.run(
'pytest', '--cov=b2', '--cov-branch', '--cov-report=xml', '--doctest-modules',
*session.posargs, 'test/unit'
)
if not session.posargs:
session.notify('cover')
@nox.session(python=PYTHON_VERSIONS)
def integration(session):
"""Run integration tests."""
install_myself(session)
session.install(*REQUIREMENTS_TEST)
session.run('pytest', '-s', *session.posargs, 'test/integration')
@nox.session(python=PYTHON_VERSIONS)
def test(session):
"""Run all tests."""
if session.python:
session.notify('unit-{}'.format(session.python))
session.notify('integration-{}'.format(session.python))
else:
session.notify('unit')
session.notify('integration')
@nox.session
def cover(session):
"""Perform coverage analysis."""
session.install('coverage')
session.run('coverage', 'report', '--fail-under=75', '--show-missing', '--skip-covered')
session.run('coverage', 'erase')
@nox.session(python=PYTHON_DEFAULT_VERSION)
def build(session):
"""Build the distribution."""
# TODO: consider using wheel as well
session.install(*REQUIREMENTS_BUILD)
session.run('python', 'setup.py', 'check', '--metadata', '--strict')
session.run('rm', '-rf', 'build', 'dist', 'b2.egg-info', external=True)
session.run('python', 'setup.py', 'sdist', *session.posargs)
# Set outputs for GitHub Actions
if CI:
asset_path = glob('dist/*')[0]
print('::set-output name=asset_path::', asset_path, sep='')
version = os.environ['GITHUB_REF'].replace('refs/tags/v', '')
print('::set-output name=version::', version, sep='')
@nox.session(python=PYTHON_DEFAULT_VERSION)
def bundle(session):
"""Bundle the distribution."""
session.install(*REQUIREMENTS_BUNDLE)
session.run('rm', '-rf', 'build', 'dist', 'b2.egg-info', external=True)
install_myself(session)
if SYSTEM == 'darwin':
session.posargs.extend(['--osx-bundle-identifier', OSX_BUNDLE_IDENTIFIER])
session.run('pyinstaller', '--onefile', *session.posargs, 'b2.spec')
if SYSTEM == 'linux' and not NO_STATICX:
session.run(
'staticx', '--no-compress', '--strip', '--loglevel', 'INFO', 'dist/b2', 'dist/b2-static'
)
session.run('mv', '-f', 'dist/b2-static', 'dist/b2', external=True)
# Set outputs for GitHub Actions
if CI:
asset_path = glob('dist/*')[0]
print('::set-output name=asset_path::', asset_path, sep='')
@nox.session(python=False)
def sign(session):
"""Sign the bundled distribution (macOS and Windows only)."""
def sign_darwin(cert_name):
session.run('security', 'find-identity', external=True)
session.run(
'codesign',
'--deep',
'--force',
'--verbose',
'--timestamp',
'--identifier',
OSX_BUNDLE_IDENTIFIER,
'--entitlements',
OSX_BUNDLE_ENTITLEMENTS,
'--options',
'runtime',
'--sign',
cert_name,
'dist/b2',
external=True
)
session.run('codesign', '--verify', '--verbose', 'dist/b2', external=True)
def sign_windows(cert_file, cert_password):
session.run('certutil', '-f', '-p', cert_password, '-importpfx', cert_file)
session.run(
WINDOWS_SIGNTOOL_PATH,
'sign',
'/f',
cert_file,
'/p',
cert_password,
'/tr',
WINDOWS_TIMESTAMP_SERVER,
'/td',
'sha256',
'/fd',
'sha256',
'dist/b2.exe',
external=True
)
session.run(WINDOWS_SIGNTOOL_PATH, 'verify', '/pa', '/all', 'dist/b2.exe', external=True)
if SYSTEM == 'darwin':
try:
certificate_name, = session.posargs
except ValueError:
session.error('pass the certificate name as a positional argument')
return
sign_darwin(certificate_name)
elif SYSTEM == 'windows':
try:
certificate_file, certificate_password = session.posargs
except ValueError:
session.error('pass the certificate file and the password as positional arguments')
return
sign_windows(certificate_file, certificate_password)
elif SYSTEM == 'linux':
session.log('signing is not supported for Linux')
else:
session.error('unrecognized platform: {}'.format(SYSTEM))
# Append OS name to the binary
asset_old_path = glob('dist/*')[0]
name, ext = os.path.splitext(os.path.basename(asset_old_path))
asset_path = 'dist/{}-{}{}'.format(name, SYSTEM, ext)
session.run('mv', '-f', asset_old_path, asset_path, external=True)
# Set outputs for GitHub Actions
if CI:
asset_path = glob('dist/*')[0]
print('::set-output name=asset_path::', asset_path, sep='')
@nox.session(python=PYTHON_DEFAULT_VERSION)
def doc(session):
"""Build the documentation."""
install_myself(session, extras=['doc'])
session.cd('doc')
sphinx_args = ['-b', 'html', '-T', '-W', 'source', 'build/html']
session.run('rm', '-rf', 'build', external=True)
if not session.interactive:
session.run('sphinx-build', *sphinx_args)
session.notify('doc_cover')
else:
sphinx_args[-2:-2] = [
'-E', '--open-browser', '--watch', '../b2', '--ignore', '*.pyc', '--ignore', '*~'
]
session.run('sphinx-autobuild', *sphinx_args)
@nox.session
def doc_cover(session):
"""Perform coverage analysis for the documentation."""
install_myself(session, extras=['doc'])
session.cd('doc')
sphinx_args = ['-b', 'coverage', '-T', '-W', 'source', 'build/coverage']
report_file = 'build/coverage/python.txt'
session.run('sphinx-build', *sphinx_args)
session.run('cat', report_file, external=True)
with open('build/coverage/python.txt') as fd:
# If there is no undocumented files, the report should have only 2 lines (header)
if sum(1 for _ in fd) != 2:
session.error('sphinx coverage has failed')