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

40 lines
4.5 KiB
Python

from pathlib import Path
import hashlib,json,zipfile,datetime
root=Path(__file__).resolve().parents[2];out=Path(__file__).resolve().parent
release=root/'outputs/investmoz-referencias-economicas-QA-20261016'
review=release/'para-revisao';old=root/'outputs/investmoz-negocios-classificacao-ativacao-20261016'
sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
m=json.loads((review/'MANIFESTO-ENTREGA.json').read_text('utf-8'))
candidate='referencias-economicas-QA-966202f15577'
assert m['candidate']==candidate
archive=release/(candidate+'.zip')
assert sha(archive)=='2dd2b205abe34a9d986cc100bb6b5a3755b230dcaaded151e251375bbded1dc8'
with zipfile.ZipFile(archive) as z:
assert z.testzip() is None and len(z.namelist())==377
for item in m['application']+m['build']+m['private_originals']:
assert hashlib.sha256(z.read('payload/'+item['path'])).hexdigest()==item['sha256']
base=json.loads((release/'ATIVO-ANTES.json').read_text('utf-8'))
for n,h in {**base['files'],**base['protected']}.items():assert sha(root/n)==h,n
for i in m['application']:assert (sha(root/i['path']) if (root/i['path']).exists() else None)==i['before_sha256'],i['path']
assert len([i for i in m['application'] if i['before_sha256']])==7
assert len([i for i in m['application'] if i['before_sha256'] is None])==6
assert not (out/'AUTORIZACAO-TEMPO.json').exists()
a={'at':datetime.datetime.now(datetime.timezone.utc).isoformat(),'candidate':candidate,'decision_record':'Mensagem expressa do responsável Tempo, 16/10/2026: pode avançar. esta autorizado. Referida à proposta concreta conferida neste turno.','code_hash':m['code_hash'],'manifest_sha256':m['data_manifest_sha256'],'package_sha256':sha(archive),'operations':['schema','import','publish','withdraw'],'scope':{'application_artifacts':13,'compiled_files':162,'references':15,'private_originals':4},'limits':['No other migrations, masters, business classifications, grants or flags','Four originals remain private','600/579 discrepancy open; chain editorial only','No accepted journeys repeated','Compatible partial rollback preserves new data after use']}
(out/'AUTORIZACAO-TEMPO.json').write_text(json.dumps(a,ensure_ascii=False,indent=2)+'\n','utf-8')
(out/'PREFLIGHT-FICHEIROS.json').write_text(json.dumps({'candidate':candidate,'files_compared':len(base['files']),'protected_compared':len(base['protected']),'previous_source_hashes':7,'absences':6,'package_entries':377,'package_sha256':sha(archive),'passed':True},indent=2)+'\n','utf-8')
text=(old/'fresh-backup.php').read_text('utf-8')
text=text.replace('outputs/investmoz-negocios-classificacao-QA-20261016','outputs/investmoz-referencias-economicas-QA-20261016').replace("$release.'/MANIFESTO.json'","$release.'/para-revisao/MANIFESTO-ENTREGA.json'")
text=text.replace("$manifest['sources']","$manifest['application']")
text=text.replace('negocios-before-','economic-references-before-').replace('negocios_before_','economic_refs_before_')
insert="""
$state=['ledger'=>(int)$source->query("SELECT count(*) FROM migrations WHERE migration='2026_10_16_120000_extend_economic_reference_hierarchy'")->fetchColumn(),'old_unique'=>(int)$source->query("SELECT count(*) FROM pg_constraint WHERE conname='economic_activities_sector_uq'")->fetchColumn(),'metadata'=>(int)$source->query("SELECT count(*) FROM information_schema.columns WHERE table_name='economic_activities' AND column_name='metadata'")->fetchColumn(),'nullable'=>$source->query("SELECT is_nullable FROM information_schema.columns WHERE table_name='economic_activities' AND column_name='sector_id'")->fetchColumn(),'new_index'=>(int)$source->query("SELECT count(*) FROM pg_indexes WHERE indexname='economic_activities_sector_idx'")->fetchColumn()];
if($state!==['ledger'=>0,'old_unique'=>1,'metadata'=>0,'nullable'=>'NO','new_index'=>0])throw new RuntimeException('Unexpected economic schema state; reconcile before delivery');
file_put_contents(__DIR__.'/PREFLIGHT-ESQUEMA.json',json_encode(['at'=>gmdate('c'),'read_only'=>true,'schema'=>$state],JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR).PHP_EOL);
"""
text=text.replace(" $env=Dotenv\\Dotenv::parse",insert+" $env=Dotenv\\Dotenv::parse")
(out/'fresh-backup.php').write_text(text,'utf-8')
text=(old/'boot-restored-clone.php').read_text('utf-8').replace('negocios-classificacao-QA-53311df2f59c',candidate).replace('negocios_before_','economic_refs_before_').replace('negocios-restore.test','economic-restore.test')
(out/'boot-restored-clone.php').write_text(text,'utf-8')
(out/'cache-snapshot.php').write_bytes((old/'cache-snapshot.php').read_bytes())
print(json.dumps({'candidate':candidate,'preflight':'passed','fresh_backup_scripts_prepared':True}))