forked from ansible/ansible-documentation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
noxfile.py
282 lines (229 loc) · 7.86 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
from __future__ import annotations
import os
import shlex
import shutil
from argparse import ArgumentParser, BooleanOptionalAction
from glob import iglob
from pathlib import Path
from typing import cast
import nox
LINT_FILES: tuple[str, ...] = (
"hacking/pr_labeler/pr_labeler",
"hacking/tagger/tag.py",
"noxfile.py",
*iglob("docs/bin/*.py"),
)
PINNED = os.environ.get("PINNED", "true").lower() in {"1", "true"}
nox.options.sessions = ("clone-core", "lint", "checkers", "make")
def _set_env_verbose(session: nox.Session, **env: str) -> dict[str, str]:
"""
Helper function to verbosely set environment variables
"""
final_env: dict[str, str] = {}
for key, value in env.items():
final_env[key] = value
session.log(f"export {key}={shlex.quote(value)}")
return final_env
def install(session: nox.Session, *args, req: str, **kwargs):
if PINNED:
pip_constraint = f"tests/{req}.txt"
# Set constraint environment variables for both pip and uv to support
# the nox uv backend
env = _set_env_verbose(
session,
PIP_CONSTRAINT=pip_constraint,
UV_CONSTRAINT=pip_constraint,
UV_BUILD_CONSTRAINT=pip_constraint,
)
kwargs.setdefault("env", {}).update(env)
session.install("-r", f"tests/{req}.in", *args, **kwargs)
CONTAINER_ENGINES = ("podman", "docker")
CHOSEN_CONTAINER_ENGINE = os.environ.get("CONTAINER_ENGINE")
ACTIONLINT_IMAGE = "docker.io/rhysd/actionlint"
def _get_container_engine(session: nox.Session) -> str:
path: str | None = None
if CHOSEN_CONTAINER_ENGINE:
path = shutil.which(CHOSEN_CONTAINER_ENGINE)
if not path:
session.error(
f"CONTAINER_ENGINE {CHOSEN_CONTAINER_ENGINE!r} does not exist!"
)
return path
for engine in CONTAINER_ENGINES:
if path := shutil.which(engine):
return path
session.error(
f"None of the following container engines were found: {CONTAINER_ENGINES}."
f" {session.name} requires a container engine installed."
)
@nox.session
def static(session: nox.Session):
"""
Run static checkers
"""
install(session, req="static")
session.run("ruff", "check", *session.posargs, *LINT_FILES)
@nox.session
def formatters(session: nox.Session):
"""
Reformat code
"""
install(session, req="formatters")
session.run("isort", *session.posargs, *LINT_FILES)
session.run("black", *session.posargs, *LINT_FILES)
@nox.session
def formatters_check(session: nox.Session):
"""
Check code formatting without making changes
"""
install(session, req="formatters")
session.run("isort", "--check", *session.posargs, *LINT_FILES)
session.run("black", "--check", *session.posargs, *LINT_FILES)
@nox.session
def typing(session: nox.Session):
install(session, req="typing")
session.run("mypy", *session.posargs, *LINT_FILES)
@nox.session
def spelling(session: nox.Session):
"""
Spell check RST documentation
"""
install(session, req="spelling")
session.run(
"codespell",
"docs/docsite",
*session.posargs,
)
@nox.session
def actionlint(session: nox.Session) -> None:
"""
Run actionlint to lint Github Actions workflows.
The actionlint tool is run in a Podman/Docker container.
"""
engine = _get_container_engine(session)
session.run_always(engine, "pull", ACTIONLINT_IMAGE, external=True)
session.run(
engine,
"run",
"--rm",
# fmt: off
"--volume", f"{Path.cwd()}:/pwd:z",
"--workdir", "/pwd",
# fmt: on
ACTIONLINT_IMAGE,
*session.posargs,
external=True,
)
@nox.session
def lint(session: nox.Session):
session.notify("typing")
session.notify("static")
session.notify("formatters")
session.notify("spelling")
session.notify("actionlint")
requirements_files = list(
{path.name.replace(".in", "") for path in Path("tests").glob("*in")}
- {"constraints", "constraints-base"}
)
@nox.session(name="pip-compile", python=["3.11"])
@nox.parametrize(["req"], requirements_files, requirements_files)
def pip_compile(session: nox.Session, req: str):
# .pip-tools.toml was introduced in v7
session.install("pip-tools >= 7")
# Use --upgrade by default unless a user passes -P.
args = list(session.posargs)
# Support a custom --check flag to fail if pip-compile made any changes
# so we can check that that lockfiles are in sync with the input (.in) files.
check_mode = "--check" in args
if check_mode:
# Remove from args, as pip-compile doesn't actually support --check.
args.remove("--check")
elif not any(
arg.startswith(("-P", "--upgrade-package", "--no-upgrade")) for arg in args
):
args.append("--upgrade")
# fmt: off
session.run(
"pip-compile",
"--output-file", f"tests/{req}.txt",
*args,
f"tests/{req}.in",
)
# fmt: on
if check_mode and session.run("git", "diff", "tests", silent=True, external=True):
session.error("Check mode: files were changed")
@nox.session(name="clone-core")
def clone_core(session: nox.Session):
"""
Clone relevant portions of ansible-core from ansible/ansible into the current
source tree to facilitate building docs.
"""
session.run_always("python", "docs/bin/clone-core.py", *session.posargs)
checker_tests = [
path.with_suffix("").name for path in Path("tests/checkers/").glob("*.py")
]
def _clone_core_check(session: nox.Session) -> None:
"""
Helper function to run the clone-core script with "--check"
"""
session.run("python", "docs/bin/clone-core.py", "--check")
def _relaxed_parser(session: nox.Session) -> ArgumentParser:
"""
Generate an argument parser with a --relaxed option.
"""
parser = ArgumentParser(prog=f"nox -e {session.name} --")
parser.add_argument(
"--relaxed",
default=False,
action=BooleanOptionalAction,
help="Whether to use requirements-relaxed file. (Default: %(default)s)",
)
return parser
def _env_python(session: nox.Session) -> str:
"""
Get the full path to an environment's python executable
"""
out = cast(
str,
session.run("python", "-c", "import sys; print(sys.executable)", silent=True),
)
return out.strip()
@nox.session
@nox.parametrize(["test"], checker_tests, checker_tests)
def checkers(session: nox.Session, test: str):
"""
Run docs build checkers
"""
args = _relaxed_parser(session).parse_args(session.posargs)
install(session, req="requirements-relaxed" if args.relaxed else "requirements")
_clone_core_check(session)
session.run("make", "-C", "docs/docsite", "clean", external=True)
session.run("python", "tests/checkers.py", test)
@nox.session
def make(session: nox.Session):
"""
Generate HTML from documentation source using the Makefile
"""
parser = _relaxed_parser(session)
parser.add_argument(
"make_args", nargs="*", help="Specify make targets as arguments"
)
args = parser.parse_args(session.posargs)
install(session, req="requirements-relaxed" if args.relaxed else "requirements")
_clone_core_check(session)
make_args: list[str] = [
f"PYTHON={_env_python(session)}",
*(args.make_args or ("clean", "coredocs")),
]
session.run("make", "-C", "docs/docsite", *make_args, external=True)
@nox.session
def tag(session: nox.Session):
"""
Check the core repo for new releases and create tags in ansible-documentation
"""
install(session, req="tag")
args = list(session.posargs)
# If run without any arguments, default to "tag"
if not any(arg.startswith(("hash", "mantag", "new-tags", "tag")) for arg in args):
args.append("tag")
session.run("python", "hacking/tagger/tag.py", *args)