main
John Lauer Adopt native-verified amplifier and comparator TI lands as Board32 98249e8
"""Isolated U3/U7 TI example lands. No adoption or qualification implied."""
from pathlib import Path
import copy, hashlib, json, shutil
import sexp
R=Path(__file__).resolve().parents[1]; O=R/'checkpoints/amplifier-comparator-land-review'; O.mkdir(exist_ok=True)
source=R/'aiflow/run/board-31.kicad_pcb';sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
assert sha(source)=='a2b7fda1783e8b519a8819d93837c8d6fa5acb57dfde4f43abd2b3fd1b405bc5'
B=R/'checkpoints/library-label-review/native-saved'
for d in ['lib','3d']:shutil.copytree(B/d,O/d,dirs_exist_ok=True)
shutil.copy2(B/'fp-lib-table',O/'fp-lib-table')
for ext in ['kicad_pro','kicad_dru']:shutil.copy2(R/('aiflow/run/board-31.'+ext),O/('ic-land-candidate.'+ext))
b=sexp.parse(source.read_text());rows=[]
for f in sexp.find(b,'footprint'):
 props={p[1]:p[2]for p in sexp.find(f,'property')};ref=props['Reference']
 if ref not in ['U3','U7']:continue
 slug=props['MPN'].lower();cat=R/'components'/slug/(slug+'.kicad_mod');c=sexp.parse(cat.read_text());pads={p[1]:p for p in sexp.find(c,'pad')};before=[]
 name='OPA189_TI_DBV0005A' if ref=='U3' else 'LM339B_TI_D0014A'
 for p in sexp.find(f,'pad'):
  before.append(copy.deepcopy(p));cp=pads[p[1]]
  sexp.first(p,'at')[1:3]=copy.deepcopy(sexp.first(cp,'at')[1:3]);sexp.first(p,'size')[1:]=copy.deepcopy(sexp.first(cp,'size')[1:]);p[3]=copy.deepcopy(cp[3])
  p[:]=[x for x in p if not(isinstance(x,list)and x and x[0]=='roundrect_rratio')]
  for x in sexp.find(cp,'roundrect_rratio'):p.append(copy.deepcopy(x))
 f[1]='Load:'+name
 lib=copy.deepcopy(f);lib[1]=name;lib[:]=[x for x in lib if not(isinstance(x,list)and x and x[0]in ['at','uuid'])]
 for p in sexp.find(lib,'property'):
  if p[1]=='Reference':p[2]='REF**'
 for p in sexp.find(lib,'pad'):
  sexp.first(p,'at')[1:]=sexp.first(p,'at')[1:3];p[:]=[x for x in p if not(isinstance(x,list)and x and x[0]in ['net','uuid'])]
 (O/'lib/Load.pretty'/(name+'.kicad_mod')).write_text(sexp.dump(lib)+'\n')
 rows.append({'reference':ref,'binding':f[1],'catalog':str(cat.relative_to(R)),'catalogSha256':sha(cat),'before':[sexp.dump(p)for p in before],'after':[sexp.dump(p)for p in sexp.find(f,'pad')]})
assert len(rows)==2
for z in sexp.find(b,'zone'):z[:]=[x for x in z if not(isinstance(x,list)and x and x[0]=='filled_polygon')]
dst=O/'ic-land-candidate.kicad_pcb';dst.write_text(sexp.dump(b)+'\n')
for p in (R/'kicad').glob('*.kicad_sch'):
 t=sexp.parse(p.read_text())
 for sy in sexp.find(t,'symbol'):
  props={p[1]:p for p in sexp.find(sy,'property')};ref=props.get('Reference',[None,None,None])[2]
  if ref in ['U3','U7']:props['Footprint'][2]=next(r['binding']for r in rows if r['reference']==ref)
 (O/p.name).write_text(sexp.dump(t)+'\n')
(O/'selection-review.json').write_text(json.dumps({'sourceSha256':sha(source),'candidateSha256':sha(dst),'adopted':False,'changes':rows,'preserved':'Placement, net membership, models, tracks, vias, zone boundaries and fabrication rules','required':'Native refill/DRC, full schematic parity, saved-board and visual review; no physical process acceptance'},indent=2)+'\n')
print('Prepared isolated U3/U7 TI example-land candidate; Board31 unchanged.')