#!/usr/bin/env python3 """Refresh the .cargo-checksum.json of vendored asic-rs crates after the patch queue (thirdparty/asic-rs/patches/) has been applied to the cargo-vendored tree. cargo verifies each vendored file against the per-file SHA256 in that crate's .cargo-checksum.json; a patched file whose checksum is stale fails the build. For every crate named on the command line (or, with --all, every asic-rs* crate under vendor/), this recomputes the SHA256 of each file already listed in the manifest and rewrites it, preserving the top-level "package"/"$comment" keys. Files not listed are left alone (cargo only checks listed files). Usage: refresh-checksums.py [ ...] refresh-checksums.py --all # every vendor/asic-rs* crate """ import hashlib import json import sys from pathlib import Path REPO = Path(__file__).resolve().parents[2] VENDOR = REPO / "vendor" def sha256(path: Path) -> str: h = hashlib.sha256() with open(path, "rb") as f: for chunk in iter(lambda: f.read(1 << 16), b""): h.update(chunk) return h.hexdigest() def refresh(crate_dir: Path) -> int: manifest = crate_dir / ".cargo-checksum.json" if not manifest.exists(): print(f"skip {crate_dir.name}: no .cargo-checksum.json") return 0 data = json.loads(manifest.read_text()) files = data.get("files", {}) changed = 0 for rel in list(files): fp = crate_dir / rel if not fp.exists(): print(f" WARN {crate_dir.name}: listed file missing: {rel}") continue new = sha256(fp) if new != files[rel]: files[rel] = new changed += 1 if changed: manifest.write_text(json.dumps(data, separators=(",", ":"))) print(f"{crate_dir.name}: refreshed {changed} checksum(s)") else: print(f"{crate_dir.name}: no changes") return changed def main() -> None: args = sys.argv[1:] if not args: print(__doc__) sys.exit(2) if args == ["--all"]: crates = sorted(p for p in VENDOR.glob("asic-rs*") if p.is_dir()) else: crates = [Path(a) if Path(a).is_absolute() else (VENDOR / a) for a in args] total = 0 for c in crates: total += refresh(c) print(f"done: {total} checksum(s) refreshed across {len(crates)} crate(s)") if __name__ == "__main__": main()