main
John Lauer Adopt native-reviewed Board31 library corrections and publish process clip 17c4685
"""Send the complete hierarchical schematic for native export and exact pin parity."""
from pathlib import Path
import json,subprocess,xml.etree.ElementTree as ET,hashlib,sys
import sexp
p=Path(__file__).resolve().parents[1];out=p/'checkpoints/library-label-review/native-saved';remote='C:/Users/john/adom-projects/adom-custom-electronic-load/review/library-label-netlist'
def call(verb,args,label):
 r=subprocess.run(['adom-bridge','--target','AdomLapper','--ai-thread','adom-custom-load',verb,json.dumps(args)],text=True,capture_output=True);j=json.loads(r.stdout);(out/(label+'.json')).write_text(json.dumps(j,indent=2)+'\n');assert r.returncode==0 and j.get('success',j.get('ok',True)) and not j.get('error') and j.get('exitCode',0)==0,j;return j
if '--reuse-export' not in sys.argv:
 call('send_files',{'filePaths':[str(f) for f in out.glob('*.kicad_sch')],'dest':remote+'/','reason':'Include every hierarchical sheet; root-only CLI shim export was empty'},'netlist-send-complete-hierarchy')
 cli=call('kicad_status',{},'netlist-native-status')['install']['kicadCli']
 call('shell_execute',{'command':'"'+cli+'"','args':['sch','export','netlist','--format','kicadxml','--output',remote+'/complete-netlist.xml',remote+'/adom-custom-electronic-load.kicad_sch'],'timeoutSeconds':120,'reason':'Export full native schematic candidate for pin and R7 identity parity'},'netlist-native-export')
 call('pull_file',{'filePaths':[remote+'/complete-netlist.xml'],'saveTo':str(out)},'netlist-native-pull')
n=ET.parse(out/'complete-netlist.xml').getroot();components=n.findall('components/comp');assert len(components)==104,len(components)
t=sexp.parse((out/'library-label-candidate.kicad_pcb').read_text());board={};allpads=set()
for f in sexp.find(t,'footprint'):
 ref=next(q[2] for q in sexp.find(f,'property') if q[1]=='Reference')
 for pad in sexp.find(f,'pad'):
  allpads.add((ref,str(pad[1])))
  nets=sexp.find(pad,'net')
  if nets:board[(ref,str(pad[1]))]=str(nets[0][-1])
sch={};no_connect=[]
for net in n.findall('nets/net'):
 for node in net.findall('node'):
  key=(node.attrib['ref'],node.attrib['pin'])
  if 'no_connect' in node.attrib.get('pintype','').split('+'):
   assert len(net.findall('node'))==1 and key in allpads and key not in board,(net.attrib,key)
   no_connect.append({'pin':key,'nativeNet':net.attrib['name'],'explicitPinType':node.attrib['pintype'],'boardPadPresentAndUnassigned':True})
  else:sch[key]=net.attrib['name']
errors=[{'pin':key,'schematic':sch.get(key),'board':board.get(key)} for key in sorted(set(sch)|set(board)) if sch.get(key)!=board.get(key)]
c=n.find("components/comp[@ref='R7']");mpn=next((x.text for x in c.findall('fields/field') if x.attrib.get('name')=='MPN'),None)
r={'componentCount':len(components),'boardPinCount':len(board),'schematicPinCount':len(sch),'explicitNoConnects':no_connect,'mismatches':errors,'r7Value':c.findtext('value'),'r7Mpn':mpn,'candidateSha256':hashlib.sha256((out/'library-label-candidate.kicad_pcb').read_bytes()).hexdigest(),'scope':'Exact native net names and pin membership plus R7 value/MPN; no electrical performance qualification'}
(out/'netlist-parity.json').write_text(json.dumps(r,indent=2)+'\n');print(json.dumps(r));assert not errors and r['r7Value']=='4.7k' and mpn=='ERJ-2RKF4701X'

for ref in ['Q3','Q4']:
 c=n.find(f"components/comp[@ref='{ref}']");mpn=next(x.text for x in c.findall('fields/field') if x.attrib.get('name')=='MPN');assert c.findtext('value')=='NX3008NBK' and mpn=='NX3008NBK,215'
print('Q3/Q4 exact ordering code and value verified in native schematic export')

refs=['R1', 'R11', 'R13', 'R14', 'R15', 'R16', 'R17', 'R18', 'R19', 'R2', 'R20', 'R21', 'R22', 'R23', 'R24', 'R25', 'R26', 'R27', 'R28', 'R29', 'R3', 'R30', 'R33', 'R34', 'R35', 'R36', 'R37', 'R38', 'R4', 'R5', 'R6']
for ref in refs:
 c=n.find(f"components/comp[@ref='{ref}']")
 assert c.findtext('footprint')=='Load:R_0402_Bourns_CR_1.00x0.50mm',(ref,c.findtext('footprint'))
print('31 exact Bourns footprint bindings verified in native schematic export')

for ref,fp in [('R7','Load:R_0402_Panasonic_0402_Reflow'),('R8','Load:R_0402_Panasonic_0402_Reflow'),('R9','Load:R_0402_Vishay_TNPW_IPC'),('R10','Load:R_0402_Vishay_TNPW_IPC')]:
 c=n.find(f"components/comp[@ref='{ref}']")
 assert c.findtext('footprint')==fp,(ref,c.findtext('footprint'))
print('Four Panasonic and precision-resistor footprint bindings verified')

c=n.find("components/comp[@ref='C6']")
assert c.findtext('footprint')=='Load:C_0805_Samsung_CL21_2.00x1.25mm_Reflow'
print('C6 manufacturer land-pattern binding verified')

for ref,fp in [('F1','Load:Fuse_1206_Littelfuse_466'),('D4','Load:D_SMA_Diodes_B140')]:
 c=n.find(f"components/comp[@ref='{ref}']")
 assert c.findtext('footprint')==fp
print('F1 and D4 manufacturer footprint bindings verified')