File size: 1,469 Bytes
bf2848b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Author:浅斟
"""Verify release files using only the Python standard library."""
from pathlib import Path
import hashlib


def main():
    root = Path(__file__).resolve().parent
    rows = (root / "SHA256SUMS").read_text().splitlines()
    seen = set()
    for line in rows:
        digest, name = line.split("  ", 1)
        if not name or Path(name).name != name or name in seen:
            raise ValueError("Invalid checksum manifest")
        seen.add(name)
        path = root / name
        if path.is_symlink() or not path.is_file():
            raise ValueError("Missing or linked release file: " + name)
        hasher = hashlib.sha256()
        with path.open("rb") as handle:
            for block in iter(lambda: handle.read(4 << 20), b""):
                hasher.update(block)
        if hasher.hexdigest() != digest:
            raise ValueError("Checksum mismatch: " + name)
    extras = {p.name for p in root.iterdir() if p.is_file()} - seen - {"SHA256SUMS"}
    if extras:
        raise ValueError("Unlisted files: " + ", ".join(sorted(extras)))
    directories = {p.name for p in root.iterdir() if p.is_dir()} - {"__pycache__"}
    if directories:
        raise ValueError("Unexpected directories: " + ", ".join(sorted(directories)))
    cache = root / "__pycache__"
    if cache.is_symlink():
        raise ValueError("__pycache__ must not be a symlink")
    print(f"OK: {len(seen)} release files verified")


if __name__ == "__main__":
    main()