File size: 1,181 Bytes
1d047d9 | 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 | """Validate this Hugging Face distribution's SHA-256 manifest (standard library only)."""
from pathlib import Path
import hashlib
ROOT = Path(__file__).resolve().parent
def main():
count = 0
seen = set()
for line in (ROOT / 'SHA256SUMS.txt').read_text(encoding='utf-8').splitlines():
if not line.strip():
continue
digest, name = line.split(' ', 1)
path = (ROOT / name).resolve()
if name in seen or len(digest) != 64 or any(c not in '0123456789abcdef' for c in digest):
raise SystemExit('Invalid or duplicate manifest entry: ' + name)
if not path.is_relative_to(ROOT) or not path.is_file():
raise SystemExit('Missing file or unsafe path: ' + name)
actual = hashlib.sha256(path.read_bytes()).hexdigest()
if actual != digest:
raise SystemExit('SHA-256 mismatch: ' + name)
seen.add(name)
count += 1
if not count:
raise SystemExit('Manifest is empty.')
print(f'PASS: {count} distribution file hashes match.')
print('Integrity verifies bytes; it does not certify mathematical correctness.')
if __name__ == '__main__':
main()
|