Files
Investidor-V2/outputs/investmoz-referencias-economicas-QA-20261016/verify-matrix-and-package.py
T
nala 2d23157e40
tests / ci (push) Canceled after 0s
versao nrgocios
2026-10-16 13:06:58 +02:00

48 lines
5.9 KiB
Python

from pathlib import Path,PurePosixPath
import csv,io,json,hashlib,zipfile,datetime,shutil
csv.field_size_limit(10000000)
out=Path(__file__).resolve().parent;root=out.parents[1];review=out/'para-revisao';source=Path('C:/Users/macbook/.config/investmoz/referencias-economicas-QA-20261016/source');sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
def records(raw):
text=raw.decode('utf-8-sig');lines=text.splitlines(keepends=True);r=csv.reader(io.StringIO(text,newline=''));header=next(r);last=r.line_num;result=[]
for row in r:
end=r.line_num;result.append((row,''.join(lines[last:end]).encode('utf-8')));last=end
return header,result
old=(out/'matrix-tool/before.csv').read_bytes();path=root/'docs/investmoz/v2/auditoria-portal/matriz-requisitos.csv';new=path.read_bytes();h,a=records(old);h2,b=records(new);assert h==h2 and len(a)==len(b)==3402
matrix=json.loads((out/'MATRIZ-ATUALIZACAO.json').read_text('utf-8'));ids=set(matrix['rows_updated']);allowed={h.index(x)for x in matrix['columns_updated']};count=0
for (r,raw),(s,current)in zip(a,b):
assert r[0]==s[0]
if r[0] not in ids:assert raw==current;count+=1
else:assert all(r[i]==s[i]for i in range(len(h))if i not in allowed)
assert count==3395
matrix['independent_raw_and_cell_verification']=True;matrix['backlog_sha256']=sha(root/'docs/investmoz/v2/auditoria-portal/backlog.md');(out/'MATRIZ-ATUALIZACAO.json').write_text(json.dumps(matrix,ensure_ascii=False,indent=2)+'\n','utf-8')
shutil.copy2(out/'MATRIZ-ATUALIZACAO.json',review/'ATUALIZACAO-MESMA-MATRIZ.json')
manifest=json.loads((review/'MANIFESTO-ENTREGA.json').read_text('utf-8'));candidate=manifest['candidate'];files={}
for entry in [*manifest['application'],*manifest['build'],*manifest['private_originals']]:
p=source/entry['path'];assert sha(p)==entry['sha256'];files['payload/'+entry['path']]=p
for entry in manifest['application']:
if entry['before_sha256']:
p=root/entry['path'];assert sha(p)==entry['before_sha256'];files['rollback/previous/'+entry['path']]=p
for p in sorted((root/'public/build').rglob('*')):
if p.is_file():files['rollback/previous/'+p.relative_to(root).as_posix()]=p
for p in review.iterdir():
if p.is_file()and p.name!='RECIBO-INTEGRIDADE.json':files['documents/'+p.name]=p
for name in ['HTTP-QA.json','REVERSAO-QA.json','PRESERVACAO-QA.json','REFERENCE-CONFLICT-QA.json','VERIFICACAO-LOCAL.json','CANONICAL-FIXTURE-COMPLETION.json','CANONICAL-PUBLICATION-PRECONDITIONS.json','CAE-PUBLIC.json','QA-ISOLAMENTO.json','AUTORIZACAO-QA.json','VISUAL-NEGOCIO.txt','VISUAL-NEGOCIO.png','VISUAL-CANONICO.txt','VISUAL-CANONICO.png','VISUAL-INSTALADOR.txt','VISUAL-INSTALADOR.png','VISUAL-EDITOR.txt','VISUAL-EDITOR.png','VISUAL-RETIRADA.txt','VISUAL-RETIRADA.png']:
files['evidence/'+name]=out/name
files['evidence/VISUAL-PROJECAO-FINAL.png']=out/'VISUAL-PROJECAO-FINAL.png'
plan=json.loads((out/'FINAL-OPERATIONAL-PLAN.json').read_text('utf-8'));assert plan['code_hash']==manifest['code_hash'];plan['operations']={k:{a:b for a,b in v.items()if a!='hash'}for k,v in plan['operations'].items()};(out/'OPERATIONAL-SUMMARY.json').write_text(json.dumps(plan,ensure_ascii=False,indent=2)+'\n','utf-8');files['evidence/OPERATIONAL-SUMMARY.json']=out/'OPERATIONAL-SUMMARY.json'
index={'candidate':candidate,'code_hash':manifest['code_hash'],'entries':{name:{'sha256':sha(p),'bytes':p.stat().st_size}for name,p in files.items()},'excludes_only_this_manifest_itself':True}
(out/'MANIFESTO-INTEGRIDADE.json').write_text(json.dumps(index,ensure_ascii=False,indent=2)+'\n','utf-8')
bundle=out/(candidate+'.zip');assert not bundle.exists(),'Preserve sealed package'
with zipfile.ZipFile(bundle,'w',zipfile.ZIP_DEFLATED,compresslevel=9)as z:
for name,p in files.items():assert '..'not in PurePosixPath(name).parts;z.write(p,name)
z.write(out/'MANIFESTO-INTEGRIDADE.json','MANIFESTO-INTEGRIDADE.json')
with zipfile.ZipFile(bundle)as z:
assert z.testzip()is None
assert set(z.namelist())==set(files)|{'MANIFESTO-INTEGRIDADE.json'}
for name,p in files.items():assert hashlib.sha256(z.read(name)).hexdigest()==sha(p)
assert hashlib.sha256(z.read('MANIFESTO-INTEGRIDADE.json')).hexdigest()==sha(out/'MANIFESTO-INTEGRIDADE.json')
assert not any('.env' in PurePosixPath(n).parts or 'fixture-private' in n or n.endswith('.cookies')or n.endswith('.dump')for n in z.namelist())
receipt={'candidate':candidate,'verified_at':datetime.datetime.now(datetime.timezone.utc).isoformat(),'code_hash':manifest['code_hash'],'manifest_data_sha256':manifest['data_manifest_sha256'],'documents':{p.name:{'sha256':sha(p),'bytes':p.stat().st_size}for p in review.iterdir()if p.is_file()and p.name!='RECIBO-INTEGRIDADE.json'},'package':{'name':bundle.name,'sha256':sha(bundle),'bytes':bundle.stat().st_size,'entries':len(files)+1,'CRC':'passed','all_entry_SHA256':'passed','internal_integrity_manifest_sha256':sha(out/'MANIFESTO-INTEGRIDADE.json')},'verification':{'HTTP_checks':len(json.loads((out/'HTTP-QA.json').read_text('utf-8'))['checks']),'HTTP_requests':len(json.loads((out/'HTTP-QA.json').read_text('utf-8'))['events']),'compatibility_checks':13,'preservation_checks':184,'raw_unrelated_matrix_rows_preserved':3395,'all_matrix_states_and_criteria_preserved':True,'types':'passed','PHP_lint':9,'build_files':162,'active_sources_and_build_identical':982,'active_config_locks_identical':3,'active_DB_connections':0},'limits':['600/579 and normative edition open','editorial model only','canonical approval preconditions synthetic','legacy business edit transactional; no unavailable legacy payload recovery','no volumetric test','partial compatible rollback retains additive schema and seven functional sources'],'active_authorized':False,'active_actions_executed':0,'reviewer_independent_ZIP_inspection':'not asserted'}
(review/'RECIBO-INTEGRIDADE.json').write_text(json.dumps(receipt,ensure_ascii=False,indent=2)+'\n','utf-8')
print(json.dumps({'package':receipt['package'],'receipt_sha256':sha(review/'RECIBO-INTEGRIDADE.json'),'matrix_raw_preserved':count},ensure_ascii=False))