screen-locker/scripts/gatelock_pin.py
Krzysztof kuhy Rudnicki 422b1d55e0
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (flutter, flutter-analyze, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,flutter-analyze,dependency-freshness) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / lock-popup-safety (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (0) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (1) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (2) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (3) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (4) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (5) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (6) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (7) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (8) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (9) (push) Waiting to run
Tests / flutter-coverage (push) Blocked by required conditions
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Install the gatelock pin over a stale clone
install.sh re-asserted ~/src/utils/gatelock editable on every deploy.
That clone sat at 0.16.0 behind the 0.16.2 pin, so the stall-watch fix
(c890f31a21) was pushed and deployed but never ran. A clone older than
the pin now gets the pinned release instead, and the verify step fails
closed when the imported gatelock is older than the pin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 10:32:47 +01:00

124 lines
3.9 KiB
Python

"""Compare a gatelock against the version screen-locker pins (install.sh's gate).
install.sh re-installs the ~/src/utils/gatelock clone editable so unreleased
gatelock work reaches the lock. That is only right while the clone is at least
the pinned release: on 2026-11-27 a stale clone (0.16.0) was re-asserted over
the 0.16.2 pin and the stall fix never reached the running lock.
CLI (exit 0 ok, 1 older than the pin, 2 unreadable/usage):
gatelock_pin.py clone-ok PYPROJECT CLONE_DIR clone may be made editable
gatelock_pin.py installed-ok PYPROJECT imported gatelock is current
gatelock_pin.py spec PYPROJECT print the pinned pip spec
"""
from __future__ import annotations
from importlib import metadata
import logging
from pathlib import Path
import re
import sys
import tomllib
_logger = logging.getLogger("gatelock_pin")
EXIT_OK = 0
EXIT_OLDER = 1
EXIT_ERROR = 2
_PIN = re.compile(r"^gatelock\s*@.*@gatelock-v(?P<v>[0-9][0-9.]*)#")
Version = tuple[int, ...]
class PinError(ValueError):
"""A version that cannot be read: the gate fails closed on it."""
def _parse(text: str) -> Version:
try:
return tuple(int(part) for part in text.split("."))
except ValueError as exc:
msg = f"not a release version: {text!r}"
raise PinError(msg) from exc
def _project(path: Path) -> dict[str, object]:
try:
data = tomllib.loads(path.read_text(encoding="utf-8"))
except (OSError, tomllib.TOMLDecodeError) as exc:
msg = f"cannot read {path}: {exc}"
raise PinError(msg) from exc
project = data.get("project", {})
return project if isinstance(project, dict) else {}
def _pin(pyproject: Path) -> re.Match[str]:
for dep in _project(pyproject).get("dependencies", []):
match = _PIN.match(str(dep))
if match:
return match
msg = f"no gatelock pin in {pyproject}"
raise PinError(msg)
def pinned_spec(pyproject: Path) -> str:
"""Return the gatelock dependency string pinned in ``pyproject``."""
return _pin(pyproject).string
def pinned_version(pyproject: Path) -> Version:
"""Return the gatelock tag version pinned in ``pyproject``'s dependencies."""
return _parse(_pin(pyproject)["v"].rstrip("."))
def clone_version(clone: Path) -> Version:
"""Return the version a gatelock source clone declares."""
return _parse(str(_project(clone / "pyproject.toml").get("version", "")))
def installed_version() -> Version:
"""Return the version of the gatelock this interpreter imports."""
return _parse(metadata.version("gatelock"))
def _show(version: Version) -> str:
return ".".join(map(str, version))
def _check(what: str, have: Version, pin: Version) -> int:
if have < pin:
_logger.error(
"gatelock %s %s is older than the pin %s", what, _show(have), _show(pin)
)
return EXIT_OLDER
_logger.warning("gatelock %s %s >= pin %s", what, _show(have), _show(pin))
return EXIT_OK
def main(argv: list[str]) -> int:
"""Run one check; see the module docstring for the exit codes."""
try:
match argv:
case ["clone-ok", pyproject, clone]:
pin = pinned_version(Path(pyproject))
return _check("clone", clone_version(Path(clone)), pin)
case ["installed-ok", pyproject]:
pin = pinned_version(Path(pyproject))
return _check("installed", installed_version(), pin)
case ["spec", pyproject]:
sys.stdout.write(pinned_spec(Path(pyproject)) + "\n")
return EXIT_OK
case _:
_logger.error("usage:%s", __doc__)
return EXIT_ERROR
except PinError, metadata.PackageNotFoundError:
# Fail closed: install.sh treats any non-zero exit as untrusted.
_logger.exception("gatelock version gate failed")
return EXIT_ERROR
if __name__ == "__main__":
logging.basicConfig(format="%(message)s")
sys.exit(main(sys.argv[1:]))