Files
WoW-Hybrid-gateway/ops/item_armor_presentation.py
praxisandClaude Fable 5.2 8a24c445b0 Content pipeline and operator tools
367 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

75 lines
6.1 KiB
Python

"""Stage source-backed armor presentation for all published matching armor items."""
from pathlib import Path
import argparse,hashlib,json,subprocess
from item_sparse_compat import ROOT
from item_package_preflight import read_compatibility_package
from item_batch_stage import source_hash
from turtle_data import inventory_item_snapshot
def stage(package_path,output):
output=Path(output).resolve()
if not output.is_relative_to(ROOT/'runtime')or output.exists():raise ValueError('Fresh owned armor presentation stage required')
package,pin=read_compatibility_package(package_path)
source={r['identity'][1]:r['raw_fields']for r in inventory_item_snapshot(ROOT/'runtime/all-item-templates-20261017')['records']}
items={};excluded=[]
for row in package['rows']:
if row['table_name']!='ItemSparse'or row.get('record_status','valid')!='valid':continue
ident=row['id'];p=source.get(ident)
if p is None or p['class']!=4 or p['subclass']not in(1,2,3,4,6)or p['armor']<=0:continue
if row.get('source_record_sha256')!=source_hash(p):excluded.append(ident);continue
if any(type(p[k])is not int for k in('armor','block','subclass'))or not 0<p['armor']<1000000 or not 0<=p['block']<1000000:raise ValueError('Source armor presentation bounds differ')
items[ident]=dict(armor=p['armor'],block=p['block'],subclass=p['subclass'])
if not items:raise ValueError('Published exact source armor metadata required')
code='GWArmorPresentationData = {version=1,items={\n'+''.join('[%d]={armor=%d,block=%d,subclass=%d},\n'%(i,p['armor'],p['block'],p['subclass'])for i,p in sorted(items.items()))+'}}\n'
files={p.name:p.read_bytes()for p in(ROOT/'client_addons/WoWHybridArmorPresentation').iterdir()if p.is_file()};files['ArmorPresentationData.lua']=code.encode()
if set(files)!={'WoWHybridArmorPresentation.toc','ArmorPresentation.lua','ArmorPresentationData.lua'}:raise ValueError('Explicit armor addon files required')
for name,raw in files.items():
if name.endswith('.lua'):subprocess.run(['luac','-p','-'],input=raw,capture_output=True,check=True)
manifest=dict(kind='source-item-armor-tooltip-presentation-stage',version=1,package_sha256=pin,items=len(items),excluded_source_hash_mismatch=excluded,files={n:hashlib.sha256(v).hexdigest()for n,v in files.items()},installed=False,gameplay_verified=False)
output.mkdir()
for name,raw in files.items():(output/name).write_bytes(raw)
(output/'manifest.json').write_text(json.dumps(manifest,indent=2)+'\n');return manifest
def install(stage_path,journal,previous=None):
from octo_icon_assets import private_icon_client
stage_path=Path(stage_path).resolve();journal=Path(journal).resolve()
if not stage_path.is_relative_to(ROOT/'runtime')or not journal.is_relative_to(ROOT/'runtime')or journal.exists():raise ValueError('Owned stage/new journal required')
client,identity=private_icon_client();m=json.loads((stage_path/'manifest.json').read_text())
if m.get('kind')!='source-item-armor-tooltip-presentation-stage'or set(m['files'])!={'WoWHybridArmorPresentation.toc','ArmorPresentation.lua','ArmorPresentationData.lua'}:raise ValueError('Explicit armor presentation stage required')
_,published_pin=read_compatibility_package(ROOT/'docs/evidence/data/item-sparse-compat/package.json')
if m['package_sha256']!=published_pin:raise ValueError('Published item package differs from armor stage')
target=client/'Interface/AddOns/WoWHybridArmorPresentation'
if any(p.is_symlink()for p in(target,target.parent,target.parent.parent,target.parent.parent.parent)):raise ValueError('Linked addon preserved')
backup=None
if target.exists():
if previous is None:raise ValueError('Existing addon preserved; explicit owned update required')
previous=Path(previous).resolve()
if not previous.is_relative_to(ROOT/'runtime'):raise ValueError('Owned previous armor journal required')
old=json.loads(previous.read_text());actual={p.name:hashlib.sha256(p.read_bytes()).hexdigest()for p in target.iterdir()if p.is_file()and not p.is_symlink()}
if old.get('kind')!=m['kind']or old.get('target')!=str(target)or actual!=old.get('files')or len(list(target.iterdir()))!=len(actual):raise ValueError('Changed armor addon preserved')
backup=ROOT/'recovery'/('armor-presentation-'+stage_path.name)
elif previous is not None:raise ValueError('Previous armor addon installation missing')
files={}
for name,pin in m['files'].items():
p=stage_path/name
if p.is_symlink():raise ValueError('Linked source rejected')
raw=p.read_bytes()
if hashlib.sha256(raw).hexdigest()!=pin:raise ValueError('Staged armor bytes differ')
if name.endswith('.lua'):subprocess.run(['luac','-p','-'],input=raw,capture_output=True,check=True)
files[name]=raw
if backup is not None:
import shutil
backup.mkdir();shutil.copytree(target,backup/'previous-addon');shutil.copy2(previous,backup/'previous-install.json')
report=dict(m,target=str(target),client_identity=identity,previous_backup=None if backup is None else str(backup.relative_to(ROOT)),status='PREPARED',rollback='Close owned client; verify addon hashes against this journal, then move only this removable addon into recovery.'if backup is None else'Close owned client; verify current addon hashes, then restore previous-addon from previous_backup.')
journal.write_text(json.dumps(report,indent=2)+'\n');target.mkdir(exist_ok=backup is not None)
for name in('ArmorPresentationData.lua','ArmorPresentation.lua','WoWHybridArmorPresentation.toc'):
temporary=target/(name+'.new')
with temporary.open('xb')as stream:stream.write(files[name])
temporary.replace(target/name)
report.update(installed=True,status='NATIVE_VERIFICATION_PENDING');journal.write_text(json.dumps(report,indent=2)+'\n');return report
if __name__=='__main__':
p=argparse.ArgumentParser(description=__doc__);group=p.add_mutually_exclusive_group(required=True);group.add_argument('--package',type=Path);group.add_argument('--install-stage',type=Path);p.add_argument('--previous-install',type=Path);p.add_argument('--output',type=Path,required=True);args=p.parse_args()
r=stage(args.package,args.output)if args.package else install(args.install_stage,args.output,args.previous_install)
print(json.dumps({k:r[k]for k in('kind','items','installed')}))