File size: 1,385 Bytes
1c2e5c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3
"""Verify every payload file against metadata/files.csv and report unexpected files. Standard library only.
Usage: python verify_dataset.py --dataset-root ."""
import argparse, csv, hashlib
from pathlib import Path
UNLISTED = {"metadata/files.csv", "dataset_manifest.json"}
def digest(p):
    h = hashlib.sha256()
    with open(p, "rb") as f:
        for b in iter(lambda: f.read(1 << 24), b""): h.update(b)
    return h.hexdigest()
a = argparse.ArgumentParser(); a.add_argument("--dataset-root", type=Path, default=Path.cwd()); root = a.parse_args().dataset_root.resolve()
rows = list(csv.DictReader(open(root / "metadata/files.csv", encoding="utf-8", newline="")))
fail = []
for r in rows:
    p = root / r["path"]
    if not p.is_file(): fail.append("missing: " + r["path"]); continue
    if p.stat().st_size != int(r["bytes"]): fail.append("size mismatch: " + r["path"]); continue
    if digest(p) != r["sha256"]: fail.append("hash mismatch: " + r["path"])
actual = {p.relative_to(root).as_posix() for p in root.rglob("*") if p.is_file() and ".git" not in p.parts and ".cache" not in p.parts}
fail += ["unexpected: " + x for x in sorted(actual - {r["path"] for r in rows} - UNLISTED)]
if fail: raise SystemExit("Dataset verification failed:\n" + "\n".join(fail))
print(f"Verified {len(rows):,} payload files ({sum(int(r['bytes']) for r in rows):,} bytes).")