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=['FAQ', '검토 사유'] def compute(root=HERE): import re source=read_csv(root,'original-example');checks=[];publish=[] for r in read_csv(root,'drafts'): errors=[];answer=str(r.get('answer') or '') if not clean(r.get('id')):errors.append('missing_id') if not clean(answer):errors.append('missing_answer') if re.search(r'01[0-9]-[0-9]{3,4}-[0-9]{4}',answer):errors.append('phone_pattern') if 'TEST KIM' in answer:errors.append('fixture_name') if not clean(r.get('source')) or not (root/r['source']).is_file():errors.append('missing_source') checks.append({'id':r['id'],'errors':errors}) if not errors:publish.append(r) out=root/'output';out.mkdir(exist_ok=True);(out/'public-faq.json').write_text(json.dumps(publish,ensure_ascii=False,indent=2),encoding='utf-8') actual={'source_topic_groups':[r['문의 묶음'] for r in source],'draft_checks':checks,'publish_candidate_ids':[r['id'] for r in publish],'blocked_count':sum(bool(r['errors']) for r in checks),'public_output_has_phone_pattern':bool(re.search(r'01[0-9]-[0-9]{3,4}-[0-9]{4}',(out/'public-faq.json').read_text()))} return actual,[[r['id'],' / '.join(r['errors']) or '공개 후보'] for r in checks],{'policy':'Nonblank id/answer/source are prerequisites;known privacy-pattern checks are not universal anonymisation.'} 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)