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): source=read_csv(root,'original-example');files=read_csv(root,'posted_files');checks=[] for r in files: doc=json.loads((root/r['path']).read_text(encoding='utf-8'));checks.append({'site':r['site'],'file_version':doc.get('version',''),'file_time':doc.get('time',''),'current':clean(doc.get('version'))=='v2' and clean(doc.get('time'))=='14:00'}) current=json.loads((root/'posted/lobby.json').read_text(encoding='utf-8'));valid=clean(current.get('version'))=='v2' and clean(current.get('time'))=='14:00';out=root/'output';out.mkdir(exist_ok=True);target=out/'elevator-v2.json' if valid:target.write_text(json.dumps(current,ensure_ascii=False,indent=2),encoding='utf-8') elif target.exists():target.unlink() actual={'source_positions':[r['게시 위치'] for r in source],'posted_checks':checks,'needs_replacement':[r['site'] for r in checks if not r['current']],'replacement_output':'output/elevator-v2.json' if valid else '', 'replacement_matches_current':valid and json.loads(target.read_text())==current,'old_elevator_file_preserved':json.loads((root/'posted/elevator.json').read_text())['version']=='v1'} return actual,[[r['site'],r['file_version'],r['file_time'],r['current']] for r in checks],{'replacement_reference_valid':valid,'policy':'A blank or incorrect current reference cannot generate a replacement copy.'} 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)