File size: 2,349 Bytes
ec01e2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""MANIFEST.sha256 of the package: one line per file, "<sha256>  <size in bytes>  <path relative to the package>". Standard library only.

    python make_manifest.py write PACKAGE_DIR
    python make_manifest.py verify PACKAGE_DIR      (exit 1 on any missing, extra or changed file)

__pycache__ folders are not listed: Python rewrites them.
"""
import hashlib
import os
import sys
from pathlib import Path

NAME = "MANIFEST.sha256"
HEADER = "# sha256  size_bytes  path, every file of this folder except this one and __pycache__\n"


def files(root: Path) -> list[Path]:
    out = []
    for d, sub, names in os.walk(root):
        sub[:] = sorted(s for s in sub if s != "__pycache__")
        for n in sorted(names):
            p = Path(d) / n
            if p != root / NAME:
                out.append(p)
    return out


def sha256(p: Path) -> str:
    h = hashlib.sha256()
    with open(p, "rb") as f:
        while b := f.read(1 << 24):
            h.update(b)
    return h.hexdigest()


def write(root: Path) -> None:
    lines, total = [], 0
    for p in files(root):
        size = p.stat().st_size
        total += size
        lines.append(f"{sha256(p)}  {size}  {p.relative_to(root).as_posix()}\n")
    (root / NAME).write_text(HEADER + "".join(lines), encoding="utf-8", newline="\n")
    print(f"{len(lines)} files, {total} bytes ({total / 2**30:.2f} GiB)")


def verify(root: Path) -> None:
    want = {}
    for line in (root / NAME).read_text(encoding="utf-8").splitlines():
        if line and not line.startswith("#"):
            sha, size, rel = line.split("  ", 2)
            want[rel] = (sha, int(size))
    have = {p.relative_to(root).as_posix(): p for p in files(root)}
    missing, extra = sorted(set(want) - set(have)), sorted(set(have) - set(want))
    changed = [r for r in sorted(set(want) & set(have))
               if have[r].stat().st_size != want[r][1] or sha256(have[r]) != want[r][0]]
    print(f"{len(want)} listed, {len(have)} on disk; missing {len(missing)}, extra {len(extra)}, changed {len(changed)}")
    for label, group in [("missing", missing), ("extra", extra), ("changed", changed)]:
        for r in group:
            print(f"  {label}: {r}")
    sys.exit(1 if missing or extra or changed else 0)


if __name__ == "__main__":
    {"write": write, "verify": verify}[sys.argv[1]](Path(sys.argv[2]))