"""Commands that produce deployable files. wtotools build-gui --stock --out [--client client] [--force] Inputs, all under the client folder: - overrides/ edit records for stock files (see wtotools.build.overrides) - gui/ our own files, laid out exactly as their path inside gui.vromfs.bin The stock archive is only read. The output must be a different file, and an existing output is replaced only with --force. """ from __future__ import annotations import argparse import sys from pathlib import Path from ..build import BuildError, OverrideError, build_script_archive, load_overrides from ..vromfs.errors import VromfsFormatError GUI_ARCHIVE = "gui.vromfs.bin" DEFAULT_CLIENT = Path("client") def register(commands: argparse._SubParsersAction) -> None: gui = commands.add_parser("build-gui", help="build gui.vromfs.bin from the stock one plus the client folder") gui.add_argument("--stock", type=Path, required=True, help="the stock gui.vromfs.bin to read") gui.add_argument("--out", type=Path, required=True, help="where to write the built archive") gui.add_argument("--client", type=Path, default=DEFAULT_CLIENT, help="folder with overrides/ and gui/") gui.add_argument("--force", action="store_true", help="overwrite an existing output file") gui.set_defaults(handler=_build_gui) def _build_gui(args: argparse.Namespace) -> int: if args.out.resolve() == args.stock.resolve(): print("refusing to write over the stock archive; choose a different --out", file=sys.stderr) return 2 if args.out.exists() and not args.force: print(f"{args.out} already exists; pass --force to overwrite", file=sys.stderr) return 2 try: overrides = load_overrides(args.client / "overrides") added = _added_files(args.client / "gui") built, report = build_script_archive(GUI_ARCHIVE, args.stock.read_bytes(), overrides, added) except (BuildError, OverrideError, VromfsFormatError) as error: print(f"build failed, nothing was written: {error}", file=sys.stderr) return 1 args.out.parent.mkdir(parents=True, exist_ok=True) args.out.write_bytes(built) print(report.summary) for path in report.overridden: print(f" edited {path}") for path in report.added: print(f" added {path}") print(f"wrote {args.out}") return 0 def _added_files(root: Path) -> dict[str, bytes]: if not root.is_dir(): return {} return {path.relative_to(root).as_posix(): path.read_bytes() for path in sorted(root.rglob("*")) if path.is_file()}