CARLA-MWRS / scripts /roundtrip_check.py
PeterNano's picture
Add CARLA-MWRS v1.0.0 metadata and verification tools
1788f61 verified
Raw
History Blame Contribute Delete
2.1 kB
#!/usr/bin/env python3
"""Extract all archives to a temporary tree and compare every source hash."""
from __future__ import annotations
import argparse
import hashlib
import shutil
import subprocess
import tempfile
from pathlib import Path
def sha256(path: Path) -> str:
h = hashlib.sha256()
with path.open("rb") as f:
for block in iter(lambda: f.read(8 * 1024 * 1024), b""):
h.update(block)
return h.hexdigest()
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--release-root", type=Path, required=True)
ap.add_argument("--keep", action="store_true", help="keep the extracted temporary tree")
args = ap.parse_args()
release = args.release_root.resolve()
temp = Path(tempfile.mkdtemp(prefix="carla-mwrs-roundtrip-"))
try:
for split in ("training", "validation"):
(temp / split).mkdir(parents=True, exist_ok=True)
for archive in sorted((release / split).glob("*.tar.zst")):
subprocess.run(["tar", "--zstd", "-xf", str(archive), "-C", str(temp / split)], check=True)
checked = 0
errors = []
with (release / "SOURCE_FILES.sha256").open(encoding="utf-8") as f:
for line in f:
digest, rel = line.rstrip("\n").split(" ", 1)
path = temp / rel
if not path.is_file():
errors.append(f"missing {rel}")
continue
got = sha256(path)
checked += 1
if got != digest:
errors.append(f"hash mismatch {rel}: {got} != {digest}")
print(f"checked {checked} extracted files")
if errors:
print("FAIL")
print("\n".join(errors[:20]))
return 1
print("PASS: every archive member round-trips to the recorded source hash")
return 0
finally:
if args.keep:
print(f"kept extracted tree at {temp}")
else:
shutil.rmtree(temp, ignore_errors=True)
if __name__ == "__main__":
raise SystemExit(main())