#!/usr/bin/env python3
"""Write manifest.json (sha256 + bytes of every package file). Run last, after REPORT/PROMISES/run_matrix are final."""
import hashlib, json
from datetime import datetime, timezone
from pathlib import Path
PKG = Path(__file__).resolve().parents[2]
SKIP = {"manifest.json", "DELIVERY_GATE.json", "page.json", "insight_page.json"}
files = {str(p.relative_to(PKG)): dict(sha256=hashlib.sha256(p.read_bytes()).hexdigest(), bytes=p.stat().st_size)
         for p in sorted(PKG.rglob("*")) if p.is_file() and p.name not in SKIP and "__pycache__" not in p.parts}
json.dump(dict(package=PKG.name, generated_at=datetime.now(timezone.utc).isoformat(timespec="seconds"), n_files=len(files), files=files),
          open(PKG / "manifest.json", "w"), indent=1)
print(len(files), "files")
