forked from bowtie-json-schema/bowtie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
358 lines (307 loc) · 9.29 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from contextlib import ExitStack
from functools import wraps
from pathlib import Path
from tempfile import TemporaryDirectory
from zipfile import ZipFile
import os
import shlex
import tarfile
import nox
ROOT = Path(__file__).parent
PYPROJECT = ROOT / "pyproject.toml"
DOCS = ROOT / "docs"
BOWTIE = ROOT / "bowtie"
SCHEMAS = BOWTIE / "schemas"
IMPLEMENTATIONS = ROOT / "implementations"
TESTS = ROOT / "tests"
UI = ROOT / "frontend"
REQUIREMENTS = dict(
main=ROOT / "requirements.txt",
docs=DOCS / "requirements.txt",
tests=ROOT / "test-requirements.txt",
)
REQUIREMENTS_IN = {
(
ROOT / "pyproject.toml"
if path.absolute() == REQUIREMENTS["main"].absolute()
else path.parent / f"{path.stem}.in"
)
for path in REQUIREMENTS.values()
}
SUPPORTED = ["3.10", "3.11"]
nox.options.sessions = []
def session(default=True, **kwargs): # noqa: D103
def _session(fn):
if default:
nox.options.sessions.append(kwargs.get("name", fn.__name__))
return nox.session(**kwargs)(fn)
return _session
@session(python=SUPPORTED)
def tests(session):
"""
Run Bowtie's test suite.
"""
session.install("-r", REQUIREMENTS["tests"])
if session.posargs and session.posargs[0] == "coverage":
if len(session.posargs) > 1 and session.posargs[1] == "github":
github = os.environ["GITHUB_STEP_SUMMARY"]
else:
github = None
session.install("coverage[toml]")
session.run("coverage", "run", "-m", "pytest", TESTS)
if github is None:
session.run("coverage", "report")
else:
with open(github, "a") as summary:
summary.write("### Coverage\n\n")
summary.flush() # without a flush, output seems out of order.
session.run(
"coverage",
"report",
"--format=markdown",
stdout=summary,
)
else:
session.run("pytest", *session.posargs, TESTS)
@session(python=SUPPORTED)
def audit(session):
"""
Audit Python dependencies for vulnerabilities.
"""
session.install("pip-audit", "-r", REQUIREMENTS["main"])
session.run("python", "-m", "pip_audit")
@session(tags=["build"])
def build(session):
"""
Build Bowtie (via a PEP517 builder), and check the built artifact is valid.
"""
session.install("build", "twine")
with TemporaryDirectory() as tmpdir:
session.run("python", "-m", "build", ROOT, "--outdir", tmpdir)
session.run("twine", "check", "--strict", tmpdir + "/*")
schemas = frozenset(SCHEMAS.rglob("*.json"))
assert schemas, "Didn't find any schemas!"
tmpdir = Path(tmpdir)
(tarpath,) = tmpdir.glob("*.tar.gz")
with tarfile.open(tarpath) as tar:
found = {
SCHEMAS.joinpath(member.name.split("/", 3)[3]).absolute()
for member in tar
if "bowtie/schemas" in member.name
}
if not schemas <= found:
session.error(
"Tar distribution schemas are missing. "
f"Expected {schemas} but found {found}."
)
(wheelpath,) = tmpdir.glob("*.whl")
wheel = ZipFile(wheelpath)
found = {
SCHEMAS.joinpath(name.removeprefix("bowtie/schemas/")).absolute()
for name in wheel.namelist()
if name.startswith("bowtie/schemas")
}
if not schemas <= found:
session.error(
"Wheel distribution schemas are missing. "
f"Expected {schemas} but found {found}."
)
@session(tags=["build"])
def shiv(session):
"""
Build a shiv which will run Bowtie.
"""
session.install("shiv")
with ExitStack() as stack:
if session.posargs:
out = session.posargs[0]
else:
tmpdir = Path(stack.enter_context(TemporaryDirectory()))
out = tmpdir / "bowtie"
session.run(
"python",
"-m",
"shiv",
"--reproducible",
"-c",
"bowtie",
"-r",
REQUIREMENTS["main"],
ROOT,
"-o",
out,
)
print(f"Outputted a shiv to {out}.")
@session(tags=["style"])
def style(session):
"""
Lint for style on Bowtie's Python codebase.
"""
session.install("ruff")
session.run("ruff", "check", BOWTIE, TESTS, __file__)
@session()
def typing(session):
"""
Check Bowtie's codebase using pyright.
"""
session.install("pyright", ROOT)
session.run("pyright", BOWTIE)
@session(tags=["docs"])
@nox.parametrize(
"builder",
[
nox.param(name, id=name)
for name in [
"dirhtml",
"doctest",
"linkcheck",
"man",
"spelling",
]
],
)
def docs(session, builder):
"""
Build Bowtie's documentation.
"""
session.install("-r", REQUIREMENTS["docs"])
with TemporaryDirectory() as tmpdir_str:
tmpdir = Path(tmpdir_str)
argv = ["-n", "-T", "-W"]
if builder != "spelling":
argv += ["-q"]
posargs = session.posargs or [tmpdir / builder]
session.run(
"python",
"-m",
"sphinx",
"-b",
builder,
DOCS,
*argv,
*posargs,
)
@session(tags=["docs", "style"], name="docs(style)")
def docs_style(session):
"""
Check Bowtie's documentation style.
"""
session.install(
"doc8",
"pygments",
"pygments-github-lexers",
)
session.run("python", "-m", "doc8", "--config", PYPROJECT, DOCS)
def benchmark(fn):
"""
A non-default noxenv to run a specific benchmark.
"""
name = fn.__name__.removeprefix("bench_")
@session(default=False, tags=["perf"], name=f"bench({name})")
@wraps(fn)
def _benchmark(session):
session.install("-r", REQUIREMENTS["main"], ROOT)
bowtie = Path(session.bin) / "bowtie"
hyperfine_args, command = fn(session=session, bowtie=bowtie)
session.run("hyperfine", *hyperfine_args, command, external=True)
return _benchmark
@benchmark
def bench_info(session, bowtie):
"""
Time how long ``bowtie info`` takes to run (effectively startup time).
"""
if session.posargs:
args = session.posargs
else:
args = [
"--warmup",
"3",
"-L",
"implementation",
",".join(p.name for p in IMPLEMENTATIONS.iterdir() if p.is_dir()),
]
return args, f"{bowtie} info -i {{implementation}}"
@benchmark
def bench_smoke(session, bowtie):
"""
Time how long ``bowtie smoke`` takes to run (startup + ~2 simple examples).
"""
if session.posargs:
args = session.posargs
else:
args = [
"--warmup",
"3",
"-L",
"implementation",
",".join(p.name for p in IMPLEMENTATIONS.iterdir() if p.is_dir()),
]
return args, f"{bowtie} smoke -i {{implementation}}"
@benchmark
def bench_suite(session, bowtie):
"""
Time how long ``bowtie suite`` takes to run a version of the test suite.
"""
if not session.posargs:
session.error("Provide a test suite to benchmark")
posargs = shlex.join(session.posargs)
if "-i" not in session.posargs:
args = [
"--warmup",
"1",
# because not all implementations will likely support the dialect
"--ignore-failure",
"-L",
"implementation",
",".join(p.name for p in IMPLEMENTATIONS.iterdir() if p.is_dir()),
]
command = f"{bowtie} suite -i {{implementation}} {posargs}"
else:
args, command = [], f"{bowtie} suite {posargs}"
return args, command
@session(default=False, python=False)
def develop_harness(session):
"""
Build a local version of an implementation harness.
The harness will be smoke tested after build, relying on Bowtie being
available on your ``PATH``.
This is used / useful during development of a new harness.
For "real" versions of harnesses, rely on the built version from GitHub
packages.
"""
for each in session.posargs:
name = Path(each).name
session.run(
"podman",
"build",
"-f",
IMPLEMENTATIONS / name / "Dockerfile",
"-t",
f"ghcr.io/bowtie-json-schema/{name}",
external=True,
)
session.run("bowtie", "smoke", "--quiet", "-i", name, external=True)
@session(default=False)
def requirements(session):
"""
Update bowtie's requirements.txt files.
"""
session.install("pip-tools")
for each in REQUIREMENTS_IN:
session.run(
"pip-compile",
"--resolver",
"backtracking",
"--strip-extras",
"-U",
each.relative_to(ROOT),
)
@session(default=False, python=False)
def ui(session):
"""
Run a local development UI.
"""
needs_install = not UI.joinpath("node_modules").is_dir()
if needs_install:
session.run("pnpm", "install", "--dir", UI)
session.run("pnpm", "run", "--dir", UI, "start")