forked from pybamm-team/PyBaMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoxfile.py
More file actions
278 lines (237 loc) · 9.16 KB
/
Copy pathnoxfile.py
File metadata and controls
278 lines (237 loc) · 9.16 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
import os
import sys
from pathlib import Path
import nox
# Options to modify nox behaviour
nox.options.default_venv_backend = "uv|virtualenv"
nox.options.reuse_existing_virtualenvs = True
nox.needs_version = ">= 2025.10.14"
homedir = os.getenv("HOME")
PYBAMM_ENV = {
"PYTHONIOENCODING": "utf-8",
"MPLBACKEND": "Agg",
}
VENV_DIR = Path("./venv").resolve()
def set_environment_variables(env_dict, session):
"""
Sets environment variables for a nox Session object.
Parameters
-----------
session : nox.Session
The session to set the environment variables for.
env_dict : dict
A dictionary of environment variable names and values.
"""
for key, value in env_dict.items():
session.env[key] = value
def install_locked(session, *, extras=None, groups=None):
"""Install pybamm and its dependencies into the session environment.
Two modes, selected by the ``PYBAMM_SOLVER_WHEELS`` environment variable:
* **Unset (local dev, solver CI):** ``uv sync --frozen`` over the workspace.
``pybammsolvers`` is built from the in-repo source via the shared lockfile.
* **Set to a directory of prebuilt solver wheels (the PyBaMM CI matrix):**
install the wheel matching this interpreter by explicit path, then install
pybamm with ``--no-sources`` so the workspace source routing is ignored.
This tests PyBaMM against the *prebuilt in-repo* ``pybammsolvers`` without
recompiling it in every matrix cell (and without needing a from-source
build on Windows, which only exists via cibuildwheel/vcpkg).
``--no-sources`` plus an explicit wheel path is required because the in-repo
solver version collides with the PyPI release, so neither ``--find-links``
resolution nor the workspace source can be relied on to select the in-repo
artifact over the identically-versioned PyPI wheel.
"""
env = {"UV_PROJECT_ENVIRONMENT": session.virtualenv.location}
wheels = os.getenv("PYBAMM_SOLVER_WHEELS")
if wheels:
wheels_path = Path(wheels)
if wheels_path.is_dir():
# The per-OS artifact holds wheels for every Python version; pick the
# one whose interpreter tag matches this cell. The trailing dash in
# "*-cpXY-*" avoids matching free-threaded "cpXYt-" builds.
tag = f"cp{sys.version_info.major}{sys.version_info.minor}"
matches = sorted(wheels_path.glob(f"*-{tag}-*.whl"))
if not matches:
session.error(
f"PYBAMM_SOLVER_WHEELS={wheels} contains no pybammsolvers "
f"wheel for {tag}"
)
wheel = matches[0].as_posix()
else:
wheel = wheels_path.as_posix()
python_bin = os.path.join(
session.bin, "python.exe" if sys.platform == "win32" else "python"
)
extras_str = f"[{','.join(extras)}]" if extras else ""
cmd = [
"uv",
"pip",
"install",
"--python",
python_bin,
"--no-sources",
wheel,
"-e",
f"./packages/pybamm{extras_str}",
]
for group in groups or []:
# Groups (dev, docs) are defined in the pybamm package, not the root.
cmd.extend(["--group", f"packages/pybamm/pyproject.toml:{group}"])
session.run(*cmd, env=env, external=True)
return
cmd = ["uv", "sync", "--frozen"]
for extra in extras or []:
cmd.extend(["--extra", extra])
for group in groups or []:
cmd.extend(["--group", group])
session.run(*cmd, env=env, external=True)
@nox.session(name="coverage", default=False)
def run_coverage(session):
"""Run the coverage tests and generate an XML report."""
set_environment_variables(PYBAMM_ENV, session=session)
install_locked(session, extras=["all", "jax"], groups=["dev"])
# Using plugin here since coverage runs unit tests on linux with latest python version.
if "CI" in os.environ:
session.install("pytest-github-actions-annotate-failures")
session.run(
"pytest", "--cov=pybamm", "--cov-report=xml", "packages/pybamm/tests/unit"
)
@nox.session(name="integration", default=False)
def run_integration(session):
"""Run the integration tests."""
set_environment_variables(PYBAMM_ENV, session=session)
install_locked(session, extras=["all", "jax", "pydiffsol"], groups=["dev"])
if (
"CI" in os.environ
and sys.version_info[:2] >= (3, 12)
and sys.platform == "linux"
):
session.install("pytest-github-actions-annotate-failures")
session.run("python", "-m", "pytest", "-m", "integration", "packages/pybamm/tests")
@nox.session(name="doctests", default=False)
def run_doctests(session):
"""Run the doctests and generate the output(s) in the docs/build/ directory."""
install_locked(session, extras=["all"], groups=["dev", "docs"])
# Fix for Python 3.12 CI. This can be removed after pybtex is replaced.
session.install("setuptools", silent=False)
session.run(
"python",
"-m",
"pytest",
"--doctest-plus",
"packages/pybamm/src",
)
@nox.session(name="unit", default=True)
def run_unit(session):
"""Run the unit tests."""
set_environment_variables(PYBAMM_ENV, session=session)
install_locked(session, extras=["all", "jax"], groups=["dev"])
session.run("python", "-m", "pytest", "-m", "unit", "packages/pybamm/tests")
@nox.session(name="memory", default=False)
def run_memory(session):
"""Run memory leak tests using pytest-memray (Linux/macOS only)."""
if sys.platform == "win32":
session.skip("memray is not supported on Windows")
set_environment_variables(PYBAMM_ENV, session=session)
install_locked(session, groups=["dev"])
session.run(
"python",
"-m",
"pytest",
"packages/pybamm/tests/memory/",
"-v",
"-o",
"addopts=",
)
@nox.session(name="examples", default=False)
def run_examples(session):
"""Run the examples tests for Jupyter notebooks."""
set_environment_variables(PYBAMM_ENV, session=session)
install_locked(session, extras=["all", "jax"], groups=["dev"])
notebooks_to_test = session.posargs if session.posargs else []
session.run(
"pytest", "--nbmake", *notebooks_to_test, "docs/source/examples/", external=True
)
@nox.session(name="scripts", default=False)
def run_scripts(session):
"""Run the scripts tests for Python scripts."""
set_environment_variables(PYBAMM_ENV, session=session)
install_locked(session, extras=["all", "jax"], groups=["dev"])
# Fix for Python 3.12 CI. This can be removed after pybtex is replaced.
session.install("setuptools", silent=False)
session.run("python", "-m", "pytest", "-m", "scripts", "packages/pybamm/tests")
@nox.session(name="dev", default=False)
def set_dev(session):
"""Install PyBaMM in editable mode."""
set_environment_variables(PYBAMM_ENV, session=session)
session.run(
"uv",
"sync",
"--frozen",
"--extra",
"all",
"--extra",
"jax",
"--group",
"dev",
env={"UV_PROJECT_ENVIRONMENT": os.fsdecode(VENV_DIR)},
external=True,
)
@nox.session(name="tests", default=False)
def run_tests(session):
"""Run the unit tests and integration tests sequentially."""
set_environment_variables(PYBAMM_ENV, session=session)
install_locked(session, extras=["all", "jax"], groups=["dev"])
session.run(
"python",
"-m",
"pytest",
*(
session.posargs
if session.posargs
else ["-m", "unit or integration", "packages/pybamm/tests"]
),
)
@nox.session(name="docs", default=False)
def build_docs(session):
"""Build the documentation and load it in a browser tab, rebuilding on changes."""
envbindir = session.bin
install_locked(session, extras=["all"], groups=["docs"])
# Fix for Python 3.12 CI. This can be removed after pybtex is replaced.
session.install("setuptools", silent=False)
session.chdir("docs")
# Local development
if session.interactive:
session.run(
"sphinx-autobuild",
"-j",
"auto",
"--open-browser",
"-qT",
".",
f"{envbindir}/../tmp/html",
)
# Runs in CI only, treating warnings as errors
# Run in single-threaded mode, see
# https://github.com/pydata/pydata-sphinx-theme/issues/1643
else:
session.run(
"sphinx-build",
"-b",
"html",
"-W",
"--keep-going",
".",
f"{envbindir}/../tmp/html",
)
@nox.session(name="pre-commit", default=True)
def lint(session):
"""Check all files against the defined pre-commit hooks."""
session.install("pre-commit", silent=False)
session.run("pre-commit", "run", "--all-files")
@nox.session(name="quick", reuse_venv=True, default=False)
def run_quick(session):
"""Run integration tests, unit tests, and doctests sequentially"""
run_tests(session)
run_doctests(session)
if __name__ == "__main__":
nox.main()