"""Pre-installation reversal from validated snapshots. Never undo SQL or erase immutable assets.""" from pathlib import Path from datetime import datetime,timezone import hashlib,json,os ROOT=Path(__file__).resolve().parent.parent DIR=ROOT/'storage/app/private/investmoz-v2/portal-audit' read=lambda p:json.loads(Path(p).read_text(encoding='utf-8-sig')) sha=lambda p:hashlib.sha256(Path(p).read_bytes()).hexdigest() stage=read(DIR/'country-https-active-stage-latest.json') check=read(DIR/'country-https-active-check-before-apply-20261011.json') assert check['result']=='passed' and check['phase']=='before-apply' and not check['country_enabled'] assert check['installed_packages']==[] and check['columns']==[] and check['installation_audits']==0 assert 0<=(datetime.now(timezone.utc)-datetime.fromisoformat(check['at'])).total_seconds()<180 assert not (ROOT/'bootstrap/cache/config.php').exists() manifest=read(ROOT/'outputs/investmoz-country02-https-20261011/manifesto-candidata.json') for name,digest in manifest['files'].items():assert sha(ROOT/name)==digest assert sha(ROOT/'public/build/manifest.json')==manifest['production']['manifest_sha256'] allowed={str(ROOT/name) for name in manifest['changed_source_files']} allowed|={str(ROOT/'.env'),str(ROOT/'public/build/manifest.json'), str(ROOT/'storage/app/private/investmoz-v2/organization-country-installation/readiness.json')} cfg=read(ROOT/'outputs/investmoz-country02-https-20261011/configuracao-ativa-leitura.json') allowed.add(str(Path(cfg['active_vhost']['path']))) assert sha(cfg['active_vhost']['path'])==stage['active_vhost_sha256'] records=stage['backup_files'] for target,entry in records.items():assert sha(entry['backup'])==entry['sha256'] restored=[] for target,entry in records.items(): if target not in allowed:continue file=Path(target);temp=file.with_name(file.name+'.country02-reversal-new') assert not temp.exists();temp.write_bytes(Path(entry['backup']).read_bytes());os.replace(temp,file) assert sha(file)==entry['sha256'];restored.append(target) assert sha(ROOT/'public/build/manifest.json')==cfg['active_manifest_sha256'] result={'result':'pre_installation_sources_bundle_env_vhost_restored','at':datetime.now(timezone.utc).isoformat(), 'restored_files':restored,'active_SQL_writes':0,'installation_performed':False, 'immutable_assets_and_TLS_files_preserved':True,'certificate_trust_not_removed_automatically':True, 'Apache_reload_if_needed':'Use the normal Laragon Apache control; do not terminate processes'} (DIR/'country-https-active-reversal-20261011.json').write_text(json.dumps(result,indent=2),encoding='utf-8') print(json.dumps({'result':result['result'],'restored_file_count':len(restored),'active_SQL_writes':0,'installation_performed':False}))