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');active=None;candidates=[];cancelled=[];invalid=[] for r in sorted(read_csv(root,'events'),key=lambda r:int(r['order'])): kind=clean(r.get('kind'));time=clean(r.get('time')) if kind not in {'candidate','confirm','cancel'} or not time:invalid.append({'order':r['order'],'kind':kind,'time':time});continue if kind=='candidate': values=[clean(v) for v in time.split('|')] if not all(values):invalid.append({'order':r['order'],'kind':kind,'time':time}) else:candidates=values elif kind=='confirm':active=time;candidates=[] elif kind=='cancel': cancelled.append(time) if active==time:active=None if invalid:active=None stale=[r['announced_time'] for r in read_csv(root,'stale_copy') if clean(r['announced_time']) in cancelled] actual={'source_stages':[r['단계'] for r in source],'active_confirmed_time':active,'new_candidates':candidates,'cancelled_times':cancelled,'stale_confirmed_copy':stale,'ready_to_send_confirmation':active is not None and not invalid} return actual,[[r['order'],r.get('kind',''),r.get('time','')] for r in read_csv(root,'events')],{'event_review':invalid,'policy':'Any unresolved event prevents a current confirmation verdict.'} 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)