forked from reef-technologies/cookiecutter-rt-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
260 lines (213 loc) · 7.93 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
"""
nox configuration for cookiecutter project template.
"""
from __future__ import annotations
import contextlib
import functools
import hashlib
import json
import os
import subprocess
import tempfile
from pathlib import Path
import nox
CI = os.environ.get("CI") is not None
ROOT = Path(".")
PYTHON_VERSIONS = ["3.11"]
PYTHON_DEFAULT_VERSION = PYTHON_VERSIONS[-1]
# tested default config overrides
CRUFT_TESTED_CONFIG_MATRIX = {
"default": {},
}
CRUFT_TESTED_CONFIGS = os.getenv("CRUFT_TESTED_CONFIGS", ",".join(CRUFT_TESTED_CONFIG_MATRIX)).split(",")
nox.options.default_venv_backend = "venv"
nox.options.reuse_existing_virtualenvs = True
# In CI, use Python interpreter provided by GitHub Actions
if CI:
nox.options.force_venv_backend = "none"
MD_PATHS = ["*.md"]
def get_cruft_config(config_name="default", **kw):
with (Path(__file__).parent / "cookiecutter.json").open() as f:
cruft_config = json.load(f)
overrides = CRUFT_TESTED_CONFIG_MATRIX[config_name]
complete_config = {**cruft_config, **overrides, **kw}
config_hash = hashlib.sha256(json.dumps(complete_config, sort_keys=True).encode()).hexdigest()
complete_config["django_project_name"] = f"{config_name}_{config_hash[:8]}"
complete_config["repostory_name"] = complete_config["django_project_name"].replace("_", "-")
return complete_config
@contextlib.contextmanager
def with_dirty_commit(session):
"""
Returned context manager will commit changes to the git repository if it is dirty.
This is needed because tools like `cruft` only use committed changes.
"""
is_dirty = not CI and subprocess.run(["git", "diff", "--quiet"], check=False).returncode
if is_dirty:
with tempfile.TemporaryDirectory(prefix="rt_tmpl_repo") as tmpdir:
session.log(f"Found dirty git repository, temporarily committing changes in {tmpdir}")
subprocess.run(["cp", "-r", ".", tmpdir], check=True)
with session.chdir(tmpdir):
subprocess.run(["git", "add", "-A"], check=True)
subprocess.run(["git", "commit", "-m", "nox: dirty commit"], check=True)
yield
else:
yield
@functools.lru_cache
def _list_files() -> list[Path]:
file_list = []
for cmd in (
["git", "ls-files"],
["git", "ls-files", "--others", "--exclude-standard"],
):
cmd_result = subprocess.run(cmd, check=True, text=True, capture_output=True)
file_list.extend(cmd_result.stdout.splitlines())
return [Path(p) for p in file_list]
def list_files(suffix: str | None = None) -> list[Path]:
"""List all non-files not-ignored by git."""
file_paths = _list_files()
if suffix is not None:
file_paths = [p for p in file_paths if p.suffix == suffix]
return file_paths
def run_readable(session, mode="check"):
session.run(
"docker",
"run",
"--platform",
"linux/amd64",
"--rm",
"-v",
f"{ROOT.absolute()}:/data",
"-w",
"/data",
"ghcr.io/bobheadxi/readable:v0.5.0@sha256:423c133e7e9ca0ac20b0ab298bd5dbfa3df09b515b34cbfbbe8944310cc8d9c9",
mode,
*MD_PATHS,
external=True,
)
def run_shellcheck(session, mode="check"):
shellcheck_cmd = [
"docker",
"run",
"--platform",
"linux/amd64", # while this image is multi-arch, we cannot use digest with multi-arch images
"--rm",
"-v",
f"{ROOT.absolute()}:/mnt",
"-w",
"/mnt",
"-q",
"koalaman/shellcheck:0.9.0@sha256:a527e2077f11f28c1c1ad1dc784b5bc966baeb3e34ef304a0ffa72699b01ad9c",
]
files = list_files(suffix=".sh")
if not files:
session.log("No shell files found")
return
shellcheck_cmd.extend(files)
if mode == "fmt":
with tempfile.NamedTemporaryFile(mode="w+") as diff_file:
session.run(
*shellcheck_cmd,
"--format=diff",
external=True,
stdout=diff_file,
success_codes=[0, 1],
)
diff_file.seek(0)
diff = diff_file.read()
if len(diff.splitlines()) > 1: # ignore single-line message
session.log("Applying shellcheck patch:\n%s", diff)
subprocess.run(
["patch", "-p1"],
input=diff,
text=True,
check=True,
)
session.run(*shellcheck_cmd, external=True)
@nox.session(name="format", python=PYTHON_DEFAULT_VERSION)
def format_(session):
"""Lint the code and apply fixes in-place whenever possible."""
session.run("pip", "install", "-e", ".[format]")
session.run("ruff", "check", "--fix", ".")
run_shellcheck(session, mode="fmt")
run_readable(session, mode="fmt")
session.run("ruff", "format", ".")
@nox.session(python=PYTHON_DEFAULT_VERSION)
def lint(session):
"""Run linters in readonly mode."""
session.run("pip", "install", "-e", ".[lint]")
session.run("ruff", "check", "--diff", "--unsafe-fixes", ".")
session.run("codespell", ".")
run_shellcheck(session, mode="check")
run_readable(session, mode="check")
session.run("ruff", "format", "--diff", ".")
@contextlib.contextmanager
def crufted_project(session, cruft_config):
session.run("pip", "install", "-e", ".")
tmpdir = crufted_project.tmpdir
if not tmpdir:
session.notify("cleanup_crufted_project")
crufted_project.tmpdir = tmpdir = tempfile.TemporaryDirectory(prefix="rt_crufted_")
tmpdir_path = Path(tmpdir.name)
tmpdir_path.mkdir(exist_ok=True)
project_path = tmpdir_path / cruft_config["repostory_name"]
if not project_path.exists():
session.log("Creating project in %s", tmpdir.name)
with with_dirty_commit(session):
session.run(
"cruft",
"create",
".",
"--output-dir",
str(tmpdir_path),
"--no-input",
"--extra-context",
json.dumps(cruft_config),
)
with session.chdir(project_path):
session.run("git", "init", external=True)
session.run("./setup-dev.sh", external=True)
with session.chdir(project_path):
yield project_path
crufted_project.tmpdir = None
def rm_root_owned(session, dirpath):
assert not ROOT.is_relative_to(dirpath) # sanity check before we nuke dirpath
children = sorted(dirpath.iterdir())
session.run(
"docker",
"run",
"--rm",
"-v",
f"{dirpath}:/tmpdir/",
"alpine:3.18.0",
"rm",
"-rf",
*[f"/tmpdir/{f.name}" for f in children],
external=True,
)
@contextlib.contextmanager
def docker_up(session):
session.run("docker", "compose", "up", "-d")
try:
yield
finally:
session.run("docker", "compose", "down", "-v", "--remove-orphans")
@nox.session(python=PYTHON_DEFAULT_VERSION, tags=["crufted_project"])
@nox.parametrize("cruft_config_name", CRUFT_TESTED_CONFIGS)
def lint_crufted_project(session, cruft_config_name):
cruft_config = get_cruft_config(cruft_config_name)
with crufted_project(session, cruft_config):
session.run("nox", "-s", "lint") # TODO: RT-49 re-enable 'type_check'
@nox.session(python=PYTHON_DEFAULT_VERSION, tags=["crufted_project"])
@nox.parametrize("cruft_config_name", CRUFT_TESTED_CONFIGS)
def test_crufted_project(session, cruft_config_name):
cruft_config = get_cruft_config(cruft_config_name)
with crufted_project(session, cruft_config):
with docker_up(session):
session.run("nox", "-s", "test")
@nox.session(python=PYTHON_DEFAULT_VERSION)
def cleanup_crufted_project(session):
if crufted_project.tmpdir:
# workaround for docker compose creating root-owned files
rm_root_owned(session, Path(crufted_project.tmpdir.name))
crufted_project.tmpdir.cleanup()
crufted_project.tmpdir = None