from pathlib import Path import csv,json,platform,datetime,sys HERE=Path(__file__).resolve().parent def clean(value):return str(value or '').strip() def read_csv(root,name): with (root/'input'/f'{name}.csv').open(encoding='utf-8-sig',newline='') as f:return list(csv.DictReader(f)) def hashfile(path): import hashlib return hashlib.sha256(path.read_bytes()).hexdigest() def known(value):return clean(value).lower() not in {"","미확인","unknown"} REPORT_HEADERS=['원본행', '자산번호', '원래 이름', '원래 위치'] def compute(root=HERE): from collections import Counter assets=read_csv(root,'assets');ids=[clean(r.get('id')) for r in assets if clean(r.get('id'))];count=Counter(ids);registered=set(ids);seen={(clean(r.get('asset')),clean(r.get('part'))) for r in read_csv(root,'parts_seen') if clean(r.get('asset')) in registered and clean(r.get('part'))};missing=[];issues=[] for i,r in enumerate(assets,1): if not clean(r.get('id')):issues.append({'row':i,'issue':'asset_id_missing'}) for r in read_csv(root,'parts_required'): asset,part=clean(r.get('asset')),clean(r.get('part')) if not asset or asset not in registered or not part: missing.append((asset or '[missing_asset]')+':'+(part or '[missing_part]'));issues.append({'asset':asset,'part':part,'issue':'component_link_unresolved'}) elif (asset,part) not in seen:missing.append(asset+':'+part) actual={'register_rows':len(assets),'unique_assets':len(count),'duplicate_ids':[k for k,v in count.items() if v>1],'same_name_distinct_ids':sorted({clean(r.get('id')) for r in assets if r['name']=='vacuum' and clean(r.get('id'))}),'missing_components':missing} return actual,[[i,clean(r.get('id')),r['name'],r['location']] for i,r in enumerate(assets,1)],{'identity_review':issues,'policy':'Empty asset/component identifiers cannot create a matched registered component. Raw names/location text stays intact.'} def run(root=HERE): try:actual,rows,details=compute(root) except ValueError as exc: report={'lab_id':root.name,'status':'INPUT_ERROR','actual':None,'input_error':str(exc),'assertion_count':0} (root/'results.json').write_text(json.dumps(report,ensure_ascii=False,indent=2),encoding='utf-8') (root/'actual.json').write_text('null',encoding='utf-8') with (root/'results.csv').open('w',encoding='utf-8-sig',newline='') as f:csv.writer(f).writerows([['입력 검토 필요'],[str(exc)]]) print(json.dumps(report,ensure_ascii=False));return report expected=json.loads((root/'expected/report.json').read_text(encoding='utf-8')) checks=[{'field':k,'pass':actual.get(k)==v,'actual':actual.get(k),'expected':v} for k,v in expected.items()] passed=all(c['pass'] for c in checks) and set(actual)==set(expected) report={'lab_id':root.name,'synthetic_fixture':True,'environment':{'python':platform.python_version(),'platform':platform.system(),'processor':'Python standard library'},'run_utc':datetime.datetime.now(datetime.timezone.utc).isoformat(),'status':'PASS' if passed else 'FAIL','assertion_count':len(checks),'actual':actual,'assertions':checks,'details':details,'limits':'Synthetic record checks only;not a real building inspection,safety/legal verdict,Excel app test,physical receipt or authenticity verification.'} (root/'results.json').write_text(json.dumps(report,ensure_ascii=False,indent=2),encoding='utf-8') (root/'actual.json').write_text(json.dumps(actual,ensure_ascii=False,indent=2),encoding='utf-8') with (root/'results.csv').open('w',encoding='utf-8-sig',newline='') as f: w=csv.writer(f);w.writerow(REPORT_HEADERS);w.writerows(rows) print(json.dumps(report,ensure_ascii=False,indent=2)) return report if __name__=='__main__': report=run() if report['status']!='PASS':raise SystemExit(1)