-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
408 lines (347 loc) · 11.4 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""nox build configuration for Nublado."""
import shutil
import sys
from pathlib import Path
import nox
from nox.command import CommandFailed
# Default sessions
nox.options.sessions = [
"lint",
"typing",
"typing-hub",
"typing-inithome",
"typing-client",
"test",
"test-hub",
"test-inithome",
"test-client",
"docs",
"docs-linkcheck",
]
# Other nox defaults
nox.options.default_venv_backend = "uv"
nox.options.reuse_existing_virtualenvs = True
# pip-installable dependencies for development and documentation. This is not
# used for pytest and typing, since it merges the controller, authenticator,
# spawner, client, and inithome dependencies.
PIP_DEPENDENCIES = [
(
"-r",
"./controller/requirements/main.txt",
"-r",
"./controller/requirements/dev.txt",
"-r",
"./inithome/requirements/main.txt",
"-r",
"./inithome/requirements/dev.txt",
),
("-e", "./authenticator[dev]"),
("-e", "./client[dev]"),
("-e", "./controller"),
("-e", "./inithome"),
("-e", "./spawner[dev]"),
]
def _install(session: nox.Session) -> None:
"""Install the application and all dependencies into the session."""
session.install("--upgrade", "uv")
for deps in PIP_DEPENDENCIES:
session.install(*deps)
def _install_dev(session: nox.Session, bin_prefix: str = "") -> None:
"""Install the application and dev dependencies into the session."""
python = f"{bin_prefix}python"
precommit = f"{bin_prefix}pre-commit"
# Install dev dependencies
session.run(
python,
"-m",
"pip",
"install",
"--upgrade",
"nox[uv]",
"pre-commit",
external=True,
)
for deps in PIP_DEPENDENCIES:
session.run(python, "-m", "uv", "pip", "install", *deps, external=True)
# Install pre-commit hooks
session.run(precommit, "install", external=True)
def _pytest(
session: nox.Session, directory: str, module: str, *, coverage: bool = True
) -> None:
"""Run pytest for the given directory and module, if needed."""
generic = []
per_directory = []
found_per_directory = False
for arg in session.posargs:
if arg.startswith("-"):
generic.append(arg)
elif arg.startswith(f"{directory}/"):
per_directory.append(arg.removeprefix(f"{directory}/"))
found_per_directory = True
elif "/" in arg and Path(arg).exists():
found_per_directory = True
else:
generic.append(arg)
if not session.posargs or not found_per_directory or per_directory:
args = []
if coverage:
args.extend([f"--cov={module}", "--cov-branch", "--cov-report="])
with session.chdir(directory):
session.run(
"pytest",
*args,
*generic,
*per_directory,
)
def _update_deps(
session: nox.Session,
*,
generate_hashes: bool = True,
hub_only: bool = False,
) -> None:
session.install("--upgrade", "uv")
session.install("--upgrade", "pre-commit")
session.run("pre-commit", "autoupdate")
directories = ("hub",) if hub_only else ("controller", "hub", "inithome")
for directory in directories:
command = ["uv", "pip", "compile", "--upgrade", "--universal"]
if generate_hashes:
command.append("--generate-hashes")
# JupyterHub uses an old Python version.
if directory == "hub":
command.extend(("-p", "3.10"))
session.run(
*command,
"--output-file",
f"{directory}/requirements/main.txt",
f"{directory}/requirements/main.in",
)
session.run(
*command,
"--output-file",
f"{directory}/requirements/dev.txt",
f"{directory}/requirements/dev.in",
)
print("\nTo refresh the development venv, run:\n\n\tnox -s init\n")
@nox.session(name="venv-init")
def venv_init(session: nox.Session) -> None:
"""Set up a development venv.
Create a venv in the current directory, replacing any existing one.
"""
session.run("python", "-m", "venv", ".venv", "--clear")
_install_dev(session, bin_prefix=".venv/bin/")
print(
"\nTo activate this virtual env, run:\n\n\tsource .venv/bin/activate\n"
)
@nox.session(name="init", python=False, venv_backend="none")
def init(session: nox.Session) -> None:
"""Set up the development environment in the current virtual env."""
_install_dev(session, bin_prefix="")
@nox.session
def lint(session: nox.Session) -> None:
"""Run pre-commit hooks."""
session.install("--upgrade", "pre-commit")
session.run("pre-commit", "run", "--all-files", *session.posargs)
@nox.session
def typing(session: nox.Session) -> None:
"""Check controller type annotations with mypy."""
session.install(
"-r",
"./controller/requirements/main.txt",
"-r",
"./controller/requirements/dev.txt",
)
session.install("-e", "./controller")
session.run(
"mypy",
*session.posargs,
"noxfile.py",
"controller/src",
"controller/tests",
)
@nox.session(name="typing-client")
def typing_client(session: nox.Session) -> None:
"""Check client type annotations with mypy."""
session.install(
"-e ./client[dev]",
)
session.run(
"mypy",
*session.posargs,
"noxfile.py",
"client/tests",
)
# "client" is, alas, a too-generic name, and you get "imported twice"
# if you don't split it like this, because there's some builtin. So
# let's do the package explicitly with -p.
session.run("mypy", *session.posargs, "-p", "rubin.nublado.client")
@nox.session(name="typing-hub")
def typing_hub(session: nox.Session) -> None:
"""Check hub plugin type annotations with mypy."""
session.install(
"-r",
"./hub/requirements/main.txt",
"-r",
"./hub/requirements/dev.txt",
)
session.install("--no-deps", "-e", "./authenticator")
session.install("--no-deps", "-e", "./spawner")
session.run(
"mypy",
*session.posargs,
"--namespace-packages",
"--explicit-package-bases",
"authenticator/src",
"authenticator/tests",
env={"MYPYPATH": "authenticator/src:authenticator"},
)
session.run(
"mypy",
*session.posargs,
"--namespace-packages",
"--explicit-package-bases",
"spawner/src",
"spawner/tests",
env={"MYPYPATH": "spawner/src:spawner"},
)
@nox.session(name="typing-inithome")
def typing_inithome(session: nox.Session) -> None:
"""Check inithome type annotations with mypy."""
session.install(
"-r",
"./inithome/requirements/main.txt",
"-r",
"./inithome/requirements/dev.txt",
)
session.install("-e", "./inithome")
session.run(
"mypy",
*session.posargs,
"--namespace-packages",
"--explicit-package-bases",
"inithome/src",
"inithome/tests",
env={"MYPYPATH": "inithome/src:inithome"},
)
@nox.session
def test(session: nox.Session) -> None:
"""Run tests of the Nublado controller."""
session.install(
"-r",
"./controller/requirements/main.txt",
"-r",
"./controller/requirements/dev.txt",
)
session.install("-e", "./controller")
_pytest(session, "controller", "controller")
@nox.session(name="test-hub")
def test_hub(session: nox.Session) -> None:
"""Run only tests affecting JupyterHub with its frozen dependencies."""
session.install(
"-r",
"./hub/requirements/main.txt",
"-r",
"./hub/requirements/dev.txt",
)
session.install("--no-deps", "-e", "./authenticator")
session.install("--no-deps", "-e", "./spawner")
_pytest(
session, "authenticator", "rubin.nublado.authenticator", coverage=False
)
_pytest(session, "spawner", "rubin.nublado.spawner")
@nox.session(name="test-inithome")
def test_inithome(session: nox.Session) -> None:
"""Run only tests affecting inithome."""
session.install(
"-r",
"./inithome/requirements/main.txt",
"-r",
"./inithome/requirements/dev.txt",
)
session.install("-e", "./inithome")
_pytest(session, "inithome", "rubin.nublado.inithome", coverage=False)
@nox.session(name="test-client")
def test_client(session: nox.Session) -> None:
"""Run only tests affecting client."""
session.install("-e", "./client[dev]")
_pytest(session, "client", "rubin.nublado.client", coverage=False)
@nox.session
def docs(session: nox.Session) -> None:
"""Build the documentation."""
_install(session)
doctree_dir = (session.cache_dir / "doctrees").absolute()
# https://github.com/sphinx-contrib/redoc/issues/48
redoc_js_path = Path("docs/_build/html/_static/redoc.js")
if redoc_js_path.exists():
redoc_js_path.unlink()
with session.chdir("docs"):
session.run(
"sphinx-build",
"-W",
"--keep-going",
"-n",
"-T",
"-b",
"html",
"-d",
str(doctree_dir),
".",
"./_build/html",
)
@nox.session(name="docs-clean")
def docs_clean(session: nox.Session) -> None:
"""Build the documentation without any cache."""
if Path("docs/_build").exists():
shutil.rmtree("docs/_build")
if Path("docs/dev/api/contents").exists():
shutil.rmtree("docs/dev/api/contents")
docs(session)
@nox.session(name="docs-linkcheck")
def docs_linkcheck(session: nox.Session) -> None:
"""Check documentation links."""
_install(session)
doctree_dir = (session.cache_dir / "doctrees").absolute()
# https://github.com/sphinx-contrib/redoc/issues/48
redoc_js_path = Path("docs/_build/linkcheck/_static/redoc.js")
if redoc_js_path.exists():
redoc_js_path.unlink()
with session.chdir("docs"):
try:
session.run(
"sphinx-build",
"-W",
"--keep-going",
"-n",
"-T",
"-blinkcheck",
"-d",
str(doctree_dir),
".",
"./_build/linkcheck",
)
except CommandFailed:
output_path = Path("_build") / "linkcheck" / "output.txt"
if output_path.exists():
sys.stdout.write(output_path.read_text())
session.error("Link check reported errors")
@nox.session(name="update-deps")
def update_deps(session: nox.Session) -> None:
"""Update pinned server dependencies and pre-commit hooks."""
_update_deps(session)
@nox.session(name="update-deps-hub")
def update_deps_hub(session: nox.Session) -> None:
"""Update pinned JupyterHub dependencies and pre-commit hooks."""
_update_deps(session, hub_only=True)
@nox.session(name="update-deps-no-hashes")
def update_deps_no_hashes(session: nox.Session) -> None:
"""Update pinned server dependencies without hashes.
Used when testing against unreleased dependencies, such as a Git version
of Safir.
"""
_update_deps(session, generate_hashes=False)
@nox.session(name="run")
def run(session: nox.Session) -> None:
"""Run the application in development mode."""
_install(session)
with session.chdir("controller"):
session.run("uvicorn", "controller.main:app", "--reload")