-
Notifications
You must be signed in to change notification settings - Fork 31
/
noxfile.py
399 lines (360 loc) · 11.8 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Copyright (C) 2023 Maxwell G <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or
# https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import annotations
import contextlib
import os
import tempfile
from collections.abc import Iterator, Sequence
from pathlib import Path
import nox
DEFAULT_MODE = os.environ.get("OTHER_ANTSIBULL_MODE", "auto")
IN_CI = "GITHUB_ACTIONS" in os.environ
ALLOW_EDITABLE = os.environ.get("ALLOW_EDITABLE", str(not IN_CI)).lower() in (
"1",
"true",
)
# Always install latest pip version
os.environ["VIRTUALENV_DOWNLOAD"] = "1"
nox.options.sessions = "lint", "test", "coverage"
def install(session: nox.Session, *args, editable=False, **kwargs):
# nox --no-venv
if isinstance(session.virtualenv, nox.virtualenv.PassthroughEnv):
session.warn(f"No venv. Skipping installation of {args}")
return
# Don't install in editable mode in CI or if it's explicitly disabled.
# This ensures that the wheel contains all of the correct files.
if editable and ALLOW_EDITABLE:
args = ("-e", *args)
session.install(*(str(arg) for arg in args), "-U", **kwargs)
def other_antsibull(
mode: str | None = None,
) -> list[str | Path]:
if mode is None:
mode = DEFAULT_MODE
to_install: list[str | Path] = []
args = (
"antsibull-core",
"antsibull-changelog",
"antsibull-docs-parser",
"antsibull-docutils",
"antsibull-fileutils",
)
for project in args:
path = Path("../", project)
path_exists = path.is_dir()
if mode == "auto":
if path_exists:
mode = "local"
else:
mode = "git"
if mode == "local":
if not path_exists:
raise ValueError(f"Cannot install {project}! {path} does not exist!")
if ALLOW_EDITABLE:
to_install.append("-e")
to_install.append(path)
elif mode == "git":
to_install.append(
f"{project} @ "
f"https://github.com/ansible-community/{project}/archive/main.tar.gz"
)
elif mode == "pypi":
to_install.append(project)
else:
raise ValueError(f"install_other_antsibull: invalid argument mode={mode!r}")
return to_install
@nox.session(python=["3.9", "3.10", "3.11", "3.12", "3.13"])
def test(session: nox.Session):
install(
session,
".[test, coverage]",
*other_antsibull(),
editable=True,
)
covfile = Path(session.create_tmp(), ".coverage")
more_args = []
if session.python in {"3.11", "3.12", "3.13"}:
more_args.append("--error-for-skips")
session.run(
"pytest",
"--cov-branch",
"--cov=antsibull_build",
"--cov-report",
"term-missing",
*more_args,
*session.posargs,
env={"COVERAGE_FILE": f"{covfile}", **session.env},
)
@contextlib.contextmanager
def coverage_run(session: nox.Session) -> Iterator[tuple[str, dict[str, str]]]:
build_command = "coverage run -p --branch --source antsibull_build -m antsibull_build.cli.antsibull_build"
tmp = Path(session.create_tmp())
covfile = tmp / ".coverage"
cov_env = {"COVERAGE_FILE": f"{covfile}", **session.env}
yield build_command, cov_env
combined = map(str, tmp.glob(".coverage.*"))
session.run("coverage", "combine", *combined, env=cov_env)
session.run("coverage", "report", env=cov_env)
@nox.session
def coverage_release(session: nox.Session):
"""
Build a test release and report coverage
"""
install(
session,
".[coverage]",
*other_antsibull(),
"ansible-core",
editable=True,
)
tmp = Path(session.create_tmp())
posargs = session.posargs
# Set default settings
if not posargs:
posargs = (
"-e",
"antsibull_ansible_version=7.99.0",
"-e",
"antsibull_ansible_git_version=stable-2.14",
)
collections = tmp / "collections"
session.run(
"ansible-galaxy",
"collection",
"install",
"git+https://github.com/ansible-collections/community.general",
env={"ANSIBLE_COLLECTIONS_PATH": str(collections), **session.env},
)
tmp_env = {}
if tmpdir := os.environ.get("ANTSIBULL_COVERAGE_RELEASE_FORCE_TMPDIR"):
tmp_env["TMPDIR"] = tmpdir
with coverage_run(session) as (build_command, cov_env):
session.run(
"ansible-playbook",
"-vv",
"playbooks/build-single-release.yaml",
*posargs,
"-e",
f"antsibull_build_command={build_command!r}",
env={
"ANSIBLE_COLLECTIONS_PATH": str(collections),
"ANSIBLE_CALLBACK_RESULT_FORMAT": "yaml",
**cov_env,
**tmp_env,
},
)
@nox.session
def coverage(session: nox.Session):
install(session, ".[coverage]", *other_antsibull(), editable=True)
combined = map(str, Path().glob(".nox/*/tmp/.coverage"))
# Combine the results into a single .coverage file in the root
session.run("coverage", "combine", "--keep", *combined)
# Create a coverage.xml for codecov
session.run("coverage", "xml")
# Display the combined results to the user
session.run("coverage", "report", "-m")
@nox.session
def lint(session: nox.Session):
session.notify("formatters")
session.notify("codeqa")
session.notify("typing")
@nox.session
def formatters(session: nox.Session):
install(session, ".[formatters]", *other_antsibull())
posargs = list(session.posargs)
if IN_CI:
posargs.append("--check")
session.run("isort", *posargs, "src", "tests", "noxfile.py")
session.run("black", *posargs, "src", "tests", "noxfile.py")
@nox.session
def codeqa(session: nox.Session):
install(session, ".[codeqa]", *other_antsibull(), editable=True)
session.run("flake8", "src/antsibull_build", *session.posargs)
session.run("pylint", "--rcfile", ".pylintrc.automated", "src/antsibull_build")
session.run("reuse", "lint")
session.run("antsibull-changelog", "lint")
@nox.session
def typing(session: nox.Session):
others = other_antsibull()
install(session, ".[typing]", *others)
session.run("mypy", "src/antsibull_build")
@nox.session
@nox.parametrize(
["major", "version", "extra_args"],
[
["8", "8.1.0", []],
["7", "7.5.0", []],
[
"8",
"8.1.0",
[
"--package-dir=tests/test_data/package-files/force_setup_cfg",
"--force-generate-setup-cfg",
],
],
],
["8.1.0", "7.5.0", "8.1.0_setup_cfg"],
)
def check_package_files(
session: nox.Session, major: str, version: str, extra_args: Sequence[str]
) -> None:
install(session, ".[coverage]", *other_antsibull(), editable=True)
tmp = session.create_tmp()
build_data = Path(tmp, "ansible-build-data")
session.run_always(
"git",
"clone",
"--depth=1",
"https://github.com/ansible-community/ansible-build-data",
str(build_data),
external=True,
)
Path("tests/.cache").mkdir(exist_ok=True)
session.run(
"python",
"tests/verify_package_files.py",
"download",
"--no-force-dl",
version,
)
with coverage_run(session) as (_, cov_env):
session.run(
"coverage",
"run",
"-p",
"--branch",
"--source=antsibull_build",
"tests/verify_package_files.py",
"check",
f"--data-dir={build_data / major}",
version,
*session.posargs,
*extra_args,
env=cov_env,
)
def check_no_modifications(session: nox.Session) -> None:
modified = session.run(
"git",
"status",
"--porcelain=v1",
"--untracked=normal",
external=True,
silent=True,
)
if modified:
session.error(
"There are modified or untracked files. "
"Commit, restore, or remove them before running this"
)
@contextlib.contextmanager
def isolated_src(session: nox.Session):
"""
Create an isolated directory that only contains the latest git HEAD
"""
with tempfile.TemporaryDirectory() as _tmpdir:
tmp = Path(_tmpdir)
session.run(
"git",
"archive",
"HEAD",
f"--output={tmp / 'HEAD.tar'}",
"--prefix=build/",
external=True,
)
with session.chdir(tmp):
session.run("tar", "-xf", "HEAD.tar", external=True)
with session.chdir(tmp / "build"):
yield
@nox.session
def bump(session: nox.Session):
check_no_modifications(session)
if len(session.posargs) not in (1, 2):
session.error(
"Must specify 1-2 positional arguments: nox -e bump -- <version> "
"[ <release_summary_message> ]."
" If release_summary_message has not been specified, "
"a file changelogs/fragments/<version>.yml must exist"
)
version = session.posargs[0]
fragment_file = Path(f"changelogs/fragments/{version}.yml")
if len(session.posargs) == 1:
if not fragment_file.is_file():
session.error(
f"Either {fragment_file} must already exist, "
"or two positional arguments must be provided."
)
# Needs newer antsibull-changelog for hatch version auto-detection support
install(session, "antsibull-changelog[toml] >= 0.26.0", "hatch")
session.run("hatch", "version", version)
if len(session.posargs) > 1:
fragment = session.run(
"python",
"-c",
"import sys, yaml ; "
f"yaml.dump(dict(release_summary={repr(session.posargs[1])}), sys.stdout)",
silent=True,
)
with open(fragment_file, "w") as fp:
fp.write(fragment)
session.run(
"git",
"add",
"src/antsibull_build/__init__.py",
str(fragment_file),
external=True,
)
session.run("git", "commit", "-m", f"Prepare {version}.", external=True)
session.run("antsibull-changelog", "release")
session.run(
"git",
"add",
"CHANGELOG.rst",
"CHANGELOG.md",
"changelogs/changelog.yaml",
"changelogs/fragments/",
# src/antsibull/__init__.py is not committed in the last step
# when the release_summary fragment is created manually
"src/antsibull_build/__init__.py",
external=True,
)
install(session, ".") # Smoke test
session.run("git", "commit", "-m", f"Release {version}.", external=True)
session.run(
"git",
"tag",
"-a",
"-m",
f"antsibull-build {version}",
"--edit",
version,
external=True,
)
dist = Path.cwd() / "dist"
with isolated_src(session):
session.run("hatch", "build", "--clean", str(dist))
@nox.session
def publish(session: nox.Session):
check_no_modifications(session)
install(session, "hatch")
session.run("hatch", "publish", *session.posargs)
session.run("hatch", "version", "post")
session.run("git", "add", "src/antsibull_build/__init__.py", external=True)
session.run("git", "commit", "-m", "Post-release version bump.", external=True)
@nox.session
def install_env(session: nox.Session):
"""
Install antsibull-build and the other project in the the local environment.
Invoke with `nox -e install_env --no-venv`
"""
session.run(
"pip",
"install",
"-U",
".",
*other_antsibull(),
*session.posargs,
external=True,
editable=True,
)