dosmqtt / src /build_assets.py
HelloSun's picture
Add DOS x Gradio demo: G.EXE asks a Gradio Space over HTTP; update dev package
a1e3a8f verified
Raw History Blame Contribute Delete
1.36 kB
#!/usr/bin/env python3
"""Pack the v86 runtime pieces and the DOS floppy as gzip+base64 JS in dist/.
HF static Spaces serve binary files through a CORS-less redirect, but text files
are served directly, so everything binary travels as base64 text."""
import base64, gzip, json, os, shutil
from pathlib import Path
root = Path(__file__).resolve().parent.parent
deps = Path(os.environ.get("DEPS", root / "deps"))
dist = root / "dist"; dist.mkdir(exist_ok=True)
parts = {"wasm": deps / "v86/v86.wasm", "bios": deps / "v86/seabios.bin",
"vga": deps / "v86/vgabios.bin", "img": root / "build/dosmqtt.img"}
assets = {}
for key, path in parts.items():
raw = path.read_bytes(); z = gzip.compress(raw, 9, mtime=0)
assets[key] = base64.b64encode(z).decode()
print(f"{key:5} {len(raw):>8} -> gzip {len(z):>8} -> base64 {len(assets[key]):>8}")
(dist / "assets.b64.js").write_text("window.DOSMQTT_ASSETS=" + json.dumps(assets) + ";\n")
# The Gradio page reuses wasm/bios/vga from assets.b64.js and only adds its own floppy.
gz = gzip.compress((root / "build/gradio.img").read_bytes(), 9, mtime=0)
(dist / "gradio-img.b64.js").write_text("window.DOSGRADIO_IMG=" + json.dumps(base64.b64encode(gz).decode()) + ";\n")
print(f"gradio floppy gzip {len(gz)}")
shutil.copy(deps / "v86/libv86.js", dist / "libv86.js")
print("wrote", dist / "assets.b64.js")