#!/usr/bin/env python3 """Check shipped native-library bytes against inventory and provenance documentation.""" from hashlib import sha256 from pathlib import Path import re import sys ROOT = Path(__file__).resolve().parents[1] INVENTORY = ROOT / "docs" / "android-native-provenance-inventory.txt" PROVENANCE = ROOT / "docs" / "NOISE-CORE-PROVENANCE.md" def digest(path: Path) -> str: hasher = sha256() with path.open("rb") as stream: for chunk in iter(lambda: stream.read(1024 * 1024), b""): hasher.update(chunk) return hasher.hexdigest() def inventory_records() -> dict[str, str]: if not INVENTORY.is_file(): raise SystemExit(f"checked-in native inventory not found: {INVENTORY}") records = {} for line in INVENTORY.read_text().splitlines(): if not line.strip(): continue try: path, expected = line.split("|", 1) except ValueError: raise SystemExit(f"invalid native inventory line: {line}") records[path] = expected return records records = inventory_records() if not records: raise SystemExit("native inventory is empty") for relative, expected in records.items(): path = ROOT / relative if not path.is_file(): raise SystemExit(f"native library not found: {relative}") actual = digest(path) if actual != expected: raise SystemExit(f"native library digest mismatch for {relative}: recorded {expected}, actual {actual}") text = PROVENANCE.read_text() for relative, expected in records.items(): abi = Path(relative).parent.name match = re.search(rf"^- `{re.escape(abi)}`: `([0-9a-f]+)`$", text, re.MULTILINE) if match is None: raise SystemExit(f"provenance document has no authoritative hash for {abi}") documented = match.group(1) if documented != expected: raise SystemExit(f"provenance digest mismatch for {relative}: recorded {expected}, documented {documented}") print(f"PASS: native provenance verified ({len(records)} libraries)")