File size: 1,033 Bytes
ec02279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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")