main
John Lauer
Preserve adopted board30 and validated resistor library candidates with native-render dependency
7fa0c9c
from pathlib import Path
from decimal import Decimal,InvalidOperation
import json,hashlib,difflib,shutil
import sexp
R=Path(__file__).resolve().parents[1];O=R/'checkpoints/sensor-gh-review-v8';a=O/'sensor-gh-candidate.kicad_pcb';b=O/'native-saved/sensor-gh-candidate.kicad_pcb'
def clean(x):
if isinstance(x,list):
v=[clean(y)for y in x if not(isinstance(y,list)and y and y[0]in ['filled_polygon','fill_segments','uuid'])]
if v and v[0]in ['kicad_pcb','footprint','pad','fp_line','fp_rect','fp_arc','fp_text','property','segment','via','zone']:
scalar=[z for z in v if not isinstance(z,list)];children=[z for z in v if isinstance(z,list)];return scalar+sorted(children,key=lambda z:json.dumps(z))
return v
if isinstance(x,sexp.Sym):
try:return str(Decimal(x).normalize())
except InvalidOperation:return str(x)
return x
x=sexp.parse(a.read_text());y=sexp.parse(b.read_text());same=clean(x)==clean(y);fills=sum(len(sexp.find(z,'filled_polygon'))for z in sexp.find(y,'zone'))
if not same:
differences=[]
def compare(a,b,path=''):
if a==b or len(differences)>30:return
if isinstance(a,list)and isinstance(b,list)and len(a)==len(b):
for i,(u,v)in enumerate(zip(a,b)):compare(u,v,path+'/'+str(i))
else:differences.append({'path':path,'before':str(a)[:1600],'after':str(b)[:1600]})
compare(clean(x),clean(y));(O/'native-definition-diff.txt').write_text(json.dumps(differences,indent=2))
r={'candidateSha256':hashlib.sha256(a.read_bytes()).hexdigest(),'nativeSavedSha256':hashlib.sha256(b.read_bytes()).hexdigest(),'sameGeometryAndPropertiesIgnoringFillsUuidsOrderNumericFormatting':same,'filledPolygonCount':fills,'limits':'Native save comparison, not thermal or physical qualification'};(O/'native-save-review.json').write_text(json.dumps(r,indent=2)+'\n');print(json.dumps(r));assert same and fills>0
# Native DRC needs a complete isolated project beside this saved snapshot.
for folder in ['lib','3d']:shutil.copytree(O/folder,b.parent/folder,dirs_exist_ok=True)
for name in ['fp-lib-table','sensor-gh-candidate.kicad_pro','sensor-gh-candidate.kicad_dru']:shutil.copy2(O/name,b.parent/name)