-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnoxfile.py
More file actions
247 lines (195 loc) · 6.75 KB
/
noxfile.py
File metadata and controls
247 lines (195 loc) · 6.75 KB
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
#!/usr/bin/env -S uv run --script # noqa: EXE001
# /// script
# dependencies = ["nox", "nox_uv"]
# ///
"""Nox setup."""
import argparse
import shutil
from enum import StrEnum, auto
from pathlib import Path
from typing import assert_never, final
import nox
from nox_uv import session
nox.needs_version = ">=2024.3.2"
nox.options.default_venv_backend = "uv"
DIR = Path(__file__).parent.resolve()
@final
class PackageEnum(StrEnum):
"""Enum for package names."""
@staticmethod
def _generate_next_value_(name: str, *_: object, **__: object) -> str:
return name
def __repr__(self) -> str:
return f"{self.value!r}"
coordinax = auto()
api = auto()
astro = auto()
curveframes = auto()
hypothesis = auto()
# =============================================================================
# Comprehensive sessions
@session(
uv_groups=["lint", "test", "docs"],
uv_extras=["workspace"],
reuse_venv=True,
default=True,
)
def all(s: nox.Session, /) -> None: # noqa: A001
"""Run all default sessions."""
s.notify("lint")
s.notify("test")
s.notify("docs")
# =============================================================================
# Linting
@session(uv_groups=["lint"], reuse_venv=True)
def lint(s: nox.Session, /) -> None:
"""Run the linter."""
s.notify("precommit")
# s.notify("pylint") # TODO: re-enable after fixing lint errors
s.notify("ty")
@session(uv_groups=["lint"], reuse_venv=True)
def precommit(s: nox.Session, /) -> None:
"""Run the linter."""
s.run("pre-commit", "run", "--all-files", *s.posargs)
@session(uv_groups=["lint"], reuse_venv=True)
@nox.parametrize("package", list(PackageEnum))
def pylint(s: nox.Session, /, package: PackageEnum) -> None:
"""Run PyLint."""
match package:
case PackageEnum.coordinax:
package_path = "src/coordinax"
case PackageEnum.api:
package_path = "packages/coordinax.api/"
case PackageEnum.astro:
package_path = "packages/coordinax.astro/"
case PackageEnum.curveframes:
package_path = "packages/coordinax.curveframes/"
case PackageEnum.hypothesis:
package_path = "packages/coordinax.hypothesis/"
case _:
assert_never(package)
s.run("pylint", package_path, *s.posargs)
@session(uv_groups=["lint"], reuse_venv=True)
@nox.parametrize("package", list(PackageEnum))
def ty(s: nox.Session, /, package: PackageEnum) -> None:
"""Run ty."""
package_paths: tuple[str, ...]
match package:
case PackageEnum.coordinax:
package_paths = ("src/coordinax", "packages/coordinax.api/")
case PackageEnum.api:
package_paths = ("packages/coordinax.api/",)
case PackageEnum.astro:
package_paths = ("packages/coordinax.astro/",)
case PackageEnum.curveframes:
package_paths = ("packages/coordinax.curveframes/",)
case PackageEnum.hypothesis:
package_paths = ("packages/coordinax.hypothesis/",)
case _:
assert_never(package)
s.run("ty", "check", *package_paths, *s.posargs)
# =============================================================================
# Testing
@session(uv_groups=["test"], reuse_venv=True, default=True)
def test(s: nox.Session, /) -> None:
"""Run the unit and regular tests."""
s.notify("pytest", posargs=s.posargs)
# s.notify("pytest_benchmark", posargs=s.posargs)
def _parse_pytest_paths(package: PackageEnum, /) -> list[str]:
match package:
case PackageEnum.coordinax:
package_paths = ["README.md", "docs", "src/", "tests/"]
case PackageEnum.api:
package_paths = ["packages/coordinax.api/"]
case PackageEnum.astro:
package_paths = ["packages/coordinax.astro/"]
case PackageEnum.curveframes:
package_paths = ["packages/coordinax.curveframes/"]
case PackageEnum.hypothesis:
package_paths = ["packages/coordinax.hypothesis/"]
case _:
assert_never(package)
return package_paths
@session(uv_groups=["test"], uv_extras=["workspace"], reuse_venv=True)
@nox.parametrize("package", list(PackageEnum))
def pytest(s: nox.Session, /, package: PackageEnum) -> None:
"""Run the unit and regular tests."""
package_paths = _parse_pytest_paths(package)
s.run("pytest", *package_paths, *s.posargs)
# =============================================================================
# Documentation
@session(uv_groups=["docs"], uv_extras=["workspace"], reuse_venv=True)
def docs(s: nox.Session, /) -> None:
"""Build the docs. Pass "--serve" to serve. Pass "-b linkcheck" to check links."""
parser = argparse.ArgumentParser()
parser.add_argument("--serve", action="store_true", help="Serve after building")
parser.add_argument(
"-b",
dest="builder",
default="html",
help="Build target (default: html)",
)
parser.add_argument("--output-dir", dest="output_dir", default="_build")
args, posargs = parser.parse_known_args(s.posargs)
if args.builder != "html" and args.serve:
s.error("Must not specify non-HTML builder with --serve")
s.chdir("docs")
# Convert jupytext markdown files to notebooks
s.run(
"jupytext",
"--to",
"notebook",
"guides/perf.md",
"--output",
"guides/perf.ipynb",
)
if args.builder == "linkcheck":
s.run(
"sphinx-build",
"-b",
"linkcheck",
".",
"_build/linkcheck",
*posargs,
)
return
shared_args = (
"-n", # nitpicky mode
"-T", # full tracebacks
f"-b={args.builder}",
f"-d {args.output_dir}/doctrees",
"-D",
"language=en",
".",
f"{args.output_dir}/{args.builder}",
*posargs,
)
if args.serve:
s.run("sphinx-autobuild", *shared_args)
else:
s.run("sphinx-build", "--keep-going", *shared_args)
@session(uv_groups=["docs"], reuse_venv=True)
def build_api_docs(s: nox.Session, /) -> None:
"""Build (regenerate) API docs."""
s.chdir("docs")
s.run(
"sphinx-apidoc",
"-o",
"api/",
"--module-first",
"--no-toc",
"--force",
"../src/coordinax",
)
# =============================================================================
# Packaging
@session(uv_groups=["build"])
def build(s: nox.Session, /) -> None:
"""Build an SDist and wheel."""
build_path = DIR.joinpath("build")
if build_path.exists():
shutil.rmtree(build_path)
s.run("python", "-m", "build")
################################################################################
if __name__ == "__main__":
nox.main()