2026-11-27 11:18:31 +01:00
|
|
|
"""Maintenance tasks run in isolated environments.
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-27 11:18:31 +01:00
|
|
|
By default, `uv run nox` runs the sessions that assure code quality:
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-28 04:49:05 +01:00
|
|
|
- "format": Format source files with `autoflake`, `black`, `flynt`, `isort`, and `ruff`
|
2026-11-30 13:15:19 +01:00
|
|
|
- "lint": Lint source files with `flake8`, `isort`, `mypy`, and `ruff`
|
2026-11-27 11:18:31 +01:00
|
|
|
- "test": Run the test suite with `pytest`
|
|
|
|
|
- "doctest": Check if code snippets in the docstrings run
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-27 11:18:31 +01:00
|
|
|
Further sessions run on demand with `uv run nox -s <session>`:
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-27 11:18:31 +01:00
|
|
|
- "docs": Build the documentation with `sphinx`
|
|
|
|
|
- "venv": Set up the local develop environment in ".venv"
|
|
|
|
|
- "deps-audit": Check dependencies for known vulnerabilities
|
|
|
|
|
- "deps-lock": Lock dependencies after manual changes in pyproject.toml
|
|
|
|
|
- "deps-upgrade": Upgrade dependencies within their allowed ranges
|
|
|
|
|
- "deps-upgrade-check": Show newer versions and the age of all pinned releases
|
2026-11-28 03:09:39 +01:00
|
|
|
- "init-project": Install the `pre-commit` hooks and configure `git blame`
|
2026-11-27 11:18:31 +01:00
|
|
|
- "clean-cwd": Run approx. `git clean -X` with minor exceptions
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-27 11:18:31 +01:00
|
|
|
Most sessions accept extra arguments after the "--",
|
|
|
|
|
e.g., `uv run nox -s test -- tests/test_config.py`.
|
2020-08-05 01:45:01 +02:00
|
|
|
"""
|
2020-08-03 21:24:06 +02:00
|
|
|
|
2026-11-25 16:47:53 +01:00
|
|
|
import collections
|
2026-11-26 16:16:53 +01:00
|
|
|
import itertools
|
2020-08-03 21:24:06 +02:00
|
|
|
import os
|
2026-11-26 16:16:53 +01:00
|
|
|
import pathlib
|
2020-08-03 21:24:06 +02:00
|
|
|
import tempfile
|
2026-11-25 16:47:53 +01:00
|
|
|
from collections.abc import Mapping
|
2026-11-26 16:02:00 +01:00
|
|
|
from typing import Any, Generator
|
2020-08-03 21:24:06 +02:00
|
|
|
|
|
|
|
|
import nox
|
|
|
|
|
from nox.sessions import Session
|
2026-11-25 16:47:53 +01:00
|
|
|
from packaging import version as pkg_version
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def nested_defaultdict() -> collections.defaultdict[str, Any]:
|
|
|
|
|
"""Create a multi-level `defaultdict` with variable depth.
|
|
|
|
|
|
|
|
|
|
The returned `dict`ionary never raises a `KeyError`
|
|
|
|
|
but always returns an empty `dict`ionary instead.
|
|
|
|
|
This behavior occurs recursively.
|
|
|
|
|
|
|
|
|
|
Adjusted from: https://stackoverflow.com/a/8702435
|
|
|
|
|
"""
|
|
|
|
|
return collections.defaultdict(nested_defaultdict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def defaultify(obj: Any) -> Any:
|
|
|
|
|
"""Turn nested `dict`s into nested `defaultdict`s."""
|
|
|
|
|
if isinstance(obj, Mapping):
|
|
|
|
|
return collections.defaultdict(
|
|
|
|
|
nested_defaultdict,
|
|
|
|
|
{key: defaultify(val) for key, val in obj.items()},
|
|
|
|
|
)
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_pyproject_toml() -> collections.defaultdict[str, Any]:
|
|
|
|
|
"""Load the contents of the pyproject.toml file.
|
|
|
|
|
|
|
|
|
|
The contents are represented as a `nested_defaultdict`;
|
|
|
|
|
so, missing keys and tables (i.e., "sections" in the .ini format)
|
|
|
|
|
do not result in `KeyError`s but return empty `nested_defaultdict`s.
|
|
|
|
|
"""
|
|
|
|
|
return defaultify(nox.project.load_toml("pyproject.toml"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_supported_python_versions(*, reverse: bool = False) -> list[str]:
|
|
|
|
|
"""Parse the Python versions from the pyproject.toml file."""
|
|
|
|
|
pyproject = load_pyproject_toml()
|
|
|
|
|
version_names = {
|
|
|
|
|
classifier.rsplit(" ")[-1]
|
|
|
|
|
for classifier in pyproject["project"]["classifiers"]
|
|
|
|
|
if classifier.startswith("Programming Language :: Python :: ")
|
|
|
|
|
}
|
|
|
|
|
return sorted(version_names, key=pkg_version.Version, reverse=reverse)
|
2020-08-03 21:24:06 +02:00
|
|
|
|
|
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
PACKAGE = "velofy"
|
|
|
|
|
PACKAGE_LOCATION = "src/"
|
2020-08-05 00:02:40 +02:00
|
|
|
|
2020-08-05 01:34:27 +02:00
|
|
|
DOCS_BUILD = ".cache/docs/"
|
2026-11-27 12:50:07 +01:00
|
|
|
DOCS_LOCATION = "docs/"
|
2020-08-05 01:34:27 +02:00
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
TESTS_LOCATION = "tests/"
|
2020-08-04 22:57:55 +02:00
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
# Locations of all *.py files
|
2020-08-05 01:34:27 +02:00
|
|
|
SRC_LOCATIONS = (
|
2026-11-27 12:50:07 +01:00
|
|
|
f"{DOCS_LOCATION}conf.py",
|
2020-08-09 17:14:23 +02:00
|
|
|
"migrations/env.py",
|
|
|
|
|
"migrations/versions/",
|
2026-11-28 04:11:02 +01:00
|
|
|
"./noxfile.py", # The "./" keeps `bandit` from warning about its module name
|
2026-11-26 15:51:31 +01:00
|
|
|
"scripts/",
|
2026-11-27 12:50:07 +01:00
|
|
|
PACKAGE_LOCATION,
|
|
|
|
|
TESTS_LOCATION,
|
2020-08-05 01:34:27 +02:00
|
|
|
)
|
2020-08-03 21:24:06 +02:00
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
|
2026-11-25 16:47:53 +01:00
|
|
|
SUPPORTED_PYTHONS = load_supported_python_versions()
|
|
|
|
|
|
|
|
|
|
# Must be listed in the "classifiers", which is checked at the bottom
|
2026-11-27 12:50:07 +01:00
|
|
|
# of this file, and satisfy "requires-python" in pyproject.toml
|
2026-11-25 16:47:53 +01:00
|
|
|
MAIN_PYTHON = "3.13"
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
|
2026-11-25 16:57:50 +01:00
|
|
|
nox.options.default_venv_backend = "uv"
|
|
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
# Use a unified .cache/ folder for all tools
|
2020-08-03 21:24:06 +02:00
|
|
|
nox.options.envdir = ".cache/nox"
|
|
|
|
|
|
2026-11-25 15:54:16 +01:00
|
|
|
# All tools except `git` and `uv` are project dependencies
|
|
|
|
|
# => Avoid accidental successes if the environment is not set up properly
|
2020-08-03 21:24:06 +02:00
|
|
|
nox.options.error_on_external_run = True
|
|
|
|
|
|
2026-11-26 13:41:49 +01:00
|
|
|
# Always start with fresh virtual environments
|
|
|
|
|
# unless the "-r" or "-R" flags are passed in
|
|
|
|
|
nox.options.reuse_venv = "no"
|
|
|
|
|
|
|
|
|
|
# Do not run further sessions once one fails
|
|
|
|
|
nox.options.stop_on_first_error = True
|
|
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
# Run only the code quality checks by default
|
2020-08-03 21:24:06 +02:00
|
|
|
nox.options.sessions = (
|
|
|
|
|
"format",
|
|
|
|
|
"lint",
|
2020-08-05 01:45:01 +02:00
|
|
|
"test",
|
2026-11-27 10:45:35 +01:00
|
|
|
"doctest",
|
2020-08-03 21:24:06 +02:00
|
|
|
)
|
|
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
|
|
|
|
|
# Make `uv` use its own `python`
|
2026-11-25 16:57:50 +01:00
|
|
|
# because only then are the headers needed to build `rpy2` from source included
|
|
|
|
|
os.environ.setdefault("UV_PYTHON_PREFERENCE", "only-managed")
|
|
|
|
|
|
2026-11-26 13:44:19 +01:00
|
|
|
# Where to keep temporary files (e.g., the constraints file in `install_pinned()`)
|
|
|
|
|
tempfile.tempdir = ".cache/tmp"
|
|
|
|
|
os.makedirs(tempfile.tempdir, exist_ok=True)
|
|
|
|
|
|
2020-08-03 21:24:06 +02:00
|
|
|
|
2026-11-25 16:47:53 +01:00
|
|
|
@nox.session(name="format", python=MAIN_PYTHON)
|
2026-11-26 16:23:12 +01:00
|
|
|
def format_code(session: Session) -> None:
|
2026-11-28 04:49:05 +01:00
|
|
|
"""Format source files with `autoflake`, `black`, `flynt`, `isort`, and `ruff`.
|
2020-08-04 02:55:41 +02:00
|
|
|
|
|
|
|
|
If no extra arguments are provided, all source files are formatted.
|
|
|
|
|
Otherwise, they are interpreted as paths the formatters work on recursively.
|
|
|
|
|
"""
|
2026-11-25 17:50:06 +01:00
|
|
|
start(session)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
# The formatters do not need `velofy` installed
|
|
|
|
|
# because they work on the source files directly
|
2026-11-28 04:49:05 +01:00
|
|
|
install_pinned(session, "autoflake", "black", "flynt", "isort", "ruff")
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
# Interpret extra arguments as `locations` of source files
|
2020-08-03 21:39:49 +02:00
|
|
|
locations = session.posargs or SRC_LOCATIONS
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
# `autoflake` changes files only with the "in-place = true"
|
2026-11-25 16:03:14 +01:00
|
|
|
# in the "[tool.autoflake]" section in pyproject.toml
|
2020-08-03 21:39:49 +02:00
|
|
|
session.run("autoflake", "--version")
|
2026-11-25 16:03:14 +01:00
|
|
|
session.run("autoflake", *locations)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-28 04:49:05 +01:00
|
|
|
session.run("flynt", "--version")
|
|
|
|
|
session.run("flynt", *locations)
|
|
|
|
|
|
2020-08-03 21:39:49 +02:00
|
|
|
session.run("black", "--version")
|
|
|
|
|
session.run("black", *locations)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-25 16:42:41 +01:00
|
|
|
session.run("isort", "--version")
|
|
|
|
|
session.run("isort", *locations)
|
2020-08-03 21:24:06 +02:00
|
|
|
|
2026-11-28 04:43:49 +01:00
|
|
|
session.run("ruff", "--version")
|
|
|
|
|
session.run("ruff", "check", "--fix-only", *locations)
|
|
|
|
|
session.run("ruff", "format", *locations)
|
|
|
|
|
|
2020-08-03 21:24:06 +02:00
|
|
|
|
2026-11-26 16:23:12 +01:00
|
|
|
@nox.session(name="lint", python=MAIN_PYTHON)
|
|
|
|
|
def lint_code(session: Session) -> None:
|
2026-11-30 13:15:19 +01:00
|
|
|
"""Lint source files with `flake8`, `isort`, `mypy`, and `ruff`.
|
2020-08-04 02:55:41 +02:00
|
|
|
|
|
|
|
|
If no extra arguments are provided, all source files are linted.
|
|
|
|
|
Otherwise, they are interpreted as paths the linters work on recursively.
|
2026-11-30 13:15:19 +01:00
|
|
|
|
|
|
|
|
`mypy` always checks the entire package regardless
|
|
|
|
|
because it needs the whole program to follow types across modules.
|
2020-08-04 02:55:41 +02:00
|
|
|
"""
|
2026-11-25 17:50:06 +01:00
|
|
|
start(session)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-25 16:57:50 +01:00
|
|
|
install_pinned(
|
2020-08-03 22:41:37 +02:00
|
|
|
session,
|
2026-11-30 13:15:19 +01:00
|
|
|
# `mypy` needs the dependencies of `velofy` installed
|
|
|
|
|
# because otherwise it treats missing packages as `Any`
|
|
|
|
|
".",
|
2020-08-03 22:41:37 +02:00
|
|
|
"flake8",
|
|
|
|
|
"flake8-annotations",
|
2026-11-28 02:17:51 +01:00
|
|
|
"flake8-bandit",
|
2020-08-03 22:41:37 +02:00
|
|
|
"flake8-black",
|
2026-11-28 02:28:27 +01:00
|
|
|
"flake8-broken-line",
|
2026-11-28 02:39:58 +01:00
|
|
|
"flake8-bugbear",
|
2026-11-28 02:44:35 +01:00
|
|
|
"flake8-commas",
|
2026-11-28 02:49:56 +01:00
|
|
|
"flake8-comprehensions",
|
2026-11-28 02:51:04 +01:00
|
|
|
"flake8-debugger",
|
2026-11-28 03:04:38 +01:00
|
|
|
"flake8-docstrings",
|
2026-11-25 16:45:56 +01:00
|
|
|
"flake8-pyproject",
|
2020-08-04 00:09:29 +02:00
|
|
|
"flake8-pytest-style",
|
2026-11-28 03:21:46 +01:00
|
|
|
"flake8-quotes",
|
2026-11-28 03:34:40 +01:00
|
|
|
"flake8-sfs",
|
2026-11-28 03:37:25 +01:00
|
|
|
"flake8-todos",
|
2026-11-28 03:47:55 +01:00
|
|
|
"flake8-unused-arguments",
|
2026-11-25 16:57:50 +01:00
|
|
|
"isort",
|
2026-11-30 13:15:19 +01:00
|
|
|
"mypy",
|
2026-11-28 03:54:17 +01:00
|
|
|
"pep8-naming",
|
2026-11-28 04:06:53 +01:00
|
|
|
"pydoclint[flake8]",
|
2026-11-28 04:37:02 +01:00
|
|
|
"ruff",
|
2020-08-03 22:41:37 +02:00
|
|
|
)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-27 12:50:07 +01:00
|
|
|
# Interpret extra arguments as `locations` of source files
|
2020-08-03 22:41:37 +02:00
|
|
|
locations = session.posargs or SRC_LOCATIONS
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-25 16:45:56 +01:00
|
|
|
# `flake8` reads the "[tool.flake8]" section in pyproject.toml
|
|
|
|
|
# only with the `flake8-pyproject` plugin installed
|
2020-08-03 22:41:37 +02:00
|
|
|
session.run("flake8", "--version")
|
2026-11-25 16:44:57 +01:00
|
|
|
session.run("flake8", *locations)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-25 16:42:41 +01:00
|
|
|
session.run("isort", "--version")
|
|
|
|
|
session.run("isort", "--check-only", *locations)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-28 04:37:02 +01:00
|
|
|
session.run("ruff", "--version")
|
|
|
|
|
session.run("ruff", "check", *locations)
|
|
|
|
|
|
2026-11-30 13:15:19 +01:00
|
|
|
session.run("mypy", "--version")
|
|
|
|
|
session.run("mypy", PACKAGE_LOCATION)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2020-08-03 21:24:06 +02:00
|
|
|
|
2026-11-26 16:23:12 +01:00
|
|
|
@nox.session(name="test", python=MAIN_PYTHON)
|
|
|
|
|
def test_code(session: Session) -> None:
|
2026-11-27 09:50:31 +01:00
|
|
|
"""Run the unit and integration tests with `pytest`.
|
2020-08-04 02:55:41 +02:00
|
|
|
|
|
|
|
|
If no extra arguments are provided, the entire test suite
|
2026-11-27 09:50:31 +01:00
|
|
|
is executed and succeeds only with 100% coverage.
|
|
|
|
|
Otherwise, they are forwarded to `pytest`.
|
2020-08-04 02:55:41 +02:00
|
|
|
"""
|
2026-11-25 17:50:06 +01:00
|
|
|
start(session)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-25 16:57:50 +01:00
|
|
|
install_pinned(
|
2020-08-09 17:14:23 +02:00
|
|
|
session,
|
2026-11-27 09:50:31 +01:00
|
|
|
# The tests import `velofy`, so it and its dependencies must be installed
|
|
|
|
|
".", # => A non-editable copy as it would really be installed
|
2020-12-29 14:37:37 +01:00
|
|
|
"factory-boy",
|
2026-11-27 09:50:31 +01:00
|
|
|
"Faker",
|
2020-08-09 17:14:23 +02:00
|
|
|
"packaging",
|
|
|
|
|
"pytest",
|
|
|
|
|
"pytest-cov",
|
|
|
|
|
"pytest-env",
|
2021-01-24 19:18:09 +01:00
|
|
|
"pytest-mock",
|
2020-08-05 00:02:40 +02:00
|
|
|
)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2021-01-11 12:24:15 +01:00
|
|
|
session.run("pytest", "--version")
|
|
|
|
|
|
2026-11-27 09:50:31 +01:00
|
|
|
pytest_default_args = (
|
|
|
|
|
# See "[tool.coverage]" in pyproject.toml for `coverage` settings,
|
|
|
|
|
# for example, branch coverage and report format,
|
|
|
|
|
# and "[tool.coverage.paths]" mapping `coverage` to src/
|
2026-11-26 14:01:03 +01:00
|
|
|
"--cov",
|
|
|
|
|
"--no-cov-on-fail",
|
|
|
|
|
"--cov-fail-under=100",
|
2026-11-27 12:50:07 +01:00
|
|
|
TESTS_LOCATION,
|
2026-11-26 14:01:03 +01:00
|
|
|
)
|
2021-01-11 12:24:15 +01:00
|
|
|
|
2026-11-27 09:50:31 +01:00
|
|
|
session.run("pytest", *(session.posargs or pytest_default_args))
|
2020-08-03 21:24:06 +02:00
|
|
|
|
|
|
|
|
|
2026-11-27 10:45:35 +01:00
|
|
|
@nox.session(name="doctest", python=MAIN_PYTHON)
|
|
|
|
|
def test_docstrings(session: Session) -> None:
|
|
|
|
|
"""Test the code snippets in the docstrings with `xdoctest`.
|
|
|
|
|
|
|
|
|
|
By default, all docstrings in the package are checked.
|
|
|
|
|
|
|
|
|
|
Extra arguments are forwarded to `xdoctest`.
|
|
|
|
|
"""
|
|
|
|
|
start(session)
|
|
|
|
|
|
|
|
|
|
install_pinned(session, ".", "xdoctest[colors]")
|
|
|
|
|
|
|
|
|
|
# As `pytest-env` does for `pytest`,
|
|
|
|
|
# keep `velofy` from connecting to the production database
|
|
|
|
|
session.env["TESTING"] = "true"
|
|
|
|
|
|
|
|
|
|
session.run(
|
|
|
|
|
"python",
|
|
|
|
|
"-m",
|
|
|
|
|
"xdoctest",
|
2026-11-27 12:50:07 +01:00
|
|
|
PACKAGE,
|
2026-11-27 10:45:35 +01:00
|
|
|
*(session.posargs or ("all",)),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-27 12:57:44 +01:00
|
|
|
@nox.session(name="docs", python=MAIN_PYTHON)
|
|
|
|
|
def build_docs(session: Session) -> None:
|
|
|
|
|
"""Build the documentation with `sphinx`.
|
|
|
|
|
|
|
|
|
|
Also, verify that all external links work.
|
|
|
|
|
"""
|
|
|
|
|
start(session)
|
|
|
|
|
|
|
|
|
|
# The `sphinx-autodoc` extension imports `velofy` to read the docstrings
|
|
|
|
|
# => `velofy` and its dependencies must be installed
|
|
|
|
|
install_pinned(session, ".", "sphinx", "sphinx-autodoc-typehints")
|
|
|
|
|
|
|
|
|
|
# As `pytest-env` does for `pytest`,
|
|
|
|
|
# keep `velofy` from connecting to the production database
|
|
|
|
|
session.env["TESTING"] = "true"
|
|
|
|
|
|
|
|
|
|
session.run("sphinx-build", DOCS_LOCATION, DOCS_BUILD)
|
|
|
|
|
|
|
|
|
|
# Verify that all external links return 200 OK
|
|
|
|
|
session.run("sphinx-build", "-b", "linkcheck", DOCS_LOCATION, DOCS_BUILD)
|
|
|
|
|
|
2026-11-28 04:37:02 +01:00
|
|
|
session.log(f"Docs are available at {os.getcwd()}/{DOCS_BUILD}index.html")
|
2026-11-27 12:57:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@nox.session(name="venv", python=MAIN_PYTHON, venv_backend="none")
|
|
|
|
|
def create_develop_environment(session: Session) -> None:
|
|
|
|
|
"""Set up the local develop environment in ".venv".
|
|
|
|
|
|
|
|
|
|
All dependency groups and extras are installed
|
|
|
|
|
with the versions pinned in the uv.lock file.
|
|
|
|
|
|
|
|
|
|
If one extra argument is provided, e.g., `uv run nox -s venv -- other-venv`,
|
|
|
|
|
it is used as the location of the environment instead of ".venv".
|
|
|
|
|
|
|
|
|
|
Then, `uv run` only uses that environment
|
|
|
|
|
with "UV_PROJECT_ENVIRONMENT" set to the same location.
|
|
|
|
|
"""
|
|
|
|
|
location, *further_args = session.posargs or (".venv",)
|
|
|
|
|
|
|
|
|
|
if further_args:
|
|
|
|
|
session.error("Must provide exactly one location")
|
|
|
|
|
|
|
|
|
|
# All dependency groups are installed because of "default-groups = 'all'"
|
|
|
|
|
# in the "[tool.uv]" section in pyproject.toml
|
|
|
|
|
session.run(
|
|
|
|
|
"uv",
|
|
|
|
|
"sync",
|
|
|
|
|
"--all-extras",
|
|
|
|
|
"--locked",
|
|
|
|
|
env={"UV_PROJECT_ENVIRONMENT": location},
|
|
|
|
|
external=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-26 14:08:28 +01:00
|
|
|
@nox.session(name="deps-audit", python=MAIN_PYTHON, reuse_venv=False)
|
|
|
|
|
def audit_pinned_dependencies(session: Session) -> None:
|
|
|
|
|
"""Check dependencies for vulnerabilities with `pip-audit`.
|
|
|
|
|
|
|
|
|
|
The dependencies are all those pinned in the uv.lock file.
|
|
|
|
|
|
|
|
|
|
`pip-audit` uses the Python Packaging Advisory Database,
|
|
|
|
|
see https://github.com/pypa/advisory-database for details.
|
|
|
|
|
"""
|
|
|
|
|
do_not_accept_arguments(session)
|
|
|
|
|
do_not_reuse(session) # Explicit guard in case `reuse_venv=False` above is removed
|
|
|
|
|
|
2026-11-25 17:50:06 +01:00
|
|
|
start(session)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-26 14:08:28 +01:00
|
|
|
# `pip-audit` is not pinned to always check with its latest version
|
|
|
|
|
session.install("pip-audit")
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-26 14:08:28 +01:00
|
|
|
with tempfile.NamedTemporaryFile(suffix=".txt") as requirements_txt:
|
2020-08-04 23:16:15 +02:00
|
|
|
session.run(
|
2026-11-26 14:08:28 +01:00
|
|
|
"uv",
|
2020-08-04 23:16:15 +02:00
|
|
|
"export",
|
2026-11-26 14:08:28 +01:00
|
|
|
"--all-extras",
|
|
|
|
|
"--all-groups",
|
|
|
|
|
"--locked",
|
|
|
|
|
"--no-emit-project",
|
|
|
|
|
"--quiet",
|
|
|
|
|
f"--output-file={requirements_txt.name}",
|
2020-08-04 23:16:15 +02:00
|
|
|
)
|
2026-11-26 14:08:28 +01:00
|
|
|
|
|
|
|
|
session.run("pip-audit", "--version")
|
2020-08-04 23:16:15 +02:00
|
|
|
session.run(
|
2026-11-26 14:08:28 +01:00
|
|
|
"pip-audit",
|
|
|
|
|
"--cache-dir=.cache/pip-audit",
|
|
|
|
|
"--disable-pip",
|
|
|
|
|
"--progress-spinner=off",
|
|
|
|
|
f"--requirement={requirements_txt.name}",
|
|
|
|
|
"--strict",
|
|
|
|
|
# TODO: Remove these ignores once we upgrade to `black` v26
|
|
|
|
|
"--ignore-vuln=PYSEC-2026-2120",
|
|
|
|
|
"--ignore-vuln=PYSEC-2026-2121",
|
|
|
|
|
"--ignore-vuln=CVE-2026-114134",
|
2020-08-04 23:16:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-26 14:50:25 +01:00
|
|
|
@nox.session(name="deps-lock", python=MAIN_PYTHON, venv_backend="none")
|
|
|
|
|
def lock_dependencies(session: Session) -> None:
|
|
|
|
|
"""Lock dependencies in pyproject.toml into uv.lock.
|
|
|
|
|
|
|
|
|
|
Convenience task to run after manual changes of dependencies
|
|
|
|
|
in the pyproject.toml file.
|
|
|
|
|
|
|
|
|
|
Use `git diff uv.lock` to review the newly pinned versions.
|
|
|
|
|
"""
|
|
|
|
|
do_not_accept_arguments(session)
|
|
|
|
|
|
|
|
|
|
# Little hack: Create a flag in the env(ironment)
|
|
|
|
|
# and call `upgrade_dependencies()` directly
|
|
|
|
|
# because `session.notify()` starts sessions with a new
|
|
|
|
|
# `nox.Session` object that does not have the flag set
|
|
|
|
|
session.env["_lock_only"] = "true"
|
|
|
|
|
|
|
|
|
|
upgrade_dependencies(session)
|
|
|
|
|
|
|
|
|
|
|
2026-11-26 14:46:48 +01:00
|
|
|
@nox.session(name="deps-upgrade", python=MAIN_PYTHON, venv_backend="none")
|
|
|
|
|
def upgrade_dependencies(session: Session) -> None:
|
|
|
|
|
"""Pin dependencies in uv.lock to their latest MINOR/PATCH versions.
|
|
|
|
|
|
|
|
|
|
The allowed versions are the ranges in the pyproject.toml file.
|
|
|
|
|
|
|
|
|
|
If no arguments are provided, i.e., `nox -s deps-upgrade`,
|
|
|
|
|
all packages are upgraded!
|
|
|
|
|
|
|
|
|
|
If package names are provided as positional arguments,
|
|
|
|
|
e.g., `nox -s deps-upgrade -- pandas`, only those are upgraded.
|
|
|
|
|
|
|
|
|
|
The upgrades are not yet synced into the local develop environment!
|
|
|
|
|
Use `nox -s venv` for that.
|
|
|
|
|
|
|
|
|
|
Use `git diff uv.lock` to review them.
|
|
|
|
|
|
|
|
|
|
So, the obtained upgrades must still be manually
|
|
|
|
|
`git commit`ed into the repository.
|
|
|
|
|
"""
|
|
|
|
|
if session.env.get("_lock_only"):
|
|
|
|
|
upgrade = []
|
|
|
|
|
elif session.posargs:
|
|
|
|
|
upgrade = [f"--upgrade-package={pkg_name}" for pkg_name in session.posargs]
|
|
|
|
|
else:
|
|
|
|
|
upgrade = ["--upgrade"]
|
|
|
|
|
|
|
|
|
|
session.run("uv", "--version", external=True)
|
|
|
|
|
session.run("uv", "lock", *upgrade, external=True)
|
|
|
|
|
|
|
|
|
|
session.run("git", "diff", "--stat", "uv.lock", external=True)
|
|
|
|
|
|
|
|
|
|
|
2026-11-26 15:51:31 +01:00
|
|
|
@nox.session(name="deps-upgrade-check", python=MAIN_PYTHON)
|
|
|
|
|
def check_dependency_upgrades(session: Session) -> None:
|
|
|
|
|
"""Check the dependencies in uv.lock for upgrades.
|
|
|
|
|
|
|
|
|
|
This task checks if upgrades are available for the PyPI
|
|
|
|
|
packages pinned in the uv.lock file. Also, it obtains
|
|
|
|
|
information about the age of the pinned releases.
|
|
|
|
|
|
|
|
|
|
The output is written to ".cache/dependencies_checked.txt".
|
|
|
|
|
"""
|
|
|
|
|
do_not_accept_arguments(session)
|
2026-11-27 12:50:07 +01:00
|
|
|
|
2026-11-26 15:51:31 +01:00
|
|
|
start(session)
|
|
|
|
|
|
|
|
|
|
# The script only uses the standard library => Nothing to install
|
|
|
|
|
session.run("python", "scripts/get_update_info_for_dependencies.py")
|
|
|
|
|
|
|
|
|
|
|
2026-11-25 16:47:53 +01:00
|
|
|
@nox.session(name="init-project", python=MAIN_PYTHON, venv_backend="none")
|
2026-11-26 16:23:12 +01:00
|
|
|
def initialize_project(session: Session) -> None:
|
2026-11-28 03:09:39 +01:00
|
|
|
"""Install the `pre-commit` hooks and configure `git blame`."""
|
2026-11-27 14:59:25 +01:00
|
|
|
# .pre-commit-config.yaml sets the hook types via "default_install_hook_types"
|
|
|
|
|
session.run("uv", "run", "pre-commit", "install")
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-28 03:09:39 +01:00
|
|
|
# Skip the commits listed in .git-blame-ignore-revs in `git blame`
|
|
|
|
|
session.run(
|
|
|
|
|
"git",
|
|
|
|
|
"config",
|
|
|
|
|
"blame.ignoreRevsFile",
|
|
|
|
|
".git-blame-ignore-revs",
|
|
|
|
|
external=True,
|
|
|
|
|
)
|
|
|
|
|
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-26 16:16:53 +01:00
|
|
|
@nox.session(name="clean-cwd", python=MAIN_PYTHON, venv_backend="none")
|
|
|
|
|
def clean_cwd(session: Session) -> None:
|
|
|
|
|
"""Remove (almost) all glob patterns listed in git's ignore file.
|
|
|
|
|
|
|
|
|
|
Compared to `git clean -X`, never remove local environment variables
|
|
|
|
|
in the ".env" and ".envrc" files, pyenv's ".python-version" file,
|
|
|
|
|
and the local develop environment in ".venv".
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-26 16:16:53 +01:00
|
|
|
An existing ".cache/" folder is always deleted first.
|
2020-08-05 01:45:01 +02:00
|
|
|
"""
|
2026-11-26 16:16:53 +01:00
|
|
|
do_not_accept_arguments(session)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-26 16:16:53 +01:00
|
|
|
do_not_remove = {
|
|
|
|
|
".env",
|
|
|
|
|
".envrc",
|
|
|
|
|
".python-version",
|
|
|
|
|
".venv",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
with pathlib.Path(".gitignore").open() as fp:
|
|
|
|
|
ignored_patterns = {
|
|
|
|
|
pattern for pattern in fp if pattern and not pattern.startswith(("#", "!"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
with pathlib.Path(".gitignore").open() as fp:
|
|
|
|
|
excluded_patterns = {pattern[1:] for pattern in fp if pattern.startswith("!")}
|
2020-08-05 01:45:01 +02:00
|
|
|
|
2026-11-26 16:16:53 +01:00
|
|
|
# Paths are resolved into absolute ones to avoid accidental matches
|
|
|
|
|
excluded_paths = {
|
|
|
|
|
pathlib.Path(path).resolve()
|
|
|
|
|
for path in do_not_remove | set(_expand(*excluded_patterns))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for path in itertools.chain([pathlib.Path(".cache")], _expand(*ignored_patterns)):
|
|
|
|
|
# The `path` must not be a sub-path of an `excluded_path`
|
|
|
|
|
if {path, *path.parents} & excluded_paths:
|
2020-08-05 01:45:01 +02:00
|
|
|
continue
|
|
|
|
|
|
2026-11-26 16:16:53 +01:00
|
|
|
session.run("rm", "-rf", path)
|
2020-08-05 01:45:01 +02:00
|
|
|
|
|
|
|
|
|
2026-11-27 12:57:44 +01:00
|
|
|
def _expand(*patterns: str) -> Generator[pathlib.Path, None, None]:
|
|
|
|
|
"""Expand glob patterns into (resolved) paths.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
*patterns: patterns to be expanded
|
|
|
|
|
|
|
|
|
|
Yields:
|
|
|
|
|
expanded: an expanded path
|
|
|
|
|
"""
|
|
|
|
|
for pattern in patterns:
|
|
|
|
|
if not (pattern := pattern.strip()):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
expanded_paths = pathlib.Path.cwd().glob(pattern)
|
|
|
|
|
|
|
|
|
|
for path in expanded_paths:
|
|
|
|
|
yield path.resolve()
|
|
|
|
|
|
|
|
|
|
|
2026-11-26 14:08:28 +01:00
|
|
|
def do_not_accept_arguments(session: Session, *, raise_error: bool = True) -> None:
|
|
|
|
|
"""Do not accept any `session.posargs`."""
|
|
|
|
|
if session.posargs:
|
|
|
|
|
msg = 'be run without any positional arguments behind the "--"'
|
2026-11-27 12:50:07 +01:00
|
|
|
|
2026-11-26 14:08:28 +01:00
|
|
|
if raise_error:
|
|
|
|
|
session.error(f"Must {msg}")
|
|
|
|
|
else:
|
|
|
|
|
session.warn(f"Should {msg}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def do_not_reuse(session: Session, *, raise_error: bool = True) -> None:
|
2026-11-27 12:50:07 +01:00
|
|
|
"""Do not reuse a session with the "-r" or "-R" flags."""
|
2026-11-26 14:08:28 +01:00
|
|
|
if session._runner.venv._reused:
|
|
|
|
|
msg = 'be run without the "-r" and "-R" flags'
|
2026-11-27 12:50:07 +01:00
|
|
|
|
2026-11-26 14:08:28 +01:00
|
|
|
if raise_error:
|
|
|
|
|
session.error(f"Must {msg}")
|
|
|
|
|
else:
|
|
|
|
|
session.warn(f"Should {msg}")
|
|
|
|
|
|
|
|
|
|
|
2026-11-25 17:50:06 +01:00
|
|
|
def start(session: Session) -> None:
|
2020-08-03 21:24:06 +02:00
|
|
|
"""Show generic info about a session."""
|
|
|
|
|
if session.posargs:
|
2026-11-25 17:50:06 +01:00
|
|
|
session.debug(f"Received extra arguments: {session.posargs}")
|
2020-08-03 21:24:06 +02:00
|
|
|
|
2026-11-25 17:50:06 +01:00
|
|
|
session.debug("Some generic information about the environment")
|
2026-11-27 12:50:07 +01:00
|
|
|
|
2020-08-03 21:24:06 +02:00
|
|
|
session.run("python", "--version")
|
2026-11-25 17:50:06 +01:00
|
|
|
session.run("python", "-c", "import sys; print(sys.executable)")
|
|
|
|
|
session.run("python", "-c", "import sys; print(sys.path)")
|
|
|
|
|
session.run("python", "-c", "import os; print(os.getcwd())")
|
|
|
|
|
session.run("python", "-c", 'import os; print(os.environ["PATH"])')
|
|
|
|
|
|
|
|
|
|
session.env["BLACK_CACHE_DIR"] = ".cache/black"
|
|
|
|
|
|
|
|
|
|
session.run(
|
|
|
|
|
"mkdir",
|
|
|
|
|
"-p",
|
|
|
|
|
*(
|
|
|
|
|
f".cache/{dir_}"
|
|
|
|
|
for dir_ in (
|
|
|
|
|
"black",
|
|
|
|
|
"coverage",
|
|
|
|
|
"mypy",
|
|
|
|
|
"nox",
|
|
|
|
|
"pytest",
|
|
|
|
|
"tmp",
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
external=True,
|
|
|
|
|
log=False,
|
|
|
|
|
)
|
2020-08-03 21:24:06 +02:00
|
|
|
|
|
|
|
|
|
2026-11-25 16:57:50 +01:00
|
|
|
def install_pinned(
|
|
|
|
|
session: Session,
|
|
|
|
|
*packages_or_pip_args: str,
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Install packages respecting the uv.lock file.
|
2020-08-03 21:24:06 +02:00
|
|
|
|
2026-11-25 16:57:50 +01:00
|
|
|
Wraps `nox.Session.install()` such that it installs packages
|
|
|
|
|
respecting the versions pinned in the uv.lock file,
|
|
|
|
|
exported into a temporary constraints file.
|
2026-11-27 12:50:07 +01:00
|
|
|
This makes `nox` sessions more deterministic.
|
2026-11-25 16:57:50 +01:00
|
|
|
|
|
|
|
|
The developed package itself (i.e., ".") is only rebuilt
|
|
|
|
|
if a file listed in the "cache-keys" in the "[tool.uv]" section
|
|
|
|
|
in pyproject.toml changes; otherwise, `uv` re-uses a cached build.
|
|
|
|
|
"""
|
2026-11-25 18:01:30 +01:00
|
|
|
if skip_install(session):
|
2020-08-03 21:24:06 +02:00
|
|
|
return
|
|
|
|
|
|
2026-11-25 16:57:50 +01:00
|
|
|
with tempfile.NamedTemporaryFile(suffix=".txt") as constraints_txt:
|
2020-08-03 21:24:06 +02:00
|
|
|
session.run(
|
2026-11-25 16:57:50 +01:00
|
|
|
"uv",
|
2020-08-03 21:24:06 +02:00
|
|
|
"export",
|
2026-11-25 16:57:50 +01:00
|
|
|
"--all-extras",
|
|
|
|
|
"--all-groups",
|
|
|
|
|
"--locked",
|
|
|
|
|
"--no-emit-project",
|
|
|
|
|
"--no-hashes",
|
|
|
|
|
"--quiet",
|
|
|
|
|
f"--output-file={constraints_txt.name}",
|
2020-08-03 21:24:06 +02:00
|
|
|
)
|
|
|
|
|
session.install(
|
2026-11-25 16:57:50 +01:00
|
|
|
f"--constraint={constraints_txt.name}",
|
2026-11-25 10:24:07 +01:00
|
|
|
*packages_or_pip_args,
|
|
|
|
|
**kwargs,
|
2020-08-03 21:24:06 +02:00
|
|
|
)
|
2026-11-25 16:47:53 +01:00
|
|
|
|
|
|
|
|
|
2026-11-25 18:01:30 +01:00
|
|
|
def skip_install(session: Session) -> bool:
|
|
|
|
|
"""Installations may be skipped if a virtual environment can be reused.
|
|
|
|
|
|
|
|
|
|
Skipping package installations must be requested with the "-R" flag.
|
|
|
|
|
"""
|
|
|
|
|
# Same logic to skip package installation as in core `nox`
|
2026-11-27 12:50:07 +01:00
|
|
|
# See: https://github.com/wntrblm/nox/blob/2026.10.20/nox/sessions.py#L905
|
2026-11-25 18:01:30 +01:00
|
|
|
venv = session._runner.venv
|
|
|
|
|
if session._runner.global_config.no_install and venv._reused:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2026-11-25 16:47:53 +01:00
|
|
|
if MAIN_PYTHON not in SUPPORTED_PYTHONS:
|
|
|
|
|
msg = f"MAIN_PYTHON version, v{MAIN_PYTHON}, is not in SUPPORTED_PYTHONS"
|
|
|
|
|
raise RuntimeError(msg)
|