42 lines
3.0 KiB
Python
42 lines
3.0 KiB
Python
from pathlib import Path
|
|
import datetime,hashlib,json,os,shutil,subprocess,sys
|
|
root=Path(__file__).resolve().parents[2];out=Path(__file__).resolve().parent
|
|
release=root/'outputs/investmoz-negocios-classificacao-QA-20261016'
|
|
assert sys.argv[1:]==['--execute-authorized-reversal'], 'Explicit execution argument required'
|
|
m=json.loads((release/'MANIFESTO.json').read_text('utf-8'));b=json.loads((out/'FRESH-BACKUP.json').read_text('utf-8'))
|
|
assert m['candidate']==b['candidate']=='negocios-classificacao-QA-53311df2f59c'
|
|
backup=Path(b['private_directory']).resolve();assert backup.parent==Path('C:/Users/macbook/.config/investmoz').resolve()
|
|
sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
|
|
assert not (root/'storage/framework/down').exists() and not (out/'REVERSAO-EXECUTADA.json').exists()
|
|
for s in m['sources']:assert sha(root/s['path'])==s['after_sha256']
|
|
for s in m['build']:assert sha(root/s['path'])==s['sha256']
|
|
stage=(backup/'reversal-staging').resolve();assert stage.is_relative_to(backup) and not stage.exists()
|
|
stage.mkdir()
|
|
# Copy, never move/delete, the private backup. The candidate build is retained.
|
|
shutil.copytree(backup/'files/public/build',stage/'build')
|
|
for f in (backup/'files/public/build').rglob('*'):
|
|
if f.is_file():assert sha(f)==sha(stage/'build'/f.relative_to(backup/'files/public/build'))
|
|
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/('reversal-'+label+'.log')).write_bytes(p.stdout+p.stderr);assert p.returncode==0,label
|
|
def artisan(action):command([php,'artisan',action],action.replace(':','-'))
|
|
def put(src,dest):
|
|
assert dest.resolve().is_relative_to(root.resolve())
|
|
temp=dest.with_name(dest.name+'.reversal.tmp');assert not temp.exists();shutil.copy2(src,temp);assert sha(temp)==sha(src);os.replace(temp,dest)
|
|
build=(root/'public/build').resolve();saved=(backup/'candidate-build-preserved-after-reversal').resolve()
|
|
assert build==root.resolve()/'public/build' and saved.is_relative_to(backup) and not saved.exists()
|
|
artisan('down')
|
|
command([php,str(out/'rollback-cache-snapshot.php'),'before'],'cache-before')
|
|
for s in m['sources']:
|
|
if s['operation']=='replace':
|
|
src=backup/'files'/s['path'];assert sha(src)==s['before_sha256'];put(src,root/s['path'])
|
|
for s in m['sources']:
|
|
if s['operation']=='new':
|
|
p=(root/s['path']).resolve();assert p.is_relative_to(root.resolve()) and sha(p)==s['after_sha256'];p.unlink()
|
|
build.rename(saved);(stage/'build').rename(build)
|
|
for action in ['config:clear','route:clear','view:clear']:artisan(action)
|
|
command([php,str(out/'rollback-cache-snapshot.php'),'after'],'cache-after')
|
|
artisan('up')
|
|
proof={'candidate':m['candidate'],'at':datetime.datetime.now(datetime.timezone.utc).isoformat(),'sources_replaced':8,'new_sources_removed_by_hash':3,'previous_build_restored':True,'database_restored_or_changed':False,'new_classification_revisions_preserved':True,'data_cache_preserved':True,'maintenance_ended':True}
|
|
(out/'REVERSAO-EXECUTADA.json').write_text(json.dumps(proof,indent=2)+'\n','utf-8');print(json.dumps(proof))
|