33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Inventory or verify originals with SHA-256; never writes to the source tree."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
|
|
|
|
def inventory(root):
|
|
return [{"path": p.relative_to(root).as_posix(), "size": p.stat().st_size,
|
|
"sha256": hashlib.sha256(p.read_bytes()).hexdigest(),
|
|
"header": p.read_bytes()[:16].hex()}
|
|
for p in sorted(root.rglob("*")) if p.is_file()]
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("root", type=Path)
|
|
parser.add_argument("--verify", type=Path)
|
|
args = parser.parse_args()
|
|
files = inventory(args.root)
|
|
if args.verify:
|
|
previous = json.loads(args.verify.read_text())["files"]
|
|
if files != previous:
|
|
raise SystemExit("Original inventory differs from recorded baseline")
|
|
print(f"Verified {len(files)} unchanged original files")
|
|
else:
|
|
print(json.dumps({"root": str(args.root), "files": files}, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|