-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
477 lines (391 loc) · 14.3 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
from __future__ import annotations
from contextlib import contextmanager
import importlib.util
import logging
import os
from pathlib import Path
import platform
import shutil
import socket
import sys
import typing as t
log = logging.getLogger(__name__)
import nox
## Set nox options
if importlib.util.find_spec("uv"):
nox.options.default_venv_backend = "uv|virtualenv"
else:
nox.options.default_venv_backend = "virtualenv"
nox.options.reuse_existing_virtualenvs = True
nox.options.error_on_external_run = False
nox.options.error_on_missing_interpreters = False
# nox.options.report = True
## Detect container env, or default to False
if "CONTAINER_ENV" in os.environ:
CONTAINER_ENV: bool = bool(os.environ["CONTAINER_ENV"])
else:
CONTAINER_ENV: bool = False
## Define versions to test
PY_VERSIONS: list[str] = ["3.12", "3.11"]
## Get tuple of Python ver ('maj', 'min', 'mic')
PY_VER_TUPLE: tuple[str, str, str] = platform.python_version_tuple()
## Dynamically set Python version
DEFAULT_PYTHON: str = f"{PY_VER_TUPLE[0]}.{PY_VER_TUPLE[1]}"
# this VENV_DIR constant specifies the name of the dir that the `dev`
# session will create, containing the virtualenv;
# the `resolve()` makes it portable
VENV_DIR = Path("./.venv").resolve()
## At minimum, these paths will be checked by your linters
# Add new paths with nox_utils.append_lint_paths(extra_paths=["..."],)
DEFAULT_LINT_PATHS: list[str] = [
"src",
]
## Set directory for requirements.txt file output
REQUIREMENTS_OUTPUT_DIR: Path = Path("./")
## Configure logging
logging.basicConfig(
level="DEBUG",
format="%(name)s | [%(levelname)s] > %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
## Add logger names to the list to 'silence' them, reducing log noise from 3rd party packages
for _logger in []:
logging.getLogger(_logger).setLevel("WARNING")
@contextmanager
def cd(new_dir) -> t.Generator[None, importlib.util.Any, None]: # type: ignore
"""Context manager to change a directory before executing command."""
prev_dir: str = os.getcwd()
os.chdir(os.path.expanduser(new_dir))
try:
yield
finally:
os.chdir(prev_dir)
def check_path_exists(_path: t.Union[str, Path] | None = None) -> bool:
"""Check the existence of a path.
Params:
p (str | Path): The path to the directory/file to check.
Returns:
(True): If Path defined in `p` exists.
(False): If Path defined in `p` does not exist.
"""
path: Path = Path(f"{_path}")
if "~" in f"{path}":
path = path.expanduser()
_exists: bool = path.exists()
if not _exists:
log.error(FileNotFoundError(f"Could not find path '{path}'."))
return _exists
def install_uv_project(session: nox.Session, external: bool = False) -> None:
"""Method to install uv and the current project in a nox session."""
log.info("Installing uv in session")
session.install("uv")
log.info("Syncing uv project")
session.run("uv", "sync", external=external)
log.info("Installing project")
session.run("uv", "pip", "install", ".", external=external)
def _find_free_port(start_port=8000) -> int:
"""Find a free port starting from a specific port number."""
port = start_port
while True:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
try:
sock.bind(("0.0.0.0", port))
return port
except socket.error:
log.info(f"Port {port} is in use, trying the next port.")
port += 1
def run_docker_cmd(session: nox.Session, compose_file: str, operation: str) -> None:
if compose_file is None:
raise ValueError("Missing a compose_file value.")
if operation is None:
raise ValueError("operation should not be None")
if not check_path_exists(_path=compose_file):
raise FileNotFoundError(f"Could not find compose file: {compose_file}")
valid_operations: list[str] = [
"build",
"build-no-cache",
"up",
"up-build",
"up-recreate",
"down",
]
match operation:
case "build":
session.run(
"docker",
"compose",
"-f",
compose_file,
"build",
external=True,
)
case "build-no-cache":
session.run(
"docker",
"compose",
"-f",
compose_file,
"build",
"--no-cache",
external=True,
)
case "up":
session.run(
"docker",
"compose",
"-f",
compose_file,
"up",
"-d",
external=True,
)
case "up-build":
session.run(
"docker",
"compose",
"-f",
compose_file,
"up",
"-d",
"--build",
external=True,
)
case "up-recreate":
session.run(
"docker",
"compose",
"-f",
compose_file,
"up",
"-d",
"--force-recreate",
external=True,
)
case "down" | "stop":
session.run(
"docker",
"compose",
"-f",
compose_file,
"down",
external=True,
)
case _:
raise ValueError(
f"Invalid Docker compose operation: {operation}. Must be one of {valid_operations}"
)
def run_podman_cmd(session: nox.Session, compose_file: str, operation: str) -> None:
if compose_file is None:
raise ValueError("Missing a compose_file value.")
if operation is None:
raise ValueError("operation should not be None")
if not check_path_exists(_path=compose_file):
raise FileNotFoundError(f"Could not find compose file: {compose_file}")
valid_operations: list[str] = [
"build",
"build-no-cache",
"up",
"up-build",
"up-recreate",
"down",
]
match operation:
case "build":
session.run(
"podman-compose",
"-f",
compose_file,
"build",
external=True,
)
case "build-no-cache":
session.run(
"podman-compose",
"-f",
compose_file,
"build",
"--no-cache",
external=True,
)
case "up":
session.run(
"podman-compose",
"-f",
compose_file,
"up",
"-d",
external=True,
)
case "up-build":
session.run(
"podman-compose",
"-f",
compose_file,
"up",
"-d",
"--build",
external=True,
)
case "up-recreate":
session.run(
"podman-compose", "-f", compose_file, "down", "&&",
"podman-compose",
"-f",
compose_file,
"up",
"-d",
"--force-recreate",
external=True,
)
case "down" | "stop":
session.run(
"podman-compose",
"-f",
compose_file,
"down",
external=True,
)
case _:
raise ValueError(
f"Invalid podman-compose operation: {operation}. Must be one of {valid_operations}"
)
#######################
# Repository Sessions #
#######################
@nox.session(python=[DEFAULT_PYTHON], name="dev-env")
def dev(session: nox.Session) -> None:
"""Sets up a python development environment for the project.
Run this on a fresh clone of the repository to automate building the project with uv.
"""
install_uv_project(session, external=True)
@nox.session(python=[DEFAULT_PYTHON], name="ruff-lint", tags=["ruff", "clean", "lint"])
def run_linter(session: nox.Session, lint_paths: list[str] = DEFAULT_LINT_PATHS) -> None:
"""Nox session to run Ruff code linting."""
if not check_path_exists(_path="ruff.toml"):
if not Path("pyproject.toml").exists():
log.warning(
"""No ruff.toml file found. Make sure your pyproject.toml has a [tool.ruff] section!
If your pyproject.toml does not have a [tool.ruff] section, ruff's defaults will be used.
Double check imports in __init__.py files, ruff removes unused imports by default.
"""
)
session.install("ruff")
log.info("Linting code")
for d in lint_paths:
if not Path(d).exists():
log.warning(f"Skipping lint path '{d}', could not find path")
pass
else:
lint_path: Path = Path(d)
log.info(f"Running ruff imports sort on '{d}'")
session.run(
"ruff",
"check",
lint_path,
"--select",
"I",
"--fix",
)
log.info(f"Running ruff checks on '{d}' with --fix")
session.run(
"ruff",
"check",
lint_path,
"--fix",
)
log.info("Linting noxfile.py")
session.run(
"ruff",
"check",
f"{Path('./noxfile.py')}",
"--fix",
)
@nox.session(name="vulture-check", tags=["coverage", "quality"])
def vulture_check(session: nox.Session) -> None:
install_uv_project(session)
log.info("Installing vulture for dead code checking")
session.install("vulture")
log.info("Running vulture")
session.run("vulture")
##############
# Pre-commit #
##############
@nox.session(python=PY_VERSIONS, name="pre-commit-all")
def run_pre_commit_all(session: nox.Session) -> None:
session.install("pre-commit")
session.run("pre-commit")
log.info("Running all pre-commit hooks")
session.run("pre-commit", "run")
@nox.session(python=PY_VERSIONS, name="pre-commit-update")
def run_pre_commit_autoupdate(session: nox.Session) -> None:
session.install("pre-commit")
log.info("Running pre-commit autoupdate")
session.run("pre-commit", "autoupdate")
@nox.session(python=PY_VERSIONS, name="pre-commit-nbstripout")
def run_pre_commit_nbstripout(session: nox.Session) -> None:
session.install("pre-commit")
log.info("Running nbstripout pre-commit hook")
session.run("pre-commit", "run", "nbstripout")
##########
# MKDocs #
##########
@nox.session(python=[DEFAULT_PYTHON], name="publish-mkdocs", tags=["mkdocs", "publish"])
def publish_mkdocs(session: nox.Session) -> None:
install_uv_project(session)
log.info("Publishing MKDocs site to Github Pages")
session.run("mkdocs", "gh-deploy")
@nox.session(python=[DEFAULT_PYTHON], name="serve-mkdocs-check-links", tags=["mkdocs", "lint"])
def check_mkdocs_links(session: nox.Session) -> None:
install_uv_project(session)
free_port = _find_free_port(start_port=8000)
log.info(f"Serving MKDocs site with link checking enabled on port {free_port}")
try:
os.environ["ENABLED_HTMLPROOFER"] = "true"
session.run("mkdocs", "serve", "--dev-addr", f"0.0.0.0:{free_port}")
except Exception as exc:
msg = f"({type(exc)}) Unhandled exception checking mkdocs site links. Details: {exc}"
log.error(msg)
raise exc
@nox.session(python=DEFAULT_PYTHON, name="serve-mkdocs", tags=["mkdocs", "serve"])
def serve_mkdocs(session: nox.Session) -> None:
install_uv_project(session)
free_port = _find_free_port(start_port=8000)
log.info(f"Serving MKDocs site on port {free_port}")
try:
session.run("mkdocs", "serve", "--dev-addr", f"0.0.0.0:{free_port}")
except Exception as exc:
msg = f"({type(exc)}) Unhandled exception serving MKDocs site. Details: {exc}"
log.error(msg)
################
# Cookiecutter #
################
@nox.session(python=DEFAULT_PYTHON, name="new-docker-template-page", tags=["mkdocs", "cookiecutter", "template"])
def new_docker_template_page(session: nox.Session) -> None:
from cookiecutter.main import cookiecutter
session.install("cookiecutter")
log.info("Answer the prompts to create a new page in docs/programming/docker/my_containers")
COOKIECUTTER_TEMPLATE_FILE: Path = Path("./templates/docs/containers/docker_template_page")
DOCKER_CONTAINER_DOCS_ROOT: Path = Path("./docs/template/docker")
if not COOKIECUTTER_TEMPLATE_FILE.exists():
log.warning(f"Could not find cookiecutter template at path '{COOKIECUTTER_TEMPLATE_FILE}'.")
while True:
docs_section_choice = input("Which section directory will the template be exported to (i.e. automation, databases): ")
if docs_section_choice is None or docs_section_choice == "":
log.warning("You must type a directory name that exists in ./docs/programming/docker/my_containers")
CONTAINER_SECTION = DOCKER_CONTAINER_DOCS_ROOT / docs_section_choice
if not CONTAINER_SECTION.exists():
# mkdir_choice = input(f"[WARNING] Container directory section '{CONTAINER_SECTION}' does not exist. Create directory now? (Y/N): ")
log.warning(f"Could not find section '{CONTAINER_SECTION}'. Creating path '{CONTAINER_SECTION}'")
try:
CONTAINER_SECTION.mkdir(parents=True, exist_ok=True)
except Exception as exc:
msg = f"({type(exc)}) Error creating section '{CONTAINER_SECTION}'. Details: {exc}"
log.error(msg)
raise exc
break
log.info(f"Rendering template [{COOKIECUTTER_TEMPLATE_FILE}] to [{CONTAINER_SECTION}]")
try:
cookiecutter(template=str(COOKIECUTTER_TEMPLATE_FILE), output_dir=str(CONTAINER_SECTION), no_input=False)
except Exception as exc:
msg = f"({type(exc)}) Error rendering template. Details: {exc}"
log.error(msg)