Files
Investidor-V2/outputs/investmoz-promotor-contexto-ativacao-20261015/deliver.py
T
nala 6a76aac80f
tests / ci (push) Canceled after 0s
versao compilada 1
2026-10-16 09:17:28 +02:00

56 lines
4.2 KiB
Python

from pathlib import Path
import hashlib,json,zipfile,datetime,subprocess,shutil,os
root=Path(__file__).resolve().parents[2];out=Path(__file__).resolve().parent
release=root/'outputs/investmoz-promotor-contexto-QA-20261015'
sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
m=json.loads((release/'MANIFESTO-DELTA.json').read_text('utf-8'));r=json.loads((release/'RECIBO-INTEGRIDADE.json').read_text('utf-8'))
b=json.loads((out/'FRESH-BACKUP.json').read_text('utf-8'));c=json.loads((out/'ARRANQUE-CLONE-RESTAURADO.json').read_text('utf-8'))
assert all(x['candidate']==m['candidate'] for x in [r,b,c])
assert b['restore_verified'] and b['files_verified'] and c['DB_read_only'] and c['kernel_bootstrap']=='passed'
assert (datetime.datetime.now(datetime.timezone.utc)-datetime.datetime.fromisoformat(b['at'])).total_seconds()<3600
assert not (out/'TROCA.json').exists() and not (root/'storage/framework/down').exists()
backup=Path(b['private_directory']);private=json.loads((backup/'backup-proof-private.json').read_text('utf-8'))
assert sha(Path(private['dump_path']))==private['sha256'] and sha(Path(private['files_path']))==private['files_sha256']
baseline=json.loads((release/'ATIVO-ANTES.json').read_text('utf-8'))
for n,h in {**baseline['files'],**baseline['protected']}.items():assert sha(root/n)==h,n
for s in m['sources']:
p=root/s['path'];assert (sha(p) if p.is_file() else None)==s['previous_sha256'],s['path']
archive=root/'outputs'/r['zip']['name'];assert sha(archive)==r['zip']['sha256']
stage=backup/'candidate-staging';assert not stage.exists();stage.mkdir()
with zipfile.ZipFile(archive) as z:
assert z.testzip() is None
for s in m['sources']:
data=z.read('fontes/'+s['path']);assert hashlib.sha256(data).hexdigest()==s['candidate_sha256']
p=stage/s['path'];p.parent.mkdir(parents=True,exist_ok=True);p.write_bytes(data)
php='C:/laragon/bin/php/php-8.5.11-Win32-vs17-x64/php.exe'
def command(args,label):
p=subprocess.run(args,cwd=root,capture_output=True);(backup/(label+'.log')).write_bytes(p.stdout+p.stderr)
assert p.returncode==0,'Command failed: '+label
def artisan(*args):command([php,'artisan',*args],'-'.join(args).replace(':','-'))
def put(src,dest):
dest.parent.mkdir(parents=True,exist_ok=True);tmp=dest.with_name(dest.name+'.promoter-delivery.tmp');assert not tmp.exists()
shutil.copy2(src,tmp);assert sha(tmp)==sha(src);os.replace(tmp,dest)
for s in m['sources']:
if s['path'].endswith('.php') and not s['path'].endswith('.blade.php'):command([php,'-l',str(stage/s['path'])],'syntax-'+Path(s['path']).stem)
receipt={'at':datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=2))).isoformat(),'candidate':m['candidate'],'source_count':3,'build_changes':0,'migrations':0,'grants_changed':0,'business_writes':0,'DOC01_reinstalled':False,'accepted_tests_repeated':False,'maintenance_started':False,'maintenance_ended':False}
try:
artisan('down','--retry=60');receipt['maintenance_started']=True
command([php,str(out/'cache-snapshot.php'),'before'],'data-cache-before')
for s in m['sources']:
p=root/s['path'];assert (sha(p) if p.is_file() else None)==s['previous_sha256']
put(stage/s['path'],p);assert sha(p)==s['candidate_sha256']
artisan('view:clear');receipt['cache_commands']=['view:clear']
command([php,str(out/'cache-snapshot.php'),'after'],'data-cache-after');receipt['data_cache_preserved']=True
for n,h in {**baseline['files'],**baseline['protected']}.items():
if n not in [s['path'] for s in m['sources']]:assert sha(root/n)==h,n
artisan('up');receipt['maintenance_ended']=True;receipt['result']='Three sources installed; authenticated editor observation pending'
except BaseException:
for s in m['sources']:
if s['previous_sha256'] is not None:put(backup/'files'/s['path'],root/s['path'])
for s in m['sources']:
p=root/s['path']
if s['previous_sha256'] is None and p.is_file() and sha(p)==s['candidate_sha256']:p.unlink()
artisan('view:clear');artisan('up');receipt['result']='Reverted to backup sources; database preserved'
(out/'TROCA-REVERTIDA.json').write_text(json.dumps(receipt,indent=2)+'\n','utf-8');raise
(out/'TROCA.json').write_text(json.dumps(receipt,indent=2)+'\n','utf-8');print(json.dumps(receipt))