-
Notifications
You must be signed in to change notification settings - Fork 2
/
noxfile.py
217 lines (191 loc) · 6.71 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
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or
# https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2023 Maxwell G <[email protected]>
import contextlib
import os
import tempfile
from pathlib import Path
import nox
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(*args, "-U", **kwargs)
@nox.session(python=["3.9", "3.10", "3.11", "3.12", "3.13"])
def test(session: nox.Session):
install(
session,
".[test, coverage]",
editable=True,
)
covfile = Path(session.create_tmp(), ".coverage")
more_args = []
if session.python in {"3.12", "3.13"}:
more_args.append("--error-for-skips")
session.run(
"pytest",
"--cov-branch",
"--cov=antsibull_fileutils",
"--cov-report",
"term-missing",
*more_args,
*session.posargs,
env={"COVERAGE_FILE": f"{covfile}", **session.env},
)
@nox.session
def coverage(session: nox.Session):
install(session, ".[coverage]", 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]")
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]", editable=True)
session.run("flake8", "src", *session.posargs)
session.run("pylint", "--rcfile", ".pylintrc.automated", "src/antsibull_fileutils")
session.run("reuse", "lint")
session.run("antsibull-changelog", "lint")
@nox.session
def typing(session: nox.Session):
install(session, ".[typing]", editable=True)
session.run("mypy", "src/antsibull_fileutils")
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_fileutils/__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_fileutils/__init__.py is not committed in the last step
# when the release_summary fragment is created manually
"src/antsibull_fileutils/__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-fileutils {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)
version = session.run("hatch", "version", silent=True).strip()
session.run("hatch", "version", "post")
session.run("git", "add", "src/antsibull_fileutils/__init__.py", external=True)
session.run("git", "commit", "-m", "Post-release version bump.", external=True)