from pathlib import Path import json,hashlib,subprocess,os from datetime import datetime,timezone R=Path(__file__).resolve().parent;ROOT=R.parents[1];sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest() b=json.loads((R/'B-activation-fresh-backup-20261015.json').read_text(encoding='utf-8'));a=json.loads((R/'B-activation-Tempo-authorization-20261015.json').read_text(encoding='utf-8'));assert a['user_statement']=='A Tempo autoriza' and b['restore_verified']and b['files_verified'] private=Path(b['private_directory']);stage=private/'candidate-staging';mapping=json.loads((R/'B-suplemento-C137-sequencia-final-20261015/MAPA-DISPONIBILIZACAO-REVERSAO.json').read_text(encoding='utf-8'));ops=mapping['source_operations'];assert len(ops)==36 prior=json.loads((R/'B-destination-preflight-readonly-20261015.json').read_text(encoding='utf-8')) assert not (ROOT/'storage/framework/down').exists() for op in ops:assert sha(ROOT/op['path'])==op['before'] and sha(stage/op['path'])==op['after'] for n,h in prior['current_build_files'].items():assert sha(ROOT/'public/build'/n)==h assert sha(ROOT/'.env')==sha(private/'files/.env') build={p.relative_to(stage/'public/build').as_posix():p for p in (stage/'public/build').rglob('*')if p.is_file()} for n,p in build.items(): t=ROOT/'public/build'/n assert not t.exists()or sha(t)==sha(p)or n in ['manifest.json','fonts-manifest.json'],'Asset collision '+n php='C:/laragon/bin/php/php-8.5.11-Win32-vs17-x64/php.exe' def artisan(*args):subprocess.run([php,'artisan',*args],cwd=ROOT,check=True,capture_output=True) def put(p,data): p.parent.mkdir(parents=True,exist_ok=True) with p.open('wb')as f:f.write(data);f.flush();os.fsync(f.fileno()) receipt={'at':datetime.now(timezone.utc).isoformat(),'candidate':a['candidate'],'C137':True,'sources':{},'added_assets':[],'path_flag_enabled':False,'maintenance_started':False,'maintenance_ended':False,'190000_installed':False,'no_QA_data_imported':True} try: artisan('down','--retry=60');receipt['maintenance_started']=True assert (ROOT/'storage/framework/down').exists() for op in ops: n=op['path'];put(ROOT/n,(stage/n).read_bytes());assert sha(ROOT/n)==op['after'];receipt['sources'][n]=op['after'] for n,p in sorted(build.items()): if n=='manifest.json':continue t=ROOT/'public/build'/n if not t.exists():receipt['added_assets'].append(n) if not t.exists()or sha(t)!=sha(p):put(t,p.read_bytes()) assert sha(t)==sha(p) put(ROOT/'public/build/manifest.json',(stage/'public/build/manifest.json').read_bytes()) assert sha(ROOT/'public/build/manifest.json')==sha(stage/'public/build/manifest.json') assert sha(ROOT/'.env')==sha(private/'files/.env') assert sha(ROOT/'config/inertia.php')==sha(private/'files/config/inertia.php') artisan('view:clear');artisan('up');receipt['maintenance_ended']=True assert not (ROOT/'storage/framework/down').exists() receipt['result']='sources_and_build_delivered; path still disabled; authenticated190000 pending' except Exception: for op in ops: n=op['path'];put(ROOT/n,(private/'files'/n).read_bytes()) for n in ['manifest.json','fonts-manifest.json']: if(private/'files/public/build'/n).exists():put(ROOT/'public/build'/n,(private/'files/public/build'/n).read_bytes()) artisan('view:clear');artisan('up');receipt['result']='file_switch_reverted_before_installation';receipt['maintenance_ended']=True (R/'B-activation-file-switch-20261015.json').write_text(json.dumps(receipt,indent=2)+'\n');raise (R/'B-activation-file-switch-20261015.json').write_text(json.dumps(receipt,indent=2)+'\n') print(json.dumps({'result':receipt['result'],'sources':len(receipt['sources']),'added_assets':len(receipt['added_assets']),'maintenance_ended':True,'flag_unchanged':True}))