File size: 1,471 Bytes
17f5052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Check the frozen SHA-256 manifest without modifying the release.

Integrity is not a certificate of scientific validity. Rebuilding a PDF or
rerunning into the frozen directory can legitimately change a hash; reproduce
into a new directory when comparing scientific results.
"""
from __future__ import annotations
import hashlib
from pathlib import Path
import sys


def main() -> None:
    root = Path(__file__).resolve().parents[1]
    manifest = root / 'SHA256SUMS.txt'
    if not manifest.is_file():
        raise SystemExit('No SHA256SUMS.txt found at the package root.')
    failures = []
    checked = 0
    for number, line in enumerate(manifest.read_text().splitlines(), 1):
        if not line.strip():
            continue
        try:
            expected, relative = line.split('  ', 1)
            path = (root / relative).resolve()
            if not path.is_relative_to(root) or len(expected) != 64:
                raise ValueError('unsafe or malformed entry')
            actual = hashlib.sha256(path.read_bytes()).hexdigest()
            if actual != expected:
                failures.append(relative + ': hash mismatch')
            checked += 1
        except (ValueError, OSError) as exc:
            failures.append(f'line {number}: {exc}')
    if failures:
        print('\n'.join(failures), file=sys.stderr)
        raise SystemExit(1)
    print(f'{checked} manifest entries verified; no mismatches.')


if __name__ == '__main__':
    main()