Files
WoW-Hybrid-gateway/ops/content_candidate_rehearsal.py

25 lines
2.5 KiB
Python

"""Forward/inverse verification in an isolated snapshot; never installs client files."""
import hashlib,json,os,shutil,tempfile
from pathlib import Path
def digest(path):
with path.open('rb') as stream:return hashlib.file_digest(stream,'sha256').hexdigest()
def load(path):return json.loads(path.read_text())
def rehearse(root):
content=load(root/'physical-content.json');plan_path=Path(content['stage_plan']);assert digest(plan_path)==content['stage_plan_sha256'];out=plan_path.parent;plan=load(plan_path)
assert plan['state']=='OFFLINE_UNINSTALLED' and plan['publication']=='HOLD' and not plan['native_runtime_verified']
parent=Path(load(root/'current-content-parent-path.json')[0])
with tempfile.TemporaryDirectory(prefix='content-rehearsal-',dir=root) as directory:
snapshot=Path(directory);initial={}
for guard in plan['preconditions']['existing_exact_sha256']:
source=out/guard['rollback_artifact'] if 'rollback_artifact' in guard else parent/'physical'/guard['target']
assert digest(source)==guard['sha256'];dest=snapshot/guard['target'];dest.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(source,dest);initial[guard['target']]=guard['sha256']
for row in plan['forward']['copy_verified_files']:
source=out/row['artifact'];assert source.stat().st_size==row['bytes'] and digest(source)==row['sha256'];dest=snapshot/row['target'];assert not dest.exists();dest.parent.mkdir(parents=True,exist_ok=True);os.link(source,dest)
activation=plan['forward']['final_activation'];dest=snapshot/activation['target'];assert digest(dest)==activation['parent_sha256'];shutil.copyfile(out/activation['file'],dest);assert digest(dest)==activation['child_sha256']
inverse=plan['inverse'];first=inverse['first_restore'];assert digest(snapshot/first['target'])==first['precondition_sha256'];shutil.copyfile(out/first['artifact'],snapshot/first['target']);assert digest(snapshot/first['target'])==first['postcondition_sha256']
for row in inverse['then_remove_created_only_if_sha256_matches']:
dest=snapshot/row['target'];assert digest(dest)==row['sha256'];dest.unlink()
final={str(p.relative_to(snapshot)):digest(p) for p in snapshot.rglob('*') if p.is_file()};assert final==initial
(root/'physical-rehearsal.json').write_text(json.dumps({'parent_files':len(initial),'created_files':len(plan['forward']['copy_verified_files']),'forward_activation_verified':True,'inverse_exact':True,'installed':False},indent=2)+'\n')