import csv,json,pathlib,hashlib,shutil csv.field_size_limit(10000000);out=pathlib.Path('outputs/investmoz-audit-current-20261012');f=pathlib.Path('docs/investmoz/v2/auditoria-portal/inventario-fontes.csv') plan=json.loads((out/'authoring-plan.json').read_text(encoding='utf-8'));entry=plan['files'][2] before=list(csv.reader(f.open(encoding='utf-8-sig',newline='')));headers=before[0];rows=[dict(zip(headers,r)) for r in before[1:]] source=json.loads((out/'source-availability-reconciled.json').read_text(encoding='utf-8'));smap={r['reference']:r for r in source} counts={} for row in rows: s=smap[row['Exemplar/caminho']];embedded=bool(s.get('embedded_member'));exists=bool(s.get('original_exists')) or bool(s.get('resolved_path') and pathlib.Path(s['resolved_path']).is_file()) status='Anexo embebido inventariado' if embedded else ('Exemplar acessível' if exists else 'Original indisponível') row['Disponibilidade conferida 12/10/2026']=status counts[status]=counts.get(status,0)+1 if not exists and not embedded:row['Observação da reconciliação']='Ficheiro temporário Word indisponível; referência histórica preservada, não omitir do inventário.' entry.update(before=before,before_sha256=hashlib.sha256(f.read_bytes()).hexdigest(),after=[headers]+[[r.get(h,'') for h in headers] for r in rows]);plan['files']=[entry] shutil.copyfile(out/'authoring-plan.json',out/'authoring-plan.initial.json');shutil.copyfile(out/'csv-authoring-proof.json',out/'csv-authoring-initial.json') (out/'authoring-plan.json').write_text(json.dumps(plan,ensure_ascii=False),encoding='utf-8');(out/'source-counts.json').write_text(json.dumps(counts,ensure_ascii=False,indent=2),encoding='utf-8') print(json.dumps(counts,ensure_ascii=True))