File size: 2,104 Bytes
1788f61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/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())