utils/dep_freshness/parsers/pacman_lock.py
Krzysztof kuhy Rudnicki 14f0f47c56
Some checks are pending
file length / file-length (push) Waiting to run
kuhy_credit drift / drift (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
dep_freshness: add pacman-repo lock adapter
A key=value *.lock file (name/repo/version/sha256) pins one package out of
an Arch-style repo .db. The adapter reads the db (gzip or zstd tar) via a
new http.get_bytes, matches %NAME% in each desc, and the judge compares
with a port of pacman's vercmp (epoch:pkgver-pkgrel), not PEP 440. A right
version with a wrong sha256 is a lock mismatch.

Locks are recognised by content (a repo= line ending in .db in the first
4 KB of a *.lock), so explicit paths from pre-commit and --all both work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 14:43:40 +01:00

82 lines
2.7 KiB
Python

"""A pacman-repo lock -> one package pinned out of an Arch-style repo db.
The format is one `key=value` per line, `#` comments allowed:
name=proton-cachyos-native
repo=https://mirror.cachyos.org/repo/x86_64/cachyos/cachyos.db
version=1:11.0.20261015-1
sha256=<hex>
There is no fixed filename, so `is_lock` recognises one by content: a
`*.lock` file whose first few KB hold a `repo=` line ending in `.db`. That
keeps Cargo.lock, pubspec.lock and friends out without a name list, and lets
pre-commit pass the file by name. `version` is kept verbatim: the epoch makes
it unreadable to `exact_pin`, and the judge compares it with pacman's vercmp.
`sha256` rides in `Dep.locked`, the field for "what the lock resolved to".
"""
from __future__ import annotations
import re
from pathlib import Path
from dep_freshness._tables import (
PACMAN_LOCK_SNIFF_BYTES,
PACMAN_LOCK_SUFFIX,
PACMAN_REPO,
)
from dep_freshness.models import Dep
from dep_freshness.registries.pacman_repo import qualified_name
_REPO_DB = re.compile(r"^\s*repo\s*=\s*\S+\.db\s*$", re.MULTILINE)
def is_lock(path: Path) -> bool:
"""True for a `*.lock` whose head names a repo `.db`."""
if not path.name.endswith(PACMAN_LOCK_SUFFIX):
return False
try:
with path.open("rb") as handle:
head = handle.read(PACMAN_LOCK_SNIFF_BYTES)
except OSError:
return False
return bool(_REPO_DB.search(head.decode("utf-8", errors="replace")))
def _fields(text: str) -> dict[str, tuple[str, int]]:
"""`key -> (value, 1-based line)`; the first occurrence of a key wins."""
fields: dict[str, tuple[str, int]] = {}
for number, line in enumerate(text.splitlines(), start=1):
stripped = line.strip()
if not stripped or stripped.startswith("#") or "=" not in stripped:
continue
key, _, value = stripped.partition("=")
fields.setdefault(key.strip(), (value.strip(), number))
return fields
def parse(path: Path) -> list[Dep]:
if not is_lock(path):
return []
try:
fields = _fields(path.read_text(encoding="utf-8", errors="replace"))
except OSError:
return []
name, name_line = fields.get("name", ("", 0))
repo, _ = fields.get("repo", ("", 0))
if not name or not repo:
return []
version, version_line = fields.get("version", ("", 0))
sha, _ = fields.get("sha256", ("", 0))
return [
Dep(
ecosystem=PACMAN_REPO,
name=qualified_name(repo, name),
constraint=version,
path=path,
line=version_line or name_line,
pinned=version or None,
locked=sha.lower() or None,
)
]