#!/usr/bin/env python3 """Verify SHA-256 hashes for the listed release files, without network access.""" from __future__ import annotations import hashlib import re from pathlib import Path ROOT = Path(__file__).resolve().parent def verify() -> int: manifest = ROOT / "SHA256SUMS.txt" if not manifest.is_file(): raise ValueError("Missing release SHA256SUMS.txt") entries = manifest.read_text(encoding="utf-8").splitlines() if not entries: raise ValueError("Empty checksum manifest") seen = set() for line in entries: match = re.fullmatch(r"([0-9a-f]{64}) (.+)", line) if match is None: raise ValueError("Malformed checksum manifest line") expected, relative = match.groups() path = (ROOT / relative).resolve() if not path.is_relative_to(ROOT) or relative in seen: raise ValueError("Unsafe or duplicate checksum path") seen.add(relative) if not path.is_file(): raise ValueError(f"Missing release file: {relative}") if hashlib.sha256(path.read_bytes()).hexdigest() != expected: raise ValueError(f"Checksum mismatch: {relative}") return len(entries) if __name__ == "__main__": try: print(f"PASS: {verify()} listed release files match SHA-256 hashes.") except (OSError, ValueError) as error: raise SystemExit(f"FAIL: {error}")