feat(ci): pin hook revisions by SHA and install dev tools from a hashed lock
Refs #113. - .pre-commit-config.yaml: every hook repo is pinned by commit SHA with the release in a `# frozen:` comment, so a moved tag cannot change the code a hook environment is built from. - requirements-dev.lock: hashed lock of requirements-dev.txt, installed with `pip install --require-hashes` by scripts/dev-setup.sh, the ci image and the fallback install of the Lint and Build workflows. - scripts/lock-requirements.sh regenerates the lock; scripts/check_requirements_lock.py (offline, a pre-commit hook) fails when it drifts from the direct pins. - check_bot_pr_diff.py accepts a `rev:` line with a `# frozen:` comment and nothing else. - ci image 0.2.16 -> 0.2.17 (image inputs changed).
This commit is contained in:
1 parent
212dd7aea8
commit
1184938f1e
21 files changed
+1648
-36
No files matched your search
@@ -65,9 +65,9 @@ jobs:
|
||||
if sha256sum --quiet -c /opt/ci-pins.sha256; then
|
||||
echo "The pin files match the ci image; using the baked tools."
|
||||
else
|
||||
echo "::warning::requirements-dev.txt, requirements.yml or .pre-commit-config.yaml differ from the ci image;" \
|
||||
echo "::warning::requirements-dev.txt, requirements-dev.lock, requirements.yml or .pre-commit-config.yaml differ from the ci image;" \
|
||||
"installing the pinned tools for this run."
|
||||
/opt/venv/bin/pip install --quiet -r requirements-dev.txt
|
||||
/opt/venv/bin/pip install --quiet --require-hashes -r requirements-dev.lock
|
||||
fi
|
||||
|
||||
# ADR 0004, Release process: the tag is annotated and equals v + the galaxy.yml version.
|
||||
|
||||
@@ -40,6 +40,7 @@ on: # yamllint disable-line rule:truthy
|
||||
- 'images/**'
|
||||
# The ci image bakes these files' contents in (images/ci/Dockerfile).
|
||||
- 'requirements-dev.txt'
|
||||
- 'requirements-dev.lock'
|
||||
- 'requirements.yml'
|
||||
- '.pre-commit-config.yaml'
|
||||
- '.gitea/workflows/image.yml'
|
||||
@@ -87,7 +88,7 @@ jobs:
|
||||
dockerfile: Dockerfile
|
||||
job_image: ubuntu:26.04@sha256:6a06111c3b0a8600feadb270aec771dc239bf407b25625f93f44ab00e11f4d3c
|
||||
- name: ci
|
||||
version: 0.2.16
|
||||
version: 0.2.17
|
||||
subpath: ""
|
||||
dockerfile: images/ci/Dockerfile
|
||||
job_image: ubuntu:26.04@sha256:6a06111c3b0a8600feadb270aec771dc239bf407b25625f93f44ab00e11f4d3c
|
||||
|
||||
@@ -64,9 +64,9 @@ jobs:
|
||||
if sha256sum --quiet -c /opt/ci-pins.sha256; then
|
||||
echo "The pin files match the ci image; using the baked tools."
|
||||
else
|
||||
echo "::warning::requirements-dev.txt, requirements.yml or .pre-commit-config.yaml differ from the ci image;" \
|
||||
echo "::warning::requirements-dev.txt, requirements-dev.lock, requirements.yml or .pre-commit-config.yaml differ from the ci image;" \
|
||||
"installing the pinned tools and collections for this run."
|
||||
/opt/venv/bin/pip install --quiet -r requirements-dev.txt
|
||||
/opt/venv/bin/pip install --quiet --require-hashes -r requirements-dev.lock
|
||||
# --force: the baked copy of a changed pin is replaced, not kept as "already installed".
|
||||
/opt/venv/bin/ansible-galaxy collection install --force -r requirements.yml -p "$ANSIBLE_COLLECTIONS_PATH"
|
||||
fi
|
||||
|
||||
+22
-7
@@ -1,20 +1,25 @@
|
||||
# Hook repositories are pinned by commit SHA, with the release in a `# frozen:` comment: a
|
||||
# tag can be moved, a commit cannot, so a moved tag shows up as a failed fetch and not as
|
||||
# new code in the hook environment (supply chain, ci image and contributors alike).
|
||||
# Renovate keeps both parts in step (pinDigests rule in renovate.json). To pin by hand:
|
||||
# `git ls-remote <repo> refs/tags/<tag>^{}` (an annotated tag needs the peeled `^{}` line).
|
||||
default_install_hook_types: [pre-commit, commit-msg]
|
||||
|
||||
repos:
|
||||
- repo: https://github.com/compilerla/conventional-pre-commit
|
||||
rev: v4.4.0
|
||||
rev: 91ab4bf57e58b32adf1a122681f6ebe164d081c8 # frozen: v4.4.0
|
||||
hooks:
|
||||
- id: conventional-pre-commit
|
||||
stages: [commit-msg]
|
||||
|
||||
- repo: https://github.com/gitleaks/gitleaks
|
||||
rev: v8.30.1
|
||||
rev: 83d9cd684c87d95d656c1458ef04895a7f1cbd8e # frozen: v8.30.1
|
||||
hooks:
|
||||
- id: gitleaks
|
||||
stages: ['pre-commit']
|
||||
|
||||
- repo: https://github.com/adrienverge/yamllint
|
||||
rev: v1.38.0
|
||||
rev: cba56bcde1fdd01c1deb3f945e69764c291a6530 # frozen: v1.38.0
|
||||
hooks:
|
||||
- id: yamllint
|
||||
args: [--strict]
|
||||
@@ -23,7 +28,7 @@ repos:
|
||||
# Python and shell scripts under scripts/ and tests/scripts/ (#112). ruff.toml holds the
|
||||
# configuration. No --fix: a finding fails the hook instead of rewriting files in CI.
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: v0.17.5
|
||||
rev: ef6cf06c079dd6b299dfc720b0f14400c9d168e1 # frozen: v0.17.5
|
||||
hooks:
|
||||
- id: ruff-check
|
||||
stages: ['pre-commit']
|
||||
@@ -33,13 +38,13 @@ repos:
|
||||
# shellcheck-py ships the shellcheck binary as a wheel, so no system package is needed.
|
||||
# Default severity, no .shellcheckrc. actionlint (below) covers the run: blocks of workflows.
|
||||
- repo: https://github.com/shellcheck-py/shellcheck-py
|
||||
rev: v0.11.0.1
|
||||
rev: 745eface02aef23e168a8afb6b5737818efbea95 # frozen: v0.11.0.1
|
||||
hooks:
|
||||
- id: shellcheck
|
||||
stages: ['pre-commit']
|
||||
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v6.0.0
|
||||
rev: 3e8a8703264a2f4a69428a0aa4dcb512790b2c8c # frozen: v6.0.0
|
||||
hooks:
|
||||
- id: check-toml
|
||||
stages: ['pre-commit']
|
||||
@@ -50,7 +55,7 @@ repos:
|
||||
# github.* contexts (Gitea aliases them), because actionlint does not know gitea.*.
|
||||
# actionlint runs shellcheck over run: blocks when shellcheck is on PATH.
|
||||
- repo: https://github.com/rhysd/actionlint
|
||||
rev: v1.7.12
|
||||
rev: 914e7df21a07ef503a81201c76d2b11c789d3fca # frozen: v1.7.12
|
||||
hooks:
|
||||
- id: actionlint
|
||||
args: [-config-file, .gitea/actionlint.yaml]
|
||||
@@ -74,6 +79,16 @@ repos:
|
||||
always_run: true
|
||||
stages: ['pre-commit']
|
||||
|
||||
# The hashed lock must cover every direct pin at the same version (#113); offline.
|
||||
- id: requirements-lock
|
||||
name: requirements-dev.lock matches requirements-dev.txt
|
||||
description: scripts/check_requirements_lock.py, regenerate with scripts/lock-requirements.sh
|
||||
entry: python3 scripts/check_requirements_lock.py
|
||||
language: system
|
||||
files: ^(requirements-dev\.(txt|lock)|scripts/check_requirements_lock\.py)$
|
||||
stages: ['pre-commit']
|
||||
pass_filenames: false
|
||||
|
||||
- id: antsibull-changelog-lint
|
||||
name: Changelog fragments lint
|
||||
description: antsibull-changelog lint over changelogs/
|
||||
|
||||
@@ -56,7 +56,7 @@ Single-context: `GLOSSARY.md` and `docs/adr/` at the root, both created lazily.
|
||||
- Runner labels are `kamino-docker` and `kamino-molecule` only (`.gitea/actionlint.yaml`). Use
|
||||
the `github.*` contexts, which Gitea aliases; actionlint does not know `gitea.*`.
|
||||
- Lint and Build run in the `ci` image. After the checkout they compare the pin files with the
|
||||
checksums baked into the image: a PR that changes `requirements-dev.txt`, `requirements.yml` or
|
||||
checksums baked into the image: a PR that changes `requirements-dev.txt`, `requirements-dev.lock`, `requirements.yml` or
|
||||
`.pre-commit-config.yaml` takes the slower path that installs the pinned tools and collections.
|
||||
- Molecule runs on every same-repo pull request, Renovate's included (a new image is tested by its own pin
|
||||
pull request), with no workflow path filter, so that it can be a required check. Fork pull requests are skipped.
|
||||
@@ -119,7 +119,7 @@ maintainer's production environment. Never reuse another Renovate credential her
|
||||
A new Ubuntu release is a deliberate edit, never a bump. The `job_image` of each matrix entry in `image.yml`
|
||||
never automerges: that job holds the registry push credential. kaniko is pinned by layer digests in
|
||||
`image.yml` and updated by hand.
|
||||
- **Two-PR loop for an image input** (`requirements-dev.txt`, `requirements.yml`, `.pre-commit-config.yaml`, `images/**`): PR 1
|
||||
- **Two-PR loop for an image input** (`requirements-dev.txt`, `requirements-dev.lock`, `requirements.yml`, `.pre-commit-config.yaml`, `images/**`): PR 1
|
||||
carries the input change plus the PATCH bump of the image `version:` in `image.yml`
|
||||
(`scripts/bump_image_versions.py [<base-ref>]`; `check_image_versions.py bump` then passes; Renovate runs it
|
||||
as a post-upgrade task). The merge builds the image on `main`. PR 2 moves the consumers' pins to the new
|
||||
|
||||
+8
-2
@@ -15,7 +15,7 @@
|
||||
- Third-party material needs an SPDX header, a `REUSE.toml` annotation (it
|
||||
overrides file headers) and an entry in `NOTICE`.
|
||||
- Set up the tooling once per clone with `scripts/dev-setup.sh`: it creates a virtualenv with
|
||||
the pinned tools (`requirements-dev.txt`), installs the pinned collections the roles call
|
||||
the pinned tools (`requirements-dev.lock`, the hashed lock of `requirements-dev.txt`), installs the pinned collections the roles call
|
||||
(`requirements.yml`, into `.ansible/collections`, so local lint resolves the versions CI does)
|
||||
and installs git hooks that find the tools (the `ansible-lint` hook, `scripts/ansible-lint.sh`, uses those
|
||||
collections, also from a bare `pre-commit run`). Then
|
||||
@@ -28,7 +28,13 @@
|
||||
|
||||
## Dependency and image updates
|
||||
|
||||
- `requirements-dev.txt`, `requirements.yml`, `.pre-commit-config.yaml` and `images/**` are inputs of the CI and
|
||||
- A pin in `requirements-dev.txt` is changed together with its lock: run `scripts/lock-requirements.sh` (needs
|
||||
PyPI and Python 3.13) and commit `requirements-dev.lock` in the same pull request. Installs use
|
||||
`pip install --require-hashes`, and the `requirements-lock` hook (`scripts/check_requirements_lock.py`,
|
||||
offline) fails when the two files disagree. Hook repositories in `.pre-commit-config.yaml` are pinned
|
||||
by commit SHA with the release in a `# frozen: vX.Y.Z` comment; find the SHA with
|
||||
`git ls-remote <repo> refs/tags/<tag>^{}`.
|
||||
- `requirements-dev.txt`, `requirements-dev.lock`, `requirements.yml`, `.pre-commit-config.yaml` and `images/**` are inputs of the CI and
|
||||
molecule images. A pull request that changes one needs a higher `version:` for that image in
|
||||
`.gitea/workflows/image.yml` (`python3 scripts/bump_image_versions.py` makes the edit; the
|
||||
`Check image versions were bumped` step enforces it). After the merge, `image.yml` builds the
|
||||
|
||||
+6
-6
@@ -7,7 +7,7 @@ workflows used to install on each run is baked in, so a job only checks out the
|
||||
|
||||
Ubuntu 26.04 with:
|
||||
|
||||
- **Python tools** in `/opt/venv` (on `PATH`), from `requirements-dev.txt`: `ansible-core`,
|
||||
- **Python tools** in `/opt/venv` (on `PATH`), from `requirements-dev.lock` (the hashed lock of `requirements-dev.txt`, installed with `pip install --require-hashes`): `ansible-core`,
|
||||
`ansible-lint`, `pre-commit`, `antsibull-changelog`, `galaxy-importer`, `reuse`.
|
||||
- **The collections the roles call** in `/opt/collections` (`ANSIBLE_COLLECTIONS_PATH` is set to it), from
|
||||
`requirements.yml`: `community.general` and `ansible.posix` at the exact versions CI tests against, so
|
||||
@@ -22,7 +22,7 @@ Ubuntu 26.04 with:
|
||||
`python3`, so sanity runs its compile test for those interpreters instead of skipping it.
|
||||
- `git`, `shellcheck` (actionlint runs it over `run:` blocks), `python3`, `python3-venv` and `ca-certificates`.
|
||||
The image runs as root and sets no working directory.
|
||||
- `/opt/ci-pins.sha256`: the checksums of the three pin files this image was built from.
|
||||
- `/opt/ci-pins.sha256`: the checksums of the four pin files this image was built from.
|
||||
|
||||
It has no Node, so JavaScript actions (`actions/checkout`) cannot run in it: clone with `git`.
|
||||
|
||||
@@ -46,7 +46,7 @@ jobs:
|
||||
- name: Check the baked tooling against the pins # run from the checkout root
|
||||
run: |
|
||||
if ! sha256sum --quiet -c /opt/ci-pins.sha256; then
|
||||
/opt/venv/bin/pip install --quiet -r requirements-dev.txt # a pin changed: install the pinned tools
|
||||
/opt/venv/bin/pip install --quiet --require-hashes -r requirements-dev.lock # a pin changed: install the pinned tools
|
||||
/opt/venv/bin/ansible-galaxy collection install --force -r requirements.yml -p "$ANSIBLE_COLLECTIONS_PATH"
|
||||
fi
|
||||
- name: Run pre-commit on all files
|
||||
@@ -55,8 +55,8 @@ jobs:
|
||||
pre-commit run --all-files --show-diff-on-failure
|
||||
```
|
||||
|
||||
`/opt/ci-pins.sha256` lists the three pin files by relative name, so `sha256sum -c` runs in the checkout root. When
|
||||
the checkout's `requirements-dev.txt`, `requirements.yml` and `.pre-commit-config.yaml` match, the baked tools and
|
||||
`/opt/ci-pins.sha256` lists the four pin files by relative name, so `sha256sum -c` runs in the checkout root. When
|
||||
the checkout's `requirements-dev.txt`, `requirements-dev.lock`, `requirements.yml` and `.pre-commit-config.yaml` match, the baked tools and
|
||||
collections are used as they are. When a pull request changes a pin the check fails and the job installs the pinned
|
||||
tools and collections itself for that run
|
||||
(slower, same result), until the image is rebuilt from `main`. `lint.yml` and `build.yml` are the real consumers.
|
||||
@@ -72,7 +72,7 @@ tools and collections itself for that run
|
||||
## How it is built and kept up to date
|
||||
|
||||
`.gitea/workflows/image.yml` builds it with kaniko (repository root as build context, because the pin
|
||||
files are copied in). The workflow runs on pushes to `main` that touch `images/**`, `requirements-dev.txt`, `requirements.yml`,
|
||||
files are copied in). The workflow runs on pushes to `main` that touch `images/**`, `requirements-dev.txt`, `requirements-dev.lock`, `requirements.yml`,
|
||||
`.pre-commit-config.yaml` or the workflow itself, never on a pull request, and builds an image only when its
|
||||
`version:` in the workflow's matrix is not yet published. A change to the image's inputs therefore needs a new
|
||||
`version:` (Lint enforces it, and Renovate's bump step does it for dependency updates), and the merge builds
|
||||
|
||||
@@ -66,7 +66,7 @@ systemd, packages, the `dbus` and `ssh` units, the Python bindings, `NET_ADMIN`)
|
||||
## How it is built and kept up to date
|
||||
|
||||
`.gitea/workflows/image.yml` builds it with kaniko. The workflow runs on pushes to `main` that touch `images/**`,
|
||||
`requirements-dev.txt`, `requirements.yml`, `.pre-commit-config.yaml` or the workflow itself, never on a pull request, and builds an
|
||||
`requirements-dev.txt`, `requirements-dev.lock`, `requirements.yml`, `.pre-commit-config.yaml` or the workflow itself, never on a pull request, and builds an
|
||||
image only when its `version:` in the workflow's matrix is not yet published. A change under
|
||||
`images/molecule-ubuntu2404/` therefore needs a new `version:` (Lint enforces it) and the merge builds the image. The
|
||||
Ubuntu release is fixed by the name: a new Ubuntu release is a new image (`molecule-ubuntuNNNN`), and the roles
|
||||
|
||||
@@ -65,7 +65,7 @@ systemd, packages, the `dbus` and `ssh` units, the Python bindings, `NET_ADMIN`)
|
||||
## How it is built and kept up to date
|
||||
|
||||
`.gitea/workflows/image.yml` builds it with kaniko. The workflow runs on pushes to `main` that touch `images/**`,
|
||||
`requirements-dev.txt`, `requirements.yml`, `.pre-commit-config.yaml` or the workflow itself, never on a pull request, and builds an
|
||||
`requirements-dev.txt`, `requirements-dev.lock`, `requirements.yml`, `.pre-commit-config.yaml` or the workflow itself, never on a pull request, and builds an
|
||||
image only when its `version:` in the workflow's matrix is not yet published. A change under
|
||||
`images/molecule-ubuntu2604/` therefore needs a new `version:` (Lint enforces it) and the merge builds the image. The
|
||||
Ubuntu release is fixed by the name: a new Ubuntu release is a new image (`molecule-ubuntuNNNN`), and the roles
|
||||
|
||||
@@ -52,6 +52,7 @@ build_ignore:
|
||||
- renovate.json
|
||||
- ruff.toml
|
||||
- requirements-dev.txt
|
||||
- requirements-dev.lock
|
||||
- requirements.yml
|
||||
- gitleaks-report.json
|
||||
- importer_result.json
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
# What is baked in, and from where:
|
||||
# - OS packages: git, shellcheck (actionlint runs it over run: blocks), the
|
||||
# distribution's python3 and ca-certificates.
|
||||
# - /opt/venv: requirements-dev.txt (ansible-core, ansible-lint, pre-commit,
|
||||
# - /opt/venv: requirements-dev.lock, the hashed lock of requirements-dev.txt (ansible-core, ansible-lint, pre-commit,
|
||||
# antsibull-changelog, galaxy-importer, reuse).
|
||||
# - /opt/collections: the collections of requirements.yml (the versions the roles are
|
||||
# tested against), found through ANSIBLE_COLLECTIONS_PATH, so `ansible-lint --offline`
|
||||
@@ -24,7 +24,7 @@
|
||||
# - Python 3.11 and 3.13 as python3.11 / python3.13 on PATH, so that
|
||||
# `ansible-test sanity` runs its compile test for them instead of skipping it.
|
||||
# They come from python-build-standalone through uv, pinned by the uv version.
|
||||
# - /opt/ci-pins.sha256: the checksums of requirements-dev.txt, requirements.yml and
|
||||
# - /opt/ci-pins.sha256: the checksums of requirements-dev.txt, requirements-dev.lock, requirements.yml and
|
||||
# .pre-commit-config.yaml this image was built from. A job runs
|
||||
# `sha256sum -c /opt/ci-pins.sha256` in its checkout; when a pull request changes a
|
||||
# pin the check fails and the job installs the tools as before, until the image
|
||||
@@ -56,10 +56,10 @@ ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
||||
UV_PYTHON_BIN_DIR=/opt/python-bin
|
||||
|
||||
WORKDIR /src
|
||||
COPY requirements-dev.txt requirements.yml .pre-commit-config.yaml ./
|
||||
COPY requirements-dev.txt requirements-dev.lock requirements.yml .pre-commit-config.yaml ./
|
||||
|
||||
RUN python3 -m venv /opt/venv \
|
||||
&& /opt/venv/bin/pip install -r requirements-dev.txt
|
||||
&& /opt/venv/bin/pip install --require-hashes -r requirements-dev.lock
|
||||
|
||||
# The collections of requirements.yml. ANSIBLE_COLLECTIONS_PATH is set to the target for the
|
||||
# install only: a collection found anywhere else on the path would be skipped as "already
|
||||
@@ -151,14 +151,14 @@ COPY --from=build /opt/pre-commit /opt/pre-commit
|
||||
COPY --from=build /root/.ansible/test /root/.ansible/test
|
||||
|
||||
# The target interpreters ansible-test looks for on PATH, and the pin record.
|
||||
COPY --from=build /src/requirements-dev.txt /src/requirements.yml /src/.pre-commit-config.yaml /opt/ci-pins/
|
||||
COPY --from=build /src/requirements-dev.txt /src/requirements-dev.lock /src/requirements.yml /src/.pre-commit-config.yaml /opt/ci-pins/
|
||||
RUN set -eu; \
|
||||
for v in 3.11 3.13; do \
|
||||
ln -s "$(echo /opt/python/cpython-${v}.*/bin/python${v})" "/usr/local/bin/python${v}"; \
|
||||
"python${v}" --version; \
|
||||
done; \
|
||||
cd /opt/ci-pins; \
|
||||
sha256sum requirements-dev.txt requirements.yml .pre-commit-config.yaml > /opt/ci-pins.sha256; \
|
||||
sha256sum requirements-dev.txt requirements-dev.lock requirements.yml .pre-commit-config.yaml > /opt/ci-pins.sha256; \
|
||||
ansible --version | head -n1; \
|
||||
ansible-galaxy collection list; \
|
||||
pre-commit --version; \
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,5 +1,8 @@
|
||||
# Pinned tooling for lint and build, used by CI (.gitea/workflows) and locally:
|
||||
# python3 -m venv .venv && . .venv/bin/activate && pip install -r requirements-dev.txt
|
||||
# python3 -m venv .venv && . .venv/bin/activate && pip install --require-hashes -r requirements-dev.lock
|
||||
# This file holds the direct pins; requirements-dev.lock is their hashed lock (every transitive
|
||||
# package too, with the sha256 of each release file) and is what gets installed. After editing
|
||||
# a pin here, run scripts/lock-requirements.sh and commit both files (#113).
|
||||
# ansible-core and ansible-lint started equal to the environment repo's runner image
|
||||
# (hosts/group_vars/all/ci_python_tools.yml there) and are no longer kept equal automatically.
|
||||
# Renovate (renovate.json) bumps these pins; a bump rebuilds the ci image (images/ci) and
|
||||
|
||||
@@ -9,7 +9,7 @@ When <head-ref> starts with `renovate/`, the diff of <base-ref>...HEAD may only
|
||||
* requirements-dev.txt: pinned `name==version` lines, the same packages before and after;
|
||||
* requirements.yml and extensions/molecule/requirements.yml: `version:` lines (the collection
|
||||
names, and every other line, stay);
|
||||
* .pre-commit-config.yaml: `rev:` lines;
|
||||
* .pre-commit-config.yaml: `rev:` lines (a tag, or a commit SHA with its `# frozen: <tag>` comment);
|
||||
* images/<name>/Dockerfile: the tag and digest of FROM lines (the image name stays), and
|
||||
the `uv==<version>` token of a line that holds one;
|
||||
* .gitea/workflows/image.yml: the matrix `version:` lines, and the tag and digest of the
|
||||
@@ -49,7 +49,9 @@ PREFIX = "renovate/"
|
||||
TAG = r"[A-Za-z0-9._-]+"
|
||||
PIN = re.compile(rf"(git\.rkdr\.net/mortis/[a-z0-9._-]+):{TAG}@sha256:[0-9a-f]{{64}}")
|
||||
REQ_LINE = re.compile(r"^([A-Za-z0-9][A-Za-z0-9._-]*)==[0-9][0-9A-Za-z.+!_-]*$")
|
||||
REV_LINE = re.compile(r"^\s*rev:\s*\S+\s*$")
|
||||
# A hook `rev:` is a tag or a commit SHA; a SHA carries its release in a `# frozen: <tag>` comment.
|
||||
# Nothing else may trail it, so the comment cannot hide a second instruction.
|
||||
REV_LINE = re.compile(r"^\s*rev:\s*\S+(?:\s+#\s*frozen:\s*\S+)?\s*$")
|
||||
VERSION_LINE = re.compile(
|
||||
r"""^\s*(?:-\s*)?version:\s*["']?(?P<v>[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?)["']?\s*(?:#.*)?$"""
|
||||
)
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
# SPDX-FileCopyrightText: Defame1297 and contributors
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Check that requirements-dev.lock is the hashed lock of requirements-dev.txt, offline.
|
||||
|
||||
python3 scripts/check_requirements_lock.py [<requirements-dev.txt> <requirements-dev.lock>]
|
||||
|
||||
requirements-dev.txt holds the direct pins (`name==version`, bumped by Renovate); the lock lists
|
||||
every package, direct and transitive, with the sha256 of each release file, and is what `pip
|
||||
install --require-hashes` installs (scripts/dev-setup.sh, images/ci, the fallback install of the
|
||||
Lint and Build workflows). The lock is made by scripts/lock-requirements.sh, which needs the
|
||||
network; this check needs none. It fails when
|
||||
|
||||
* a direct pin of the txt file is missing from the lock, or is there at another version;
|
||||
* a lock entry has no `--hash=sha256:` line, or its version is not an exact `==` pin;
|
||||
* the lock carries a line that is neither an entry, a hash, nor a comment (a URL, an
|
||||
`-e` editable, an index option: none of these can be verified by a hash).
|
||||
|
||||
It does not prove that the hashes are genuine (only the lock script, against PyPI, does), it
|
||||
proves the file cannot drift from the pins or smuggle in an unhashed install.
|
||||
|
||||
Exit codes: 0 ok, 1 a finding (one `::error::` line each), 2 bad usage.
|
||||
"""
|
||||
|
||||
import re
|
||||
import sys
|
||||
|
||||
DEFAULT_TXT = "requirements-dev.txt"
|
||||
DEFAULT_LOCK = "requirements-dev.lock"
|
||||
NAME = r"[A-Za-z0-9][A-Za-z0-9._-]*"
|
||||
PIN = re.compile(
|
||||
rf"^(?P<name>{NAME})==(?P<version>[0-9][0-9A-Za-z.+!_-]*)\s*(?:;[^#\\]*)?(?:\\)?\s*$"
|
||||
)
|
||||
HASH = re.compile(r"^\s+--hash=sha256:[0-9a-f]{64}(?:\s+\\)?\s*$")
|
||||
USAGE = "usage: check_requirements_lock.py [<requirements-dev.txt> <requirements-dev.lock>]\n"
|
||||
|
||||
|
||||
def canonical(name):
|
||||
"""PEP 503 name, so `Ruamel.Yaml` and `ruamel-yaml` are one package."""
|
||||
return re.sub(r"[-_.]+", "-", name).lower()
|
||||
|
||||
|
||||
def read_pins(text):
|
||||
"""{canonical name: version} of the `name==version` lines of requirements-dev.txt."""
|
||||
pins = {}
|
||||
for line in text.splitlines():
|
||||
stripped = line.split("#", 1)[0].strip()
|
||||
if not stripped:
|
||||
continue
|
||||
m = PIN.match(stripped)
|
||||
if m:
|
||||
pins[canonical(m["name"])] = m["version"]
|
||||
return pins
|
||||
|
||||
|
||||
def read_lock(text):
|
||||
"""({canonical name: version}, [finding]) of the lock file."""
|
||||
entries, findings = {}, []
|
||||
current = None
|
||||
hashes = {}
|
||||
for number, line in enumerate(text.splitlines(), 1):
|
||||
if not line.strip() or line.lstrip().startswith("#"):
|
||||
continue
|
||||
if HASH.match(line):
|
||||
if current is None:
|
||||
findings.append(f"line {number}: a hash with no package before it")
|
||||
else:
|
||||
hashes[current] += 1
|
||||
continue
|
||||
m = PIN.match(line)
|
||||
if m and not line[0].isspace():
|
||||
current = canonical(m["name"])
|
||||
if current in entries:
|
||||
findings.append(f"line {number}: {current} is listed twice")
|
||||
entries[current] = m["version"]
|
||||
hashes.setdefault(current, 0)
|
||||
continue
|
||||
findings.append(
|
||||
f"line {number}: not a `name==version` entry, a sha256 hash or a comment: {line.strip()[:60]}"
|
||||
)
|
||||
for name, count in hashes.items():
|
||||
if count == 0:
|
||||
findings.append(f"{name}=={entries[name]} has no --hash=sha256 line")
|
||||
return entries, findings
|
||||
|
||||
|
||||
def check(txt, lock):
|
||||
pins = read_pins(txt)
|
||||
entries, findings = read_lock(lock)
|
||||
for name, version in sorted(pins.items()):
|
||||
if name not in entries:
|
||||
findings.append(
|
||||
f"{name}=={version} is pinned in requirements-dev.txt but missing from the lock"
|
||||
)
|
||||
elif entries[name] != version:
|
||||
findings.append(
|
||||
f"{name} is {version} in requirements-dev.txt but {entries[name]} in the lock"
|
||||
)
|
||||
if findings:
|
||||
findings.append("regenerate the lock with scripts/lock-requirements.sh")
|
||||
return findings
|
||||
|
||||
|
||||
def main(argv):
|
||||
if len(argv) not in (1, 3):
|
||||
sys.stderr.write(USAGE)
|
||||
return 2
|
||||
txt_path, lock_path = (
|
||||
(argv[1], argv[2]) if len(argv) == 3 else (DEFAULT_TXT, DEFAULT_LOCK)
|
||||
)
|
||||
try:
|
||||
with (
|
||||
open(txt_path, encoding="utf-8") as f,
|
||||
open(lock_path, encoding="utf-8") as g,
|
||||
):
|
||||
findings = check(f.read(), g.read())
|
||||
except OSError as exc:
|
||||
print(f"::error::{exc}")
|
||||
return 1
|
||||
for finding in findings:
|
||||
print(f"::error::{lock_path}: {finding}")
|
||||
return 1 if findings else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(sys.argv))
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# Sets up a local checkout for development: a virtualenv with the pinned tools
|
||||
# (requirements-dev.txt, the versions CI runs), the pinned collections the roles call
|
||||
# (requirements-dev.lock, the hashed lock of requirements-dev.txt: the versions CI runs), the pinned collections the roles call
|
||||
# (requirements.yml) and git hooks that find the tools.
|
||||
#
|
||||
# scripts/dev-setup.sh # KAMINO_PYTHON=python3.13 scripts/dev-setup.sh to pick one
|
||||
@@ -21,7 +21,7 @@
|
||||
# of requirements.yml into .ansible/collections of this clone: scripts/ansible-lint.sh (the
|
||||
# ansible-lint hook) resolves modules from there, so local lint sees the collection versions CI
|
||||
# does (the ci image bakes requirements.yml) and not the control host's ~/.ansible. It is
|
||||
# idempotent; run it again after changing requirements-dev.txt, requirements.yml or the hook types.
|
||||
# idempotent; run it again after changing requirements-dev.txt or its lock, requirements.yml or the hook types.
|
||||
set -euo pipefail
|
||||
|
||||
# ansible-core 2.22 (requirements-dev.txt) needs Python 3.13+. Renovate raises that pin, so
|
||||
@@ -120,7 +120,7 @@ if [ -x .venv/bin/python ] && ! .venv/bin/python -c "$py_ok" 2>/dev/null; then
|
||||
rm -rf "$root/.venv"
|
||||
fi
|
||||
[ -x .venv/bin/python ] || "$python" -m venv .venv
|
||||
.venv/bin/pip install --quiet --disable-pip-version-check -r requirements-dev.txt
|
||||
.venv/bin/pip install --quiet --disable-pip-version-check --require-hashes -r requirements-dev.lock
|
||||
.venv/bin/pre-commit install --install-hooks
|
||||
|
||||
# The collections of requirements.yml, exactly as pinned, into this clone (.ansible/ is ignored by
|
||||
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env bash
|
||||
# SPDX-FileCopyrightText: Defame1297 and contributors
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#
|
||||
# Regenerate requirements-dev.lock, the hashed lock of requirements-dev.txt (#113).
|
||||
#
|
||||
# scripts/lock-requirements.sh
|
||||
#
|
||||
# Needs the network (PyPI) and Python 3.13+ (ansible-core 2.22; KAMINO_PYTHON overrides python3).
|
||||
# pip-tools is installed, at the pinned version below, into a throwaway virtualenv, never into
|
||||
# the project's .venv. Run it in the same pull request as any change to requirements-dev.txt,
|
||||
# then commit both files: scripts/check_requirements_lock.py (a pre-commit hook and the Lint job)
|
||||
# fails when they disagree. An image input changed, so the image version needs its bump as usual
|
||||
# (python3 scripts/bump_image_versions.py).
|
||||
set -euo pipefail
|
||||
|
||||
pip_tools_version="7.5.2"
|
||||
python="${KAMINO_PYTHON:-python3}"
|
||||
if ! "$python" -c 'import sys; sys.exit(0 if sys.version_info >= (3, 13) else 1)' 2>/dev/null; then
|
||||
echo "error: $python is missing or older than 3.13; set KAMINO_PYTHON=/path/to/python3.13" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
root="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/.." && pwd)"
|
||||
tmp="$(mktemp -d)"
|
||||
trap 'rm -rf "$tmp"' EXIT
|
||||
|
||||
"$python" -m venv "$tmp/venv"
|
||||
"$tmp/venv/bin/pip" install --quiet --disable-pip-version-check "pip-tools==${pip_tools_version}"
|
||||
cd "$root"
|
||||
# --allow-unsafe: setuptools is a dependency of a tool here, and pip refuses an unpinned
|
||||
# requirement in a file that carries hashes.
|
||||
"$tmp/venv/bin/pip-compile" --quiet --generate-hashes --strip-extras --allow-unsafe --newline lf \
|
||||
--output-file requirements-dev.lock requirements-dev.txt
|
||||
python3 scripts/check_requirements_lock.py
|
||||
echo "requirements-dev.lock regenerated; commit it with requirements-dev.txt."
|
||||
@@ -66,6 +66,7 @@ IGNORED = (
|
||||
".ansible-lint",
|
||||
".pre-commit-config.yaml",
|
||||
"requirements-dev.txt",
|
||||
"requirements-dev.lock",
|
||||
"ruff.toml",
|
||||
"apm.yml",
|
||||
"apm.lock.yaml",
|
||||
|
||||
@@ -62,6 +62,11 @@ class RuleTest(unittest.TestCase):
|
||||
" version: 14.1.0",
|
||||
),
|
||||
(".pre-commit-config.yaml", " rev: v1.0.0", " rev: v1.1.0"),
|
||||
(
|
||||
".pre-commit-config.yaml",
|
||||
" rev: v1.0.0",
|
||||
f" rev: {'a' * 40} # frozen: v1.1.0",
|
||||
),
|
||||
(
|
||||
"images/ci/Dockerfile",
|
||||
f"FROM ubuntu:24.04@{D1} AS os",
|
||||
@@ -117,6 +122,16 @@ class RuleTest(unittest.TestCase):
|
||||
),
|
||||
("requirements-dev.txt", "reuse==6.2.0", "reuse @ https://x/y.tgz"),
|
||||
(".pre-commit-config.yaml", " rev: v1.0.0", " repo: https://evil/x"),
|
||||
(
|
||||
".pre-commit-config.yaml",
|
||||
" rev: v1.0.0",
|
||||
f" rev: {'a' * 40} # frozen: v1.1.0 && evil",
|
||||
),
|
||||
(
|
||||
".pre-commit-config.yaml",
|
||||
" rev: v1.0.0",
|
||||
f" rev: {'a' * 40} # note",
|
||||
),
|
||||
(
|
||||
"images/ci/Dockerfile",
|
||||
f"FROM ubuntu:24.04@{D1} AS os",
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
# SPDX-FileCopyrightText: Defame1297 and contributors
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
"""Run with: python3 -m unittest discover -s tests/scripts"""
|
||||
|
||||
import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts"
|
||||
sys.path.insert(0, str(SCRIPTS))
|
||||
import check_requirements_lock as crl
|
||||
|
||||
H = "a" * 64
|
||||
G = "b" * 64
|
||||
TXT = "# pins\nansible-core==2.22.0\nRuamel.Yaml==0.18.0\n"
|
||||
LOCK = (
|
||||
"#\n# autogenerated\n#\n"
|
||||
f"ansible-core==2.22.0 \\\n --hash=sha256:{H} \\\n --hash=sha256:{G}\n"
|
||||
" # via -r requirements-dev.txt\n"
|
||||
f"ruamel-yaml==0.18.0 \\\n --hash=sha256:{H}\n"
|
||||
)
|
||||
|
||||
|
||||
class CheckTest(unittest.TestCase):
|
||||
def test_matching_lock_is_clean(self):
|
||||
self.assertEqual(crl.check(TXT, LOCK), [])
|
||||
|
||||
def test_name_spelling_is_normalised(self):
|
||||
self.assertEqual(crl.canonical("Ruamel.Yaml"), "ruamel-yaml")
|
||||
|
||||
def test_missing_direct_pin(self):
|
||||
findings = crl.check(TXT + "reuse==6.3.0\n", LOCK)
|
||||
self.assertTrue(any("reuse==6.3.0" in f and "missing" in f for f in findings))
|
||||
|
||||
def test_version_drift(self):
|
||||
findings = crl.check(TXT.replace("2.22.0", "2.22.1"), LOCK)
|
||||
self.assertTrue(
|
||||
any("2.22.1 in requirements-dev.txt but 2.22.0" in f for f in findings)
|
||||
)
|
||||
|
||||
def test_entry_without_hash(self):
|
||||
findings = crl.check(TXT, LOCK + "evil==1.0\n")
|
||||
self.assertTrue(any("evil==1.0 has no --hash" in f for f in findings))
|
||||
|
||||
def test_non_pin_lines_are_refused(self):
|
||||
for line in (
|
||||
"evil @ https://x/y.tgz",
|
||||
"-e .",
|
||||
"--index-url https://evil/simple",
|
||||
"evil>=1",
|
||||
):
|
||||
with self.subTest(line=line):
|
||||
findings = crl.check(TXT, LOCK + line + "\n")
|
||||
self.assertTrue(
|
||||
any("not a `name==version` entry" in f for f in findings)
|
||||
)
|
||||
|
||||
def test_hash_before_any_package(self):
|
||||
findings = crl.check(TXT, f" --hash=sha256:{H}\n" + LOCK)
|
||||
self.assertTrue(any("no package before it" in f for f in findings))
|
||||
|
||||
def test_short_hash_is_refused(self):
|
||||
findings = crl.check(TXT, LOCK.replace(G, "b" * 63))
|
||||
self.assertTrue(findings)
|
||||
|
||||
def test_duplicate_entry(self):
|
||||
findings = crl.check(
|
||||
TXT, LOCK + f"ansible-core==2.22.0 \\\n --hash=sha256:{H}\n"
|
||||
)
|
||||
self.assertTrue(any("listed twice" in f for f in findings))
|
||||
|
||||
def test_environment_marker_is_allowed(self):
|
||||
lock = (
|
||||
LOCK
|
||||
+ f'colorama==0.4.6 ; sys_platform == "win32" \\\n --hash=sha256:{H}\n'
|
||||
)
|
||||
self.assertEqual(crl.check(TXT, lock), [])
|
||||
|
||||
def test_the_repository_lock_matches_its_pins(self):
|
||||
root = SCRIPTS.parent
|
||||
self.assertEqual(
|
||||
crl.check(
|
||||
(root / "requirements-dev.txt").read_text(encoding="utf-8"),
|
||||
(root / "requirements-dev.lock").read_text(encoding="utf-8"),
|
||||
),
|
||||
[],
|
||||
)
|
||||
|
||||
|
||||
class MainTest(unittest.TestCase):
|
||||
def test_exit_codes(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
txt, lock = pathlib.Path(tmp, "r.txt"), pathlib.Path(tmp, "r.lock")
|
||||
txt.write_text(TXT)
|
||||
lock.write_text(LOCK)
|
||||
self.assertEqual(crl.main(["x", str(txt), str(lock)]), 0)
|
||||
lock.write_text("")
|
||||
self.assertEqual(crl.main(["x", str(txt), str(lock)]), 1)
|
||||
self.assertEqual(
|
||||
crl.main(["x", str(txt), str(pathlib.Path(tmp, "none"))]), 1
|
||||
)
|
||||
self.assertEqual(crl.main(["x", "only-one"]), 2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -110,6 +110,7 @@ class DevSetupTest(unittest.TestCase):
|
||||
(repo / "scripts").mkdir(parents=True)
|
||||
shutil.copy(SCRIPT, repo / "scripts" / "dev-setup.sh")
|
||||
(repo / "requirements-dev.txt").write_text("")
|
||||
(repo / "requirements-dev.lock").write_text("")
|
||||
(repo / "requirements.yml").write_text("---\ncollections: []\n")
|
||||
self.git(repo, "init", "--quiet")
|
||||
return repo
|
||||
|
||||
Reference in new issue
Block a user