forked from reef-technologies/cookiecutter-rt-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoxfile.py
191 lines (152 loc) · 5.57 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
"""
nox configuration for cookiecutter project template.
"""
from __future__ import annotations
import contextlib
import functools
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]
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"]
@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())
file_paths = [Path(p) for p in file_list]
return file_paths
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", ".")
session.run("codespell", ".")
run_shellcheck(session, mode="check")
run_readable(session, mode="check")
session.run("ruff", "format", "--diff", ".")
@contextlib.contextmanager
def crufted_project(session):
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_")
session.log("Creating project in %s", tmpdir.name)
session.run("cruft", "create", ".", "--output-dir", tmpdir.name, "--no-input")
project_path = Path(tmpdir.name) / "project"
with session.chdir(project_path):
session.run("git", "init", external=True)
session.run("./setup-dev.sh", external=True)
else:
project_path = Path(tmpdir.name) / "project"
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
session.run("docker", "run", "--rm", "-v", f"{dirpath}:/tmpdir/", "alpine:3.18.0", "rm", "-rf", "/tmpdir/project")
@contextlib.contextmanager
def docker_up(session):
session.run("docker", "compose", "up", "-d")
yield
session.run("docker", "compose", "down", "-v", "--remove-orphans")
@nox.session(python=PYTHON_DEFAULT_VERSION, tags=["crufted_project"])
def lint_crufted_project(session):
with crufted_project(session):
session.run("nox", "-s", "lint") # TODO: RT-49 re-enable 'type_check'
@nox.session(python=PYTHON_DEFAULT_VERSION, tags=["crufted_project"])
def test_crufted_project(session):
with crufted_project(session):
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, crufted_project.tmpdir.name)
crufted_project.tmpdir.cleanup()
crufted_project.tmpdir = None