|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Verify that a release archive contains required member paths.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import sys |
| 8 | +import tarfile |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +def normalize_member(path: str) -> str: |
| 13 | + return path.strip().lstrip("./").rstrip("/") |
| 14 | + |
| 15 | + |
| 16 | +def archive_members(path: Path) -> set[str]: |
| 17 | + with tarfile.open(path, "r:*") as archive: |
| 18 | + return {normalize_member(member.name) for member in archive.getmembers()} |
| 19 | + |
| 20 | + |
| 21 | +def main() -> int: |
| 22 | + parser = argparse.ArgumentParser(description="Verify required files inside a tar archive") |
| 23 | + parser.add_argument("archive", help="Archive to inspect") |
| 24 | + parser.add_argument( |
| 25 | + "--require", |
| 26 | + action="append", |
| 27 | + default=[], |
| 28 | + help="Required archive member path. May be supplied multiple times.", |
| 29 | + ) |
| 30 | + args = parser.parse_args() |
| 31 | + |
| 32 | + archive_path = Path(args.archive) |
| 33 | + if not archive_path.is_file(): |
| 34 | + print(f"archive does not exist: {archive_path}", file=sys.stderr) |
| 35 | + return 2 |
| 36 | + if not args.require: |
| 37 | + print("at least one --require path is needed", file=sys.stderr) |
| 38 | + return 2 |
| 39 | + |
| 40 | + try: |
| 41 | + members = archive_members(archive_path) |
| 42 | + except (tarfile.TarError, OSError) as exc: |
| 43 | + print(f"could not read archive {archive_path}: {exc}", file=sys.stderr) |
| 44 | + return 2 |
| 45 | + |
| 46 | + required = [normalize_member(item) for item in args.require] |
| 47 | + missing = [item for item in required if item not in members] |
| 48 | + if missing: |
| 49 | + print(f"archive content verification failed for {archive_path}:", file=sys.stderr) |
| 50 | + for item in missing: |
| 51 | + print(f"- missing: {item}", file=sys.stderr) |
| 52 | + return 1 |
| 53 | + |
| 54 | + print(f"archive content verified: {archive_path} ({len(required)} required paths)") |
| 55 | + return 0 |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + raise SystemExit(main()) |
0 commit comments