-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathnoxfile.py
More file actions
289 lines (259 loc) · 8.04 KB
/
Copy pathnoxfile.py
File metadata and controls
289 lines (259 loc) · 8.04 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
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
# Thanks to pyca/cryptography for the way to get rust coverage out
# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
# SPDX-FileCopyrightText: pyca/cryptography contributors
# SPDX-FileCopyrightText: Frazer McLean
import glob
import json
import re
import sys
from collections.abc import Iterator
from contextlib import contextmanager
from itertools import chain
from pathlib import Path
from uuid import uuid4
import nox
nox.options.reuse_existing_virtualenvs = True
nox.options.default_venv_backend = "uv"
PYTHON_VERSIONS = [
"3.9",
"3.10",
"3.11",
"3.12",
"3.13",
"3.14",
"3.14t",
"3.15",
"3.15t",
]
SUPPORTED_EXTRAS = [
"brotli",
"execnet",
"jinja",
"nteventlog",
"redis",
"sqlalchemy",
"zmq",
]
SUPPORTED_EXTRAS_FREETHREADING = [
"execnet",
"jinja",
"redis",
"zmq",
]
@nox.session(python="3.13")
def docs(session: nox.Session) -> None:
session.run_install(
"uv",
"sync",
"--no-dev",
"--group=docs",
f"--python={session.virtualenv.location}",
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
temp_dir = session.create_tmp()
session.run(
"sphinx-build",
"-W",
"-b",
"html",
"-d",
f"{temp_dir}/doctrees",
"docs",
"docs/_build/html",
)
session.run(
"sphinx-build",
"-W",
"-b",
"linkcheck",
"-d",
f"{temp_dir}/doctrees",
"docs",
"docs/_build/html",
)
@nox.session(python=PYTHON_VERSIONS)
@nox.parametrize("speedups", [False, True], ids=["nospeedups", "speedups"])
def tests(session: nox.Session, speedups: bool) -> None:
prof_location = Path(".rust-cov", str(uuid4())).absolute()
rustflags = session.env.get("RUSTFLAGS", "")
rustflags = f"-Cinstrument-coverage {rustflags}"
session.env.update(
{
"RUSTFLAGS": rustflags,
"LLVM_PROFILE_FILE": str(prof_location / "cov-%p.profraw"),
}
)
is_freethreaded = session.python.endswith("t")
supported_extras = (
SUPPORTED_EXTRAS_FREETHREADING if is_freethreaded else SUPPORTED_EXTRAS
)
session.run_install(
"uv",
"sync",
*(f"--extra={extra}" for extra in supported_extras),
"--no-editable",
"--no-dev",
"--group=coverage",
"--group=test",
f"--python={session.virtualenv.location}",
*([] if is_freethreaded else ["--group=gevent"]),
env={
"UV_PROJECT_ENVIRONMENT": session.virtualenv.location,
"DISABLE_LOGBOOK_CEXT": "" if speedups else "1",
"SETUPTOOLS_RUST_CARGO_PROFILE": "release",
},
)
session.run("coverage", "run", "-m", "pytest", *session.posargs)
if speedups:
libs = glob.glob(
f"{session.virtualenv.location}/lib/**/logbook/_speedups.*",
recursive=True,
)
[rust_so] = libs
process_rust_coverage(session, [rust_so], prof_location)
@nox.session(name="test-min-deps", python=PYTHON_VERSIONS)
def test_min_deps(session: nox.Session) -> None:
is_freethreaded = session.python.endswith("t")
supported_extras = (
SUPPORTED_EXTRAS_FREETHREADING if is_freethreaded else SUPPORTED_EXTRAS
)
with restore_file("uv.lock"):
session.run_install(
"uv",
"sync",
*(f"--extra={extra}" for extra in supported_extras),
"--no-editable",
"--no-dev",
"--group=coverage",
"--group=test",
*([] if is_freethreaded else ["--group=gevent"]),
"--resolution=lowest-direct",
f"--python={session.virtualenv.location}",
env={
"UV_PROJECT_ENVIRONMENT": session.virtualenv.location,
"SETUPTOOLS_RUST_CARGO_PROFILE": "release",
},
)
session.run("pytest", *session.posargs)
@nox.session(name="test-latest", python=PYTHON_VERSIONS)
def test_latest(session: nox.Session) -> None:
is_freethreaded = session.python.endswith("t")
session.run_install(
"uv",
"sync",
"--no-install-project",
"--no-dev",
"--group=coverage",
"--group=test",
*([] if is_freethreaded else ["--group=gevent"]),
f"--python={session.virtualenv.location}",
env={"UV_PROJECT_ENVIRONMENT": session.virtualenv.location},
)
supported_extras = (
SUPPORTED_EXTRAS_FREETHREADING if is_freethreaded else SUPPORTED_EXTRAS
)
session.run_install(
"uv",
"pip",
"install",
"--upgrade",
f".[{','.join(supported_extras)}]",
*([] if is_freethreaded else ["gevent"]),
f"--python={session.virtualenv.location}",
env={
"UV_PROJECT_ENVIRONMENT": session.virtualenv.location,
"SETUPTOOLS_RUST_CARGO_PROFILE": "release",
},
)
session.run("pytest", *session.posargs)
@nox.session(python=PYTHON_VERSIONS)
def rust(session: nox.Session) -> None:
prof_location = Path(".rust-cov", str(uuid4())).absolute()
rustflags = session.env.get("RUSTFLAGS", "")
rustflags = f"-Cinstrument-coverage {rustflags}"
session.env.update(
{
"RUSTFLAGS": rustflags,
"LLVM_PROFILE_FILE": str(prof_location / "cov-%p.profraw"),
}
)
build_output = session.run(
"cargo",
"test",
"--no-default-features",
"--all",
"--no-run",
"--quiet",
"--message-format=json",
external=True,
silent=True,
)
session.run("cargo", "test", "--no-default-features", "--all", external=True)
if build_output is not None:
assert isinstance(build_output, str)
rust_tests = []
for line in build_output.splitlines():
data = json.loads(line)
if data.get("profile", {}).get("test", False):
rust_tests.extend(data["filenames"])
process_rust_coverage(session, rust_tests, prof_location)
@contextmanager
def restore_file(path: str) -> Iterator[None]:
with open(path, "rb") as f:
original = f.read()
try:
yield
finally:
with open(path, "wb") as f:
f.write(original)
LCOV_SOURCEFILE_RE = re.compile(
r"^SF:.*[\\/]src[\\/]rust[\\/](.*)$", flags=re.MULTILINE
)
BIN_EXT = ".exe" if sys.platform == "win32" else ""
def process_rust_coverage(
session: nox.Session,
rust_binaries: list[str],
prof_raw_location: Path,
) -> None:
# Hitting weird issues merging Windows and Linux Rust coverage, so just
# say the hell with it.
if sys.platform == "win32":
return
target_libdir = session.run(
"rustc", "--print", "target-libdir", external=True, silent=True
)
if target_libdir is not None:
target_bindir = Path(target_libdir).parent / "bin"
profraws = [
str(prof_raw_location / p) for p in prof_raw_location.glob("*.profraw")
]
session.run(
str(target_bindir / ("llvm-profdata" + BIN_EXT)),
"merge",
"-sparse",
*profraws,
"-o",
"rust-cov.profdata",
external=True,
)
lcov_data = session.run(
str(target_bindir / ("llvm-cov" + BIN_EXT)),
"export",
rust_binaries[0],
*chain.from_iterable(["-object", b] for b in rust_binaries[1:]),
"-instr-profile=rust-cov.profdata",
"--ignore-filename-regex=[/\\].cargo[/\\]",
"--ignore-filename-regex=[/\\]rustc[/\\]",
"--ignore-filename-regex=[/\\].rustup[/\\]toolchains[/\\]",
"--ignore-filename-regex=[/\\]target[/\\]",
"--format=lcov",
silent=True,
external=True,
)
assert isinstance(lcov_data, str)
lcov_data = LCOV_SOURCEFILE_RE.sub(
lambda m: "SF:src/rust/" + m.group(1).replace("\\", "/"),
lcov_data.replace("\r\n", "\n"),
)
with open(f"{uuid4()}.lcov", "w") as f:
f.write(lcov_data)