generated from tweag/project
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnoxfile.py
136 lines (107 loc) · 4.45 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
import hashlib
import os
import shutil
from pathlib import Path
from typing import Iterable
import nox
python_versions = ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
# Use 'uv' to manager Nox' virtualenvs, if available
if shutil.which("uv"):
nox.options.default_venv_backend = "uv"
def patch_binaries_if_needed(session: nox.Session, venv_dir: str) -> None:
"""If we are on Nix, auto-patch any binaries under `venv_dir`.
Detect if we are running under Nix, and auto-patch any pre-built binaries
that were just installed into the Nox virtualenv.
"""
build_inputs = os.environ.get("buildInputs", "") # noqa: SIM112
if "auto-patchelf-hook" not in build_inputs:
return
# We want to invoke autoPatchelf, but it is a shell function in the
# surrounding Nix shell, and thus not directly available to session.run().
# However, we can invoke nix-shell and tell it to run autoPathelf for us:
argv = ["nix-shell", "--run", f"autoPatchelf {venv_dir}"]
session.run(*argv, silent=True, external=True)
def install_groups(
session: nox.Session,
*,
include: Iterable[str] = (),
exclude: Iterable[str] = (),
include_self: bool = True,
) -> None:
"""Install Poetry dependency groups
This function installs the given dependency groups into the session's
virtual environment. When 'include_self' is true (the default), the
function also installs this package (".") and its default dependencies.
We cannot use `poetry install` directly here, because it ignores the
session's virtualenv and installs into Poetry's own virtualenv. Instead, we
use `poetry export` with suitable options to generate a requirements.txt
file which we can then pass to session.install().
Auto-skip the `poetry export` step if the poetry.lock file is unchanged
since the last time this session was run.
"""
if isinstance(session.virtualenv, nox.virtualenv.PassthroughEnv):
session.warn(
"Running outside a Nox virtualenv! We will skip installation here, "
"and simply assume that the necessary dependency groups have "
"already been installed by other means!"
)
return
lockdata = Path("poetry.lock").read_bytes()
digest = hashlib.blake2b(lockdata).hexdigest()
requirements_txt = Path(session.cache_dir, session.name, "reqs_from_poetry.txt")
hashfile = requirements_txt.with_suffix(".hash")
if not hashfile.is_file() or hashfile.read_text() != digest:
requirements_txt.parent.mkdir(parents=True, exist_ok=True)
argv = [
"poetry",
"export",
"--format=requirements.txt",
f"--output={requirements_txt}",
]
if include:
option = "only" if not include_self else "with"
argv.append(f"--{option}={','.join(include)}")
if exclude:
argv.append(f"--without={','.join(exclude)}")
session.run_always(*argv, external=True)
hashfile.write_text(digest)
session.install("-r", str(requirements_txt))
if not session.virtualenv._reused: # noqa: SLF001
patch_binaries_if_needed(session, session.virtualenv.location)
if include_self:
session.install("-e", ".")
@nox.session(python=python_versions)
def tests(session):
install_groups(session, include=["test"])
session.run(
"pytest",
"-x",
"--log-level=debug",
"--durations=10",
"--hypothesis-show-statistics",
*session.posargs,
)
@nox.session(python=python_versions)
def integration_tests(session):
install_groups(session, include=["test"])
session.run("pytest", "-x", "-m", "integration", "--durations=10", *session.posargs)
@nox.session(python=python_versions)
def self_test(session):
# Install all optional dependency groups for a self test
install_groups(session, include=["nox", "test", "lint", "format", "dev"])
session.run("fawltydeps")
@nox.session
def lint(session):
install_groups(session, include=["lint"])
session.run("mypy")
session.run("ruff", "check", ".")
@nox.session
def format(session): # noqa: A001
install_groups(session, include=["format"], include_self=False)
session.run("codespell", "--enable-colors")
session.run("ruff", "format", "--diff", ".")
@nox.session
def reformat(session):
install_groups(session, include=["format"], include_self=False)
session.run("codespell", "--write-changes")
session.run("ruff", "format", ".")