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');table=read_csv(root,'photo_links');checks=[];bad=[];issues=[];all_open=True for r in table: role={'전체':'original','주변':'original','표시본':'marked'}.get(clean(r.get('role')),'');fid=clean(r.get('id'));file=str(r.get('file') or '');original=str(r.get('original') or '');file_ok=bool(clean(file)) and (root/file).is_file();all_open=all_open and file_ok if role=='marked': original_ok=bool(clean(original)) and (root/original).is_file();different=file_ok and original_ok and hashfile(root/file)!=hashfile(root/original) checks.append({'id':fid,'original_exists':original_ok,'different_hash':different});all_open=all_open and original_ok broken_exists=bool(clean(original)) and (root/'broken-set'/Path(original).name).is_file() if not broken_exists:bad.append(fid) if not original_ok:issues.append({'id':fid,'issue':'original_reference_missing'}) elif role!='original':issues.append({'id':fid,'issue':'role_missing_or_unknown'});all_open=False actual={'original_example_roles':[r['사진 역할'] for r in source],'valid_marked_links':checks,'broken_set_missing_original':bad,'all_linked_files_open':all_open} return actual,[[clean(r.get('id')),r.get('role',''),r.get('file',''),r.get('original','')] for r in table],{'link_issues':issues,'policy':'Marked roles require a nonblank original reference and the actual source file.'} 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)