File size: 1,233 Bytes
ee2574f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
#!/usr/bin/env python3
"""Verify every SHA-256 digest listed in metadata/checksums.sha256."""

from __future__ import annotations

import hashlib
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
CHECKSUMS = ROOT / "metadata" / "checksums.sha256"


def sha256(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as handle:
        for chunk in iter(lambda: handle.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def main() -> None:
    verified = 0
    for line_number, line in enumerate(
        CHECKSUMS.read_text(encoding="utf-8").splitlines(), start=1
    ):
        if not line.strip():
            continue
        expected, relative_path = line.split("  ", maxsplit=1)
        path = ROOT / Path(relative_path)
        if not path.is_file():
            raise FileNotFoundError(f"Missing file listed at line {line_number}: {path}")
        actual = sha256(path)
        if actual != expected:
            raise ValueError(
                f"Checksum mismatch for {relative_path}: expected {expected}, got {actual}"
            )
        verified += 1
    print(f"Verified {verified} release files.")


if __name__ == "__main__":
    main()