-
Notifications
You must be signed in to change notification settings - Fork 4
/
noxfile.py
264 lines (191 loc) · 7.73 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
"""
Ref: https://github.com/scikit-build/scikit-build/blob/master/noxfile.py
"""
import os
import platform
import typing as t
import nox
from nox.command import CommandFailed
from nox.virtualenv import CondaEnv
nox.options.default_venv_backend = 'conda'
SUPPORTED_PYTHONS = ['3.8', '3.9', '3.10', '3.11', '3.12']
OS = platform.system()
RELEASE = platform.release()
PYCDS_ROOT = os.path.dirname(__file__)
GA = os.environ.get('GITHUB_ACTIONS') == 'true'
CDS_PYPERFORMANCE = 'git+https://github.com/oraluben/pyperformance.git@cds'
def _clean_nox():
from nox.virtualenv import shutil
shutil.rmtree(os.path.join(PYCDS_ROOT, '.nox'), ignore_errors=True)
def ci_session_cleanup():
if OS == 'Windows' and GA:
_clean_nox()
def _py_version(session: nox.Session):
"""
:return: "3.9", "3.10", ...
"""
out: str = session.run('python', '-c', 'import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]}")',
silent=True)
out = out.strip()
return out
def _self_tests(session: nox.Session):
session.install(".")
session.install("pytest")
session.run('pytest', )
class Package(t.NamedTuple):
name: str
module: t.Union[t.List[str], str, None] = None
conda: bool = False
skip: t.Optional[t.Callable[[str], bool]] = None
def __repr__(self):
return self.name
@property
def import_stmt(self):
mods = self.module
if mods is None:
mods = self.name
elif isinstance(mods, list):
mods = ', '.join(mods)
return f"""import {mods}"""
def install_in(self, session: nox.Session):
if self.conda:
assert isinstance(session.virtualenv, CondaEnv)
session.conda_install(self.name)
else:
session.install(self.name)
def should_skip(self, py_version):
if self.skip:
return self.skip(py_version)
return False
@nox.session(venv_backend='venv')
def tests_venv_current(session: nox.Session):
"""
Run test on current CPython.
"""
_self_tests(session)
@nox.session(python=SUPPORTED_PYTHONS)
def tests_multiple_conda(session: nox.Session):
"""
Regression on supported version of CPython.
"""
_self_tests(session)
PACKAGES = (
Package('boto3'),
Package('requests'),
Package('numpy'),
Package('pyparsing'),
Package('sqlalchemy'),
Package('werkzeug'),
Package('aiohttp', skip=lambda _py: _py in ('3.12',)),
Package('google-cloud-storage', module='google.cloud.storage'),
Package('flask'),
Package('azure-core', module='azure.core'),
Package('jsonschema'),
# Pillow is a package with C extension, and do not involve much CPython's internal library.
# So this should be a good test for cds'ing C extensions.
Package('Pillow', module=['PIL.Image']),
Package('scipy', conda=True),
# conda-provided tf might require pypy and this is not what we want,
# and pypi only provides tf for CPython <= 3.11
Package('tensorflow', skip=lambda _py: _py in ('3.12',)),
Package('seaborn', conda=True),
Package('azureml-core', module='azureml.core'),
Package('opencv-python', module='cv2')
)
for py in SUPPORTED_PYTHONS:
@nox.session(name=f'test_import_third_party-{py}', tags=['test_import_third_party'], python=py)
@nox.parametrize('package', [package for package in PACKAGES if not package.should_skip(py)])
def test_import_third_party(session: nox.Session, package):
"""
Test import with / without PyCDS.
"""
session.install('.')
package.install_in(session)
session.run('python', '-c', package.import_stmt)
tmp = session.create_tmp()
lst = os.path.join(tmp, 'test.lst')
img = os.path.join(tmp, 'test.img')
session.run('python', '-c', package.import_stmt, env={'PYCDSMODE': 'TRACE', 'PYCDSLIST': lst})
session.run('python', '-c', f'import cds.dump; cds.dump.run_dump({repr(lst)}, {repr(img)})')
session.run('python', '-c', package.import_stmt, env={'PYCDSMODE': 'SHARE', 'PYCDSARCHIVE': img})
ci_session_cleanup()
@nox.session(name=f'test_import_third_party_perf-{py}', tags=['test_import_third_party_perf'], python=py)
@nox.parametrize('package', [package for package in PACKAGES if not package.should_skip(py)])
def test_import_third_party_perf(session: nox.Session, package):
"""
Benchmark import statements w./w.o. CDS.
Generates perf-{py-ver}-{cds|raw}.json.
See `perf.sh`.
"""
from nox.logger import logger
session.install('pyperf', '.')
package.install_in(session)
tmp = session.create_tmp()
lst = os.path.join(tmp, 'test.lst')
img = os.path.join(tmp, 'test.img')
logger.info(f'start generating CDS archive for {package.name}')
session.run('python', '-c', package.import_stmt, env={'PYCDSMODE': 'TRACE', 'PYCDSLIST': lst}, log=False)
session.run('python', '-c', f'import cds.dump; cds.dump.run_dump({repr(lst)}, {repr(img)})', log=False)
session.run('python', '-c', package.import_stmt, env={'PYCDSMODE': 'SHARE', 'PYCDSARCHIVE': img}, log=False)
logger.info(f'finish generating CDS archive for {package.name}')
raw_out = f'perf-import-{session.python}-raw'
cds_out = f'perf-import-{session.python}-cds'
session.run('pyperf', 'command', '--fast', f'--append={raw_out}.json', f'--name={package.name}',
'python', '-c', package.import_stmt)
session.run('pyperf', 'command', '--fast', f'--append={cds_out}.json', f'--name={package.name}',
'--inherit-environ=PYCDSMODE,PYCDSARCHIVE',
'python', '-c', package.import_stmt,
env={'PYCDSMODE': 'SHARE', 'PYCDSARCHIVE': img})
ci_session_cleanup()
def _pyperformance(session: nox.Session, pyperformance_args=None):
session.install(CDS_PYPERFORMANCE)
configs = [
(os.path.realpath(f'pyperformance-{_py_version(session)}-{config_name}.json'), config_args)
for (config_name, config_args) in [
('raw', []),
('cds-site', ['--install-cds', PYCDS_ROOT]),
('cds', ['--install-cds', PYCDS_ROOT, '--enable-cds']),
]
]
tmp = session.create_tmp()
session.chdir(tmp)
if pyperformance_args is None:
pyperformance_args = ['pyperformance', 'run']
cmd_exc = None
for out, args in configs:
if os.path.exists(out):
session.run('mv', out, out + '.old')
try:
session.run(*(pyperformance_args + args), f'--out={out}')
except CommandFailed as e:
if cmd_exc is None:
cmd_exc = e
if cmd_exc is not None:
raise cmd_exc
@nox.session(venv_backend='venv')
def pyperformance_current(session: nox.Session):
_pyperformance(session, ['pyperformance', 'run', '--fast'])
@nox.session(python=SUPPORTED_PYTHONS)
def pyperformance(session: nox.Session):
_pyperformance(session, ['pyperformance', 'run', '--fast'])
@nox.session(python=SUPPORTED_PYTHONS)
def pyperformance_looong(session: nox.Session):
_pyperformance(session, ['pyperformance', 'run', '--rigorous'])
@nox.session(venv_backend='venv')
def build_sdist(session: nox.Session):
"""
Make an SDist.
"""
session.install("build")
session.run("python", "-m", "build", "--sdist")
@nox.session(python=SUPPORTED_PYTHONS)
def build_wheel(session: nox.Session):
"""
Make a wheel.
"""
session.install("build")
session.run("python", "-m", "build", "--wheel")
@nox.session(venv_backend='venv')
def render_readme(session: nox.Session):
session.install('docutils')
session.run('rst2html.py', '--report=2', '--halt=2', 'README.rst', 'README.html')