"""Unpack the checked resource archive used by the browser deployment.""" from pathlib import Path import hashlib import json import zipfile def ensure_resources(): root = Path(__file__).resolve().parent archive = root / "resources.zip" if not archive.exists(): return expected = json.loads((root / "resources_manifest.json").read_text()) if hashlib.sha256(archive.read_bytes()).hexdigest() != expected["archive_sha256"]: raise RuntimeError("Resource archive hash mismatch.") with zipfile.ZipFile(archive) as z: if set(z.namelist()) != set(expected["files"]): raise RuntimeError("Unexpected resource archive members.") for name in z.namelist(): path = Path(name) if path.is_absolute() or ".." in path.parts or path.parts[0] not in {"data", "evidence", "assets", "tests", "scripts", "validation"}: raise RuntimeError("Unsafe resource path.") content = z.read(name) if hashlib.sha256(content).hexdigest() != expected["files"][name]: raise RuntimeError("Resource file hash mismatch.") target = root / path target.parent.mkdir(parents=True,exist_ok=True) target.write_bytes(content)