main
John Lauer Adopt native-verified amplifier and comparator TI lands as Board32 98249e8
"""Prove candidate edits are limited and preserve native schematic pin connectivity."""
from pathlib import Path
import json,hashlib,copy,xml.etree.ElementTree as ET
import sexp
R=Path(__file__).resolve().parents[1];O=R/'checkpoints/amplifier-comparator-land-review'
a=sexp.parse((R/'aiflow/run/board-31.kicad_pcb').read_text());b=sexp.parse((O/'ic-land-candidate.kicad_pcb').read_text())
def normalize(t):
 t=copy.deepcopy(t)
 for f in sexp.find(t,'footprint'):
  ref=next(p[2]for p in sexp.find(f,'property')if p[1]=='Reference')
  if ref not in ['U3','U7']:continue
  f[1]='allowed-library-binding'
  for p in sexp.find(f,'pad'):
   p[3]=sexp.Sym('allowed-shape');at=sexp.first(p,'at');at[1:3]=[sexp.Sym('0'),sexp.Sym('0')]
   sexp.first(p,'size')[1:]=[sexp.Sym('0'),sexp.Sym('0')]
   p[:]=[x for x in p if not(isinstance(x,list)and x and x[0]=='roundrect_rratio')]
 for z in sexp.find(t,'zone'):z[:]=[x for x in z if not(isinstance(x,list)and x and x[0]=='filled_polygon')]
 return t
assert normalize(a)==normalize(b),'Unexpected edit beyond two footprints'
netlist=R/'checkpoints/library-label-review/native-saved/complete-netlist.xml';n=ET.parse(netlist).getroot();expected={}
for net in n.findall('nets/net'):
 for node in net.findall('node'):
  if 'no_connect' not in node.attrib.get('pintype','').split('+'):expected[(node.attrib['ref'],node.attrib['pin'])]=net.attrib['name']
actual={}
for f in sexp.find(b,'footprint'):
 ref=next(p[2]for p in sexp.find(f,'property')if p[1]=='Reference')
 for p in sexp.find(f,'pad'):
  net=sexp.first(p,'net')
  if net:actual[(ref,p[1])]=net[-1]
assert actual==expected
j={'candidateSha256':hashlib.sha256((O/'ic-land-candidate.kicad_pcb').read_bytes()).hexdigest(),'unchangedExceptU3U7LandGeometryBindingsAndRemovedFills':True,'retainedNativeNetlist':str(netlist.relative_to(R)),'nativeNetlistSha256':hashlib.sha256(netlist.read_bytes()).hexdigest(),'connectedPinMemberships':len(actual),'pinMembershipMatches':True,'limits':'Retained native schematic connectivity comparison; modified schematic footprint bindings still need native export. No adoption.'}
(O/'scope-and-connectivity.json').write_text(json.dumps(j,indent=2)+'\n');print(json.dumps(j))