main
John Lauer Component record for ltc2945cms-pbf: files batch 3 16c3b96 23h ago
"""Check over HTTP that every file page.json declares (component.parts, eda_assets, hero) and every
file in the local page tree is served by the wiki, plus the viewer routes.
usage: verify_live.py <slug>"""
import sys, os, json, urllib.request, urllib.parse, hashlib
slug = sys.argv[1]; D = sys.argv[2] if len(sys.argv) > 2 else f"/home/adom/project/uta-dynos/work/pages/{slug}"
W = "https://wiki.adom.inc"
p = json.load(open(os.path.join(D, "page.json")))
declared = set(p["component"]["parts"].values())
for v in p.get("eda_assets", {}).values():
    declared |= {x for k, x in v.items() if k != "provenance"}
declared.add(p["hero"]["path"])
declared |= {"verification.json", "models.json", "lod/manifest.json", "README.md", "page.json"}
local = {os.path.relpath(os.path.join(a, f), D) for a, _, fs in os.walk(D) for f in fs}
def get(url):
    try:
        with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "adom-verify"}), timeout=60) as r:
            return r.status, r.read()
    except urllib.error.HTTPError as e:
        return e.code, b""
    except Exception as e:
        return str(e), b""
bad = []
for f in sorted(declared | local):
    st, body = get(f"{W}/api/pages/adom/{slug}/files/" + urllib.parse.quote(f))
    same = os.path.exists(os.path.join(D, f)) and hashlib.sha256(body).hexdigest() == hashlib.sha256(open(os.path.join(D, f), "rb").read()).hexdigest()
    ok = st == 200 and len(body) > 0 and (same or f == "page.json")
    if not ok or f in declared:
        print(("OK  " if ok else "BAD ") + f"{st} {len(body):>8} {'same' if same else 'DIFF'} {f}{'  [declared]' if f in declared else ''}")
    if not ok: bad.append(f)
for r in ("viewer/3d", "viewer/symbol", "viewer/footprint"):
    st, body = get(f"{W}/{r}/adom/component/{slug}")
    print(f"route {r}: {st} {len(body)} bytes"); 
    if st != 200 or len(body) < 500: bad.append(r)
st, body = get(f"{W}/adom/{slug}"); print("page:", st, len(body))
print(json.dumps({"slug": slug, "checked": len(declared | local), "bad": bad}))