File size: 1,180 Bytes
40bbfa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Write the deterministic SHA-256 manifest for a release directory."""

from __future__ import annotations

import argparse
import hashlib
from pathlib import Path


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


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--root", type=Path, default=Path("."))
    args = parser.parse_args()
    root = args.root.resolve()
    output = root / "SHA256SUMS.txt"
    files = sorted(
        path
        for path in root.rglob("*")
        if path.is_file()
        and path != output
        and ".git" not in path.relative_to(root).parts
        and "__pycache__" not in path.relative_to(root).parts
    )
    rows = [f"{sha256_file(path)}  {path.relative_to(root).as_posix()}\n" for path in files]
    output.write_text("".join(rows), encoding="utf-8")
    print(f"wrote {len(rows)} entries to {output}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())