Files
computer-playbook/utils/update/base.py
T
kevinveenbirkenbachandClaude Opus 4.7 98457f98fd refactor(update): unify version bumpers under utils/update + cli/contributing/update + scripts/github/update
Folds the parallel Docker-image and git-ref version-update plumbing
into a single subsystem so the auto-update workflow, the external
freshness tests, and the unit tests all share one module layout.

utils/
  update/
    base.py        # semver primitives shared by both backends
    docker.py      # OCI registry lookups + version: line rewriter
    repository.py  # git ls-remote walker + ref: line rewriter

cli/contributing/update/
  docker/           # python -m cli.contributing.update.docker
  repository/       # python -m cli.contributing.update.repository

scripts/github/update/
  image_versions.sh
  repository_refs.sh

Tests mirror the same shape under tests/{unit,external}/update/{docker,repository}/.

Also widens the Docker bump scope to all roles (previously gated on
`web-*`) so non-web roles like sys-svc-container get bumped too;
pins Bookwyrm's python image at 3.11-bookworm via
`# nocheck: docker-version` with a TODO until upstream supports
Python >= 3.12.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 20:12:07 +02:00

66 lines
2.0 KiB
Python

"""Semver primitives shared by every version-bump backend.
A "version" here is a tag of the shape ``v?<numeric-semver>(-<flavor>)?``,
where the numeric part has 1 to 4 dot-separated components and the
optional ``-<flavor>`` suffix is an opaque discriminator (e.g. the
Docker Official Image tag ``5.4.5-php8.3-apache`` or the npm-style
``1.2.3-alpha``).
Upgrade candidates MUST share the same depth (1, 2, 3 or 4 components)
AND the same flavor as the current tag, so that ``5.4.5-php8.3-apache``
never silently bumps to ``5.4.6-php8.4-apache`` (different runtime) or
to ``5.4.6`` (different depth).
"""
from __future__ import annotations
import re
_SEMVER_CORE = r"v?\d+(?:\.\d+){0,3}"
_VERSIONED_TAG_RE = re.compile(rf"^(?P<semver>{_SEMVER_CORE})(?P<flavor>-\S+)?$")
def _parse_versioned_tag(tag: str) -> tuple[str, str] | None:
match = _VERSIONED_TAG_RE.match(str(tag).strip())
if match is None:
return None
return match.group("semver"), match.group("flavor") or ""
def is_semver(value: str) -> bool:
return _parse_versioned_tag(value) is not None
def version_key(tag: str) -> tuple[int, ...]:
parsed = _parse_versioned_tag(tag)
if parsed is None:
return (0,) * 4
semver, _flavor = parsed
parts = tuple(int(part) for part in semver.lstrip("v").split("."))
return parts + (0,) * (4 - len(parts))
def version_depth(tag: str) -> int:
parsed = _parse_versioned_tag(tag)
if parsed is None:
return 0
semver, _flavor = parsed
return len(semver.lstrip("v").split("."))
def version_flavor(tag: str) -> str:
"""Return the ``-<flavor>`` suffix of a versioned tag, or "" when none."""
parsed = _parse_versioned_tag(tag)
return parsed[1] if parsed else ""
def latest_semver(tags: list[str], depth: int, flavor: str = "") -> str | None:
candidates = [
tag
for tag in tags
if is_semver(tag)
and version_depth(tag) == depth
and version_flavor(tag) == flavor
]
return max(candidates, key=version_key, default=None)