"""One native DOCX-to-ODT policy entry for validated static Word editable exports."""
from pathlib import Path
import gzip,hashlib,json,shutil
root=Path.cwd();base=root/'third_party/office/26.2-fc03117-doc4';out=root/'third_party/office/26.2-fc03117-doc5';assert not out.exists()
pin=lambda b:dict(bytes=len(b),sha256=hashlib.sha256(b).hexdigest())
old=gzip.decompress((base/'runtime/office-combined.js.gz').read_bytes());assert pin(old)==json.loads((base/'runtime-integrity.json').read_text())['office-combined.js']
a=b"docx:['rtf','doc']";b=b"docx:['rtf','doc','odt']";assert old.count(a)==1;new=old.replace(a,b)
shutil.copytree(base/'sources',out/'sources');(out/'runtime').mkdir()
header=out/'sources/tree/benchmarks/research/office-wasm/scripts/office-cli-header.js';data=header.read_bytes();assert data.count(a)==1;header.write_bytes(data.replace(a,b))
(out/'runtime/office-combined.js.gz').write_bytes(gzip.compress(new,compresslevel=9,mtime=0));shutil.copy2(base/'MPL-2.0.txt',out/'MPL-2.0.txt')
(out/'NOTICE.txt').write_text('Tabmorph modified LibreOffice adapter doc5, MPL-2.0 subset. Exact doc4 runtime with one editable whitelist entry: DOCX to ODT. This supports the validated static Word DOCM/DOTX editable exports. Native office23 wasm, fonts4, source/output preflight, network blocking, PDF defaults, parser, disposal, engine CSP and every other byte are unchanged. No new native compilation, external resources, uploads or dependencies. All original and prior modified sources/notices remain public and immutable.\n')
(out/'sources/BUILD.txt').write_text('Run scripts/package-word-editable-adapter.py in the complete Tabmorph checkout. Verify exact doc4 decoded pin; replace exactly one docx editable output array with the same array plus odt in both the generated combined worker and complete header source. Gzip9 mtime0. Native office23 and fonts4 unchanged. Do not modify published doc4 or its source. Full original sources and licences are in ../26.2-fc03117-office1/SOURCE.html. Retained sources/stage.json proves that reversing the single replacement yields exact doc4 bytes.\n')
shutil.copy2(root/'scripts/package-word-editable-adapter.py',out/'sources/tree/scripts/package-word-editable-adapter.py')
(out/'sources/stage.json').write_text(json.dumps({'before':pin(old),'after':pin(new),'needle':a.decode(),'replacement':b.decode(),'occurrences':1,'nativeWasmUnchanged':True,'engineCspUnchanged':True},indent=2)+'\n')
(out/'SOURCE.html').write_text('<!doctype html><meta charset="utf-8"><title>ODT editable adapter doc5</title><h1>ODT editable adapter doc5</h1><a href="NOTICE.txt">Notice</a> <a href="sources/BUILD.txt">Rebuild</a> <a href="sources/tree/benchmarks/research/office-wasm/scripts/office-cli-header.js">Complete changed header</a> <a href="sources/stage.json">Exact one-change proof</a> <a href="../26.2-fc03117-doc4/SOURCE.html">Previous adapter</a> <a href="../26.2-fc03117-office1/SOURCE.html">Complete original sources and licences</a> <a href="manifest.json">All hashes</a>')
runtime={'office-combined.js':pin(new)};(out/'runtime-integrity.json').write_text(json.dumps(runtime,indent=2)+'\n')
manifest={'version':out.name,'parentAdapterVersion':base.name,'baseSourceVersion':'26.2-fc03117-office1','nativeBinaryVersion':'26.2-fc03117-office23','fontDataVersion':'26.2-fc03117-fonts4','runtime':runtime,'files':{str(p.relative_to(out)):pin(p.read_bytes())for p in sorted(out.rglob('*'))if p.is_file()}}
(out/'manifest.json').write_text(json.dumps(manifest,indent=2)+'\n');assert new.replace(b,a)==old;print({'version':out.name,'runtime':runtime,'files':len(manifest['files'])})
