"""Offline citation/archive/tree/blob/tag and physical inventory audit.""" import hashlib import json import tarfile import zipfile from build_definition import OUT, REF, REPO, build from inventory import inventory def main(): b = build() seen = set() for e in b["sources"]: p = (OUT / e["local_file"]).resolve() raw = p.read_bytes() assert hashlib.sha256(raw).hexdigest() == e["sha256"] assert 1 <= e["lines"][0] <= e["lines"][1] <= len(raw.decode().splitlines()) if p in seen: continue seen.add(p) if "git_blob_sha1" in e: blob = hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest() assert blob == e["git_blob_sha1"] tree = p.parent / (e["id"] + "-tree.json") if tree.exists(): t = json.loads(tree.read_bytes()) assert t["sha"] == e["commit"] and not t["truncated"] assert next(x["sha"] for x in t["tree"] if x["path"] == e["file"]) == blob if "package_sha256" in e: archive = next( a for a in p.parent.iterdir() if a.is_file() and a.suffix in [".zip", ".tgz", ".gz"] and hashlib.sha256(a.read_bytes()).hexdigest() == e["package_sha256"] ) if archive.suffix == ".tgz": with tarfile.open(archive, "r:gz") as z: assert z.extractfile(e["file"]).read() == raw else: with zipfile.ZipFile(archive) as z: assert z.read(e.get("member", e["file"])) == raw for key in ["core-math", "core-preview"]: tag = json.loads((REF / (key + "-tag.json")).read_bytes()) tree = json.loads((REF / (key + "-tree.json")).read_bytes()) assert tag["object"]["sha"] == tree["sha"] inv = inventory() assert len(inv["fields"]) == 86 and len(inv["notes"]) == 12 assert [f["pointer"] for f in inv["fields"]] == [f["pointer"] for f in b["fields"]] g = json.loads((REPO / b["source_path"]).read_bytes()) ns = {n["id"]: n for n in g["nodes"]} for eid, src, slot, dst, inp, _ in g["links"]: assert eid in ns[src]["outputs"][slot]["links"] and ns[dst]["inputs"][inp]["link"] == eid sub = g["definitions"]["subgraphs"][0] sn = {n["id"]: n for n in sub["nodes"]} for e in sub["links"]: if e["origin_id"] != -10: assert e["id"] in sn[e["origin_id"]]["outputs"][e["origin_slot"]]["links"] if e["target_id"] != -20: assert sn[e["target_id"]]["inputs"][e["target_slot"]]["link"] == e["id"] assert [n["id"] for n in ns.values() if n["mode"] != 0] == [185] and ns[185]["mode"] == 4 assert all(n["mode"] == 0 for n in sn.values()) assert not ns[204]["outputs"][0]["links"] and not ns[204]["outputs"][2]["links"] assert ns[121]["inputs"][0]["link"] is None and ns[116]["inputs"][3]["link"] is None assert {e["commit"] for e in b["sources"] if e["id"] == "frontend"} == { "8c05266b83b5024f2c12c8de6c2a0a4ecf2ebd33" } (OUT / "source-check.json").write_text( json.dumps( dict( all_pass=True, cited_texts=len(seen), citations=len(b["sources"]), counts=b["counts"], local_file_base=str(OUT.relative_to(REPO)), upstream_executed=False, frontend_release="1.23.0", git_rest_rate_limit_fallback=( "core0.29.2 tag read using git smart protocol;" " source only, no checkout/execution" ), ), ensure_ascii=False, indent=2, ) + "\n" ) print("PASS sources, archives, identities, physical links, 86 fields and 12 notes") if __name__ == "__main__": main()