main
John Lauer README: component table per the wiki format, open-source section, no duplicate hero; generators at gen/ 39a59ef 7d ago
#!/usr/bin/env python3
"""Compare a KiCad-exported schematic netlist (kicadsexpr) with design/netlist.json, pin by pin."""
import re, json, sys, os
ROOT = os.path.join(os.path.dirname(__file__), "..")
t = open(sys.argv[1] if len(sys.argv) > 1 else os.path.join(ROOT, "kicad", "sch.net")).read()
t = re.sub(r"\s+", " ", t)
sch = {}
for b in t.split("(net (code")[1:]:
    name = re.search(r'\(name "([^"]*)"\)', b).group(1).lstrip("/")
    nodes = re.findall(r'\(node \(ref "([^"]+)"\) \(pin "([^"]+)"\)', b)
    if name.startswith(("unconnected", "Net-(")):
        continue
    sch[name] = sorted(f"{r}.{p}" for r, p in nodes)
want = {k: sorted(v) for k, v in json.load(open(os.path.join(ROOT, "design", "netlist.json")))["nets"].items()}
ps = {p: n for n, l in sch.items() for p in l}
pw = {p: n for n, l in want.items() for p in l}
diff = sorted((p, pw.get(p), ps.get(p)) for p in set(ps) | set(pw) if pw.get(p) != ps.get(p))
print(f"schematic nets {len(sch)}, netlist.json nets {len(want)}, connected pins {len(pw)}, mismatches {len(diff)}")
for d in diff[:30]:
    print("  ", d)
sys.exit(1 if diff else 0)