ci(scripts): lock either root requirements file

The molecule-controller image gets a requirements file and lock of its
own (ADR 0003), so the lock script takes the file to lock:
requirements-dev.txt by default, as before, or requirements-molecule.txt.
Any other argument is a usage error, so a typo cannot write a lock
somewhere else.

The new test runs the script against a stub interpreter, so it needs
no network.

Refs #477

Assisted-by: Claude Code
This commit is contained in:
Defame1297 committed 2026-11-29 17:03:05 +00:00
1 parent 0173f6f226
commit 26c6026d1a
2 files changed
+105 -10

No files matched your search

+18 -10
View File
@@ -2,16 +2,17 @@
# 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).
# Regenerate the hashed lock of a root requirements file: requirements-dev.txt (Lint and Build, the
# default) or requirements-molecule.txt (the molecule-controller image) into the .lock of the same name.
#
# scripts/lock-requirements.sh re-resolve, keeping every locked version that still fits
# scripts/lock-requirements.sh --upgrade move every package, transitive ones included, to its newest release
# scripts/lock-requirements.sh [FILE] re-resolve, keeping every locked version that still fits
# scripts/lock-requirements.sh --upgrade [FILE] move every package, transitive ones included, to its newest release
#
# Needs the network (PyPI) and the Python of .python-version (python<MINOR>; KAMINO_PYTHON overrides it): the
# header of the lock records it, and check_image_versions.py pins fails when it differs.
# pip and pip-tools are installed, at the pinned versions below, into a throwaway virtualenv, never
# into the project's .venv. pip is pinned too: pip-tools breaks on newer pip releases (7.5.2 on pip 26). Commit
# the lock with requirements-dev.txt; pip refuses an install of the two when they disagree
# the lock with its requirements file; pip refuses an install of the two when they disagree
# (`pip install --require-hashes -r requirements-dev.lock -r requirements-dev.txt`).
# The pins are not hash-checked themselves: they come from PyPI over TLS, like the pip that
# created the throwaway venv.
@@ -25,12 +26,19 @@ set -euo pipefail
pip_version="25.2"
pip_tools_version="7.6.2"
usage="usage: lock-requirements.sh [--upgrade] [requirements-dev.txt|requirements-molecule.txt]"
upgrade=()
case "${1:-}" in
"") ;;
--upgrade) upgrade=(--upgrade) ;;
*) echo "usage: lock-requirements.sh [--upgrade]" >&2; exit 2 ;;
if [ "${1:-}" = "--upgrade" ]; then
upgrade=(--upgrade)
shift
fi
requirements="${1:-requirements-dev.txt}"
case "$requirements" in
requirements-dev.txt|requirements-molecule.txt) ;;
*) echo "$usage" >&2; exit 2 ;;
esac
[ "$#" -le 1 ] || { echo "$usage" >&2; exit 2; }
lock="${requirements%.txt}.lock"
root="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/.." && pwd)"
[ -f "$root/.python-version" ] || { echo "error: .python-version is missing (the Python of the lock, a clone and the ci image)" >&2; exit 1; }
@@ -54,5 +62,5 @@ 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 \
"${upgrade[@]}" --output-file requirements-dev.lock requirements-dev.txt
echo "requirements-dev.lock regenerated; commit it with requirements-dev.txt."
"${upgrade[@]}" --output-file "$lock" "$requirements"
echo "$lock regenerated; commit it with $requirements."
+87
View File
@@ -0,0 +1,87 @@
# SPDX-FileCopyrightText: Defame1297 and contributors
# SPDX-License-Identifier: GPL-3.0-or-later
"""Tests for scripts/lock-requirements.sh with a stub interpreter in place of PyPI and pip-tools.
Run with: python3 -m unittest discover -s tests/scripts
"""
import os
import pathlib
import shutil
import subprocess
import tempfile
import unittest
REPO = pathlib.Path(__file__).resolve().parents[2]
SCRIPT = REPO / "scripts" / "lock-requirements.sh"
# `-c` (the version check) passes; `-m venv <dir>` makes a venv whose pip does nothing and whose
# pip-compile records its arguments.
PYTHON_STUB = """#!/usr/bin/env bash
[ "$1" = "-c" ] && exit 0
[ "$1 $2" = "-m venv" ] || exit 1
mkdir -p "$3/bin"
printf '#!/bin/sh\\nexit 0\\n' > "$3/bin/pip"
printf '#!/bin/sh\\necho "$*" >> "$STUB_LOG"\\n' > "$3/bin/pip-compile"
chmod +x "$3/bin/pip" "$3/bin/pip-compile"
"""
@unittest.skipUnless(shutil.which("bash"), "bash is required")
class LockRequirementsTest(unittest.TestCase):
def setUp(self):
self.tmp = pathlib.Path(tempfile.mkdtemp(prefix="kamino-lock-")).resolve()
self.addCleanup(shutil.rmtree, self.tmp, True)
(self.tmp / "scripts").mkdir()
shutil.copy(SCRIPT, self.tmp / "scripts")
(self.tmp / ".python-version").write_text("3.14\n")
for name in ("requirements-dev.txt", "requirements-molecule.txt"):
(self.tmp / name).write_text("a==1\n")
python = self.tmp / "python"
python.write_text(PYTHON_STUB)
python.chmod(0o755)
self.log = self.tmp / "log"
def lock(self, *args):
proc = subprocess.run(
["bash", str(self.tmp / "scripts" / "lock-requirements.sh"), *args],
capture_output=True,
text=True,
check=False,
env={
**os.environ,
"KAMINO_PYTHON": str(self.tmp / "python"),
"STUB_LOG": str(self.log),
},
)
compiled = self.log.read_text() if self.log.exists() else ""
return proc.returncode, compiled, proc.stderr
def test_the_default_locks_the_dev_requirements(self):
code, compiled, err = self.lock()
self.assertEqual(code, 0, err)
self.assertIn(
"--output-file requirements-dev.lock requirements-dev.txt", compiled
)
self.assertNotIn("--upgrade", compiled)
def test_a_requirements_file_is_locked_next_to_itself(self):
code, compiled, err = self.lock("--upgrade", "requirements-molecule.txt")
self.assertEqual(code, 0, err)
self.assertIn(
"--upgrade --output-file requirements-molecule.lock requirements-molecule.txt",
compiled,
)
def test_a_file_that_is_not_a_root_requirements_file_is_refused(self):
for bad in ("requirements-none.txt", "setup.cfg", "../requirements-dev.txt"):
with self.subTest(bad=bad):
code, compiled, _err = self.lock(bad)
self.assertNotEqual(code, 0)
self.assertEqual(compiled, "")
def test_an_unknown_option_is_a_usage_error(self):
code, compiled, err = self.lock("--frobnicate")
self.assertEqual(code, 2)
self.assertIn("usage:", err)
self.assertEqual(compiled, "")