ec-swe / harbor /verify_bundle.py
servicesToptal's picture
EC-SWE dataset
ec02279
Raw History Blame Contribute Delete
1.03 kB
"""Check every distributed file against checksums.json before building anything."""
import hashlib
import json
from pathlib import Path
root = Path(__file__).resolve().parent
checks = json.loads((root / "checksums.json").read_text())
for name, expected in checks.items():
path = (root / name).resolve()
if not path.is_relative_to(root):
raise ValueError("Unsafe checksum path")
with path.open("rb") as stream:
actual = hashlib.file_digest(stream, "sha256").hexdigest()
if actual != expected:
raise RuntimeError("File checksum mismatch: " + name)
listed = set(checks) | {"checksums.json"}
extra = sorted(
path.relative_to(root).as_posix()
for folder in ("tasks", "sources", "evidence")
for path in (root / folder).rglob("*")
if path.is_file() and path.relative_to(root).as_posix() not in listed
and "__pycache__" not in path.parts
)
if extra:
raise RuntimeError("Files not in checksums.json: " + ", ".join(extra[:10]))
print(f"Validated {len(checks)} file checksums")