"""发布包的轻量启动器。 启动器不联网、不更新文件,只负责从发布根目录启动 ``app/CMAutoBuy.exe``。 """ from __future__ import annotations import ctypes import subprocess import sys from pathlib import Path def install_root(executable: str | None = None) -> Path: """返回 Launcher.exe 所在的发布根目录。""" return Path(executable or sys.executable).resolve().parent def app_executable(root: Path) -> Path: """返回主程序路径。""" return root / "app" / "CMAutoBuy.exe" def show_error(message: str) -> None: """使用 Windows 原生对话框显示启动错误。""" ctypes.windll.user32.MessageBoxW(0, message, "商品采集采购工具", 0x10) def main() -> int: """启动主程序;找不到程序时返回非零退出码。""" root = install_root() target = app_executable(root) if not target.is_file(): show_error( "主程序不存在,请确认 app 文件夹完整。\n\n" f"缺少文件:{target}" ) return 1 try: subprocess.Popen([str(target)], cwd=str(target.parent)) except OSError as exc: show_error(f"主程序启动失败。\n\n{exc}") return 1 return 0 if __name__ == "__main__": raise SystemExit(main())