build.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. """Build script for the Bambuddy Windows installer.
  2. Stages all artifacts under ``installers/windows/build/staging/`` for the
  3. Inno Setup compiler to package. Run this on Windows (or in a Windows CI
  4. runner) — it pip-installs Bambuddy's deps against the embedded Python it
  5. downloads, which requires the matching platform.
  6. Steps:
  7. 1. Download python.org embeddable distribution for Windows x64
  8. 2. Configure embedded Python (allow site-packages)
  9. 3. Bootstrap pip into the embedded distribution
  10. 4. Install ``requirements.txt`` into the embedded Python
  11. 5. Build the React frontend (``frontend/npm run build``)
  12. 6. Stage backend source + frontend bundle
  13. 7. Download NSSM
  14. 8. Download ffmpeg static build for Windows
  15. 9. Print "ready for ISCC" message
  16. After this script succeeds, run::
  17. "C:\\Program Files (x86)\\Inno Setup 6\\ISCC.exe" bambuddy.iss
  18. to produce the final installer .exe under ``build/output/``.
  19. """
  20. from __future__ import annotations
  21. import argparse
  22. import os
  23. import shutil
  24. import subprocess
  25. import sys
  26. import urllib.request
  27. import zipfile
  28. from pathlib import Path
  29. # Repo root: installers/windows/build.py -> ../../
  30. REPO_ROOT = Path(__file__).resolve().parents[2]
  31. INSTALLER_DIR = Path(__file__).resolve().parent
  32. BUILD_DIR = INSTALLER_DIR / "build"
  33. STAGING = BUILD_DIR / "staging"
  34. DOWNLOADS = BUILD_DIR / "downloads"
  35. # Python 3.13 — matches Dockerfile (python:3.13-slim-trixie). Bump when
  36. # the Dockerfile bumps; the Windows installer should track production.
  37. PYTHON_VERSION = "3.13.1"
  38. PYTHON_EMBED_URL = f"https://www.python.org/ftp/python/{PYTHON_VERSION}/python-{PYTHON_VERSION}-embed-amd64.zip"
  39. # NSSM 2.24 is the long-time stable build (no new release since 2014).
  40. # Vendored under installers/windows/vendor/nssm.exe rather than fetched
  41. # at build time — nssm.cc has flaked with 503s mid-CI-run before, and
  42. # pinning to a checked-in binary makes builds reproducible and lets us
  43. # inspect the binary in PRs if it ever needs updating. SHA-256:
  44. # f689ee9af94b00e9e3f0bb072b34caaf207f32dcb4f5782fc9ca351df9a06c97
  45. NSSM_VERSION = "2.24"
  46. # ffmpeg static build. BtbN's gyan-equivalent build is the most reliable
  47. # automated source. Pin to a release tag so builds are reproducible.
  48. FFMPEG_URL = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip"
  49. # get-pip.py for bootstrapping pip into the embedded distribution
  50. GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py"
  51. def log(msg: str) -> None:
  52. print(f"[build] {msg}", flush=True)
  53. def download(url: str, dest: Path) -> Path:
  54. """Download ``url`` to ``dest`` if not already present."""
  55. if dest.exists():
  56. log(f"already downloaded: {dest.name}")
  57. return dest
  58. dest.parent.mkdir(parents=True, exist_ok=True)
  59. log(f"downloading {url}")
  60. with urllib.request.urlopen(url) as resp, open(dest, "wb") as f: # noqa: S310 — pinned URLs
  61. shutil.copyfileobj(resp, f)
  62. return dest
  63. def unzip(zip_path: Path, dest: Path) -> None:
  64. log(f"unzipping {zip_path.name} -> {dest}")
  65. dest.mkdir(parents=True, exist_ok=True)
  66. with zipfile.ZipFile(zip_path) as zf:
  67. zf.extractall(dest)
  68. def stage_embedded_python() -> Path:
  69. """Download and configure the embedded Python distribution."""
  70. target = STAGING / "python"
  71. if target.exists():
  72. shutil.rmtree(target)
  73. zip_path = download(
  74. PYTHON_EMBED_URL,
  75. DOWNLOADS / f"python-{PYTHON_VERSION}-embed-amd64.zip",
  76. )
  77. unzip(zip_path, target)
  78. # Edit pythonXY._pth to allow site-packages. The embedded distribution
  79. # ships with `import site` commented out — uncomment it so pip-installed
  80. # packages in Lib\site-packages are importable.
  81. pth_files = list(target.glob("python3*._pth"))
  82. if not pth_files:
  83. raise RuntimeError(f"no python3*._pth file found in {target}")
  84. pth = pth_files[0]
  85. content = pth.read_text()
  86. content = content.replace("#import site", "import site")
  87. # Also add Lib\site-packages explicitly. The embedded distribution
  88. # doesn't include this path by default even with `import site` enabled.
  89. if "Lib\\site-packages" not in content and "Lib/site-packages" not in content:
  90. content = content.rstrip() + "\nLib\\site-packages\n"
  91. pth.write_text(content)
  92. # Bootstrap pip
  93. get_pip = download(GET_PIP_URL, DOWNLOADS / "get-pip.py")
  94. log("bootstrapping pip into embedded Python")
  95. subprocess.run(
  96. [str(target / "python.exe"), str(get_pip), "--no-warn-script-location"],
  97. check=True,
  98. )
  99. # Install setuptools + wheel. The embedded distribution ships without
  100. # them, and get-pip.py installs only pip — but pip needs
  101. # ``setuptools.build_meta`` (PEP 517 backend) to build any source-only
  102. # package. Bambuddy's requirements.txt hits this with pyftpdlib 2.2.0
  103. # which is sdist-only on PyPI; other source-only packages would fail
  104. # the same way without this step.
  105. log("installing setuptools + wheel for PEP 517 builds")
  106. subprocess.run(
  107. [
  108. str(target / "python.exe"),
  109. "-m",
  110. "pip",
  111. "install",
  112. "--no-warn-script-location",
  113. "setuptools",
  114. "wheel",
  115. ],
  116. check=True,
  117. )
  118. return target
  119. def install_requirements(python_dir: Path) -> None:
  120. """Install Bambuddy's requirements.txt into the embedded Python."""
  121. py = python_dir / "python.exe"
  122. requirements = REPO_ROOT / "requirements.txt"
  123. log(f"installing requirements.txt into {python_dir}")
  124. subprocess.run(
  125. [
  126. str(py),
  127. "-m",
  128. "pip",
  129. "install",
  130. "--no-warn-script-location",
  131. "-r",
  132. str(requirements),
  133. ],
  134. check=True,
  135. )
  136. def build_frontend() -> Path:
  137. """Run ``npm ci && npm run build`` and return the build output path.
  138. Vite is configured with ``outDir: '../static'`` (see
  139. ``frontend/vite.config.ts``), so the bundle lands at ``<repo>/static/``
  140. — NOT ``frontend/dist/``. The path matches the runtime expectation in
  141. ``backend/app/core/config.py`` (``static_dir = _app_dir / "static"``).
  142. """
  143. frontend = REPO_ROOT / "frontend"
  144. dist = REPO_ROOT / "static"
  145. log("running npm ci in frontend/")
  146. npm = shutil.which("npm")
  147. if not npm:
  148. raise RuntimeError("npm not found on PATH — install Node.js 22 LTS")
  149. subprocess.run([npm, "ci"], cwd=frontend, check=True, shell=False)
  150. log("running npm run build in frontend/")
  151. subprocess.run([npm, "run", "build"], cwd=frontend, check=True, shell=False)
  152. if not dist.exists():
  153. raise RuntimeError(f"expected frontend build output at {dist}")
  154. return dist
  155. def stage_backend(frontend_dist: Path) -> None:
  156. """Copy backend source + frontend bundle into the staging tree.
  157. The runtime layout under STAGING/app/ mirrors a Bambuddy checkout:
  158. ``backend/`` (source), ``static/`` (frontend bundle served by FastAPI).
  159. """
  160. app = STAGING / "app"
  161. if app.exists():
  162. shutil.rmtree(app)
  163. app.mkdir(parents=True)
  164. # Backend source — copy the package tree, skip caches/tests/migrations
  165. log("staging backend source")
  166. shutil.copytree(
  167. REPO_ROOT / "backend",
  168. app / "backend",
  169. ignore=shutil.ignore_patterns(
  170. "__pycache__",
  171. "*.pyc",
  172. "tests",
  173. ".pytest_cache",
  174. ),
  175. )
  176. # Frontend bundle — FastAPI's StaticFiles mounts from app/static.
  177. # Strip macOS metadata files (.DS_Store, ._.*) that the dev box leaks
  178. # in; they'd just bloat the installer and never be served anyway.
  179. log("staging frontend bundle")
  180. shutil.copytree(
  181. frontend_dist,
  182. app / "static",
  183. ignore=shutil.ignore_patterns(".DS_Store", "._*"),
  184. )
  185. # gcode_viewer/ is a vendored 3D-preview iframe served via explicit
  186. # routes in main.py (looked up via static_dir.parent / "gcode_viewer").
  187. # In the staged layout STAGING/app/static/'s sibling is STAGING/app/,
  188. # so place the directory next to static/ to match runtime resolution.
  189. gcode_viewer_src = REPO_ROOT / "gcode_viewer"
  190. if gcode_viewer_src.exists():
  191. log("staging gcode_viewer/")
  192. shutil.copytree(
  193. gcode_viewer_src,
  194. app / "gcode_viewer",
  195. ignore=shutil.ignore_patterns(".DS_Store", "._*"),
  196. )
  197. def stage_nssm() -> None:
  198. target = STAGING / "bin"
  199. target.mkdir(parents=True, exist_ok=True)
  200. # Vendored binary — no network fetch at build time
  201. src = INSTALLER_DIR / "vendor" / "nssm.exe"
  202. if not src.exists():
  203. raise RuntimeError(f"vendored NSSM binary missing at {src} — was it committed?")
  204. log(f"staging nssm.exe from {src}")
  205. shutil.copy(src, target / "nssm.exe")
  206. def stage_ffmpeg() -> None:
  207. target = STAGING / "bin"
  208. target.mkdir(parents=True, exist_ok=True)
  209. zip_path = download(FFMPEG_URL, DOWNLOADS / "ffmpeg-win64-gpl.zip")
  210. extract = DOWNLOADS / "ffmpeg-extracted"
  211. if not extract.exists():
  212. unzip(zip_path, extract)
  213. src = next(extract.rglob("bin/ffmpeg.exe"))
  214. log(f"staging ffmpeg.exe from {src}")
  215. shutil.copy(src, target / "ffmpeg.exe")
  216. # ffprobe is used by some camera/timelapse paths
  217. ffprobe = next(extract.rglob("bin/ffprobe.exe"), None)
  218. if ffprobe is not None:
  219. shutil.copy(ffprobe, target / "ffprobe.exe")
  220. def stage_service_scripts() -> None:
  221. """Copy the service install/uninstall .bat files into staging."""
  222. service_src = INSTALLER_DIR / "service"
  223. service_dst = STAGING / "service"
  224. if service_dst.exists():
  225. shutil.rmtree(service_dst)
  226. shutil.copytree(service_src, service_dst)
  227. def _read_app_version() -> str:
  228. """Read APP_VERSION from backend/app/core/config.py (the canonical
  229. source used by every other Bambuddy surface — FastAPI OpenAPI title,
  230. /system info, support bundles, spoolbuddy update check).
  231. """
  232. config_py = REPO_ROOT / "backend" / "app" / "core" / "config.py"
  233. if not config_py.exists():
  234. return "0.0.0+dev"
  235. for raw in config_py.read_text().splitlines():
  236. stripped = raw.strip()
  237. if stripped.startswith("APP_VERSION"):
  238. # APP_VERSION = "0.2.5b1" -> 0.2.5b1
  239. return stripped.split("=", 1)[1].strip().strip('"').strip("'")
  240. return "0.0.0+dev"
  241. def _resolve_installer_version() -> str:
  242. """Decide what version string the installer carries.
  243. Priority:
  244. 1. ``GITHUB_REF`` env var when set to a tag (e.g.
  245. ``refs/tags/v0.2.5b1-daily.20260610``) — the daily-beta and stable
  246. publish scripts both push tags in the ``v<APP_VERSION>[-daily.<date>]``
  247. shape, and we want the installer filename + Inno Setup AppVersion
  248. to match the GitHub release exactly so dailies stay distinguishable
  249. from each other and from the eventual stable.
  250. 2. ``APP_VERSION`` from config.py for manual workflow_dispatch runs
  251. (no tag) and for local builds.
  252. Strips the leading ``v`` from tags so the installer filename is
  253. ``bambuddy-0.2.5b1-daily.20260610-windows-x64-setup.exe``, not
  254. ``bambuddy-v0.2.5b1-...``.
  255. """
  256. ref = os.environ.get("GITHUB_REF", "")
  257. if ref.startswith("refs/tags/"):
  258. tag = ref.removeprefix("refs/tags/")
  259. if tag.startswith("v"):
  260. tag = tag[1:]
  261. return tag or _read_app_version()
  262. return _read_app_version()
  263. def write_version_file() -> None:
  264. """Write the installer version as both a plain VERSION file and an
  265. Inno Setup include file so the .iss script can pick it up at compile
  266. time without a fragile file-read hack.
  267. """
  268. version = _resolve_installer_version()
  269. (STAGING / "VERSION").write_text(version)
  270. # Inno Setup include — bambuddy.iss does `#include "build\staging\version.iss"`
  271. iss_version = STAGING / "version.iss"
  272. iss_version.write_text(f'#define MyAppVersion "{version}"\n')
  273. log(f"staged VERSION = {version}")
  274. def main() -> int:
  275. parser = argparse.ArgumentParser(description=__doc__)
  276. parser.add_argument(
  277. "--skip-frontend",
  278. action="store_true",
  279. help="Skip frontend build (use existing frontend/dist/)",
  280. )
  281. parser.add_argument(
  282. "--skip-pip",
  283. action="store_true",
  284. help="Skip pip install (use existing staged Python)",
  285. )
  286. parser.add_argument(
  287. "--allow-non-windows",
  288. action="store_true",
  289. help=(
  290. "Override the Windows-only guard. Only useful if you have a "
  291. "working wine + windows-python toolchain. Not exercised by CI."
  292. ),
  293. )
  294. args = parser.parse_args()
  295. if sys.platform != "win32" and not args.allow_non_windows:
  296. log("ERROR: this build script must run on Windows.")
  297. log("")
  298. log("It downloads a Windows embeddable Python distribution and")
  299. log("pip-installs Bambuddy's requirements.txt against it — both")
  300. log("require executing python.exe, which only runs on Windows.")
  301. log("")
  302. log("Supported build paths:")
  303. log(" 1. GitHub Actions: trigger '.github/workflows/windows-")
  304. log(" installer.yml' (Actions tab -> Windows Installer ->")
  305. log(" Run workflow). Downloads the .exe as a workflow artifact.")
  306. log(" 2. Windows VM / box: clone, install Python 3.13 + Node 22 +")
  307. log(" Inno Setup 6, run this script.")
  308. log("")
  309. log("Unsupported escape hatch (cross-build under Wine): rerun with")
  310. log("--allow-non-windows. Requires wine + a Windows Python in $PATH")
  311. log("via wine python.exe — fragile and not exercised by CI.")
  312. return 1
  313. BUILD_DIR.mkdir(parents=True, exist_ok=True)
  314. DOWNLOADS.mkdir(parents=True, exist_ok=True)
  315. STAGING.mkdir(parents=True, exist_ok=True)
  316. python_dir = stage_embedded_python()
  317. if not args.skip_pip:
  318. install_requirements(python_dir)
  319. if args.skip_frontend:
  320. frontend_dist = REPO_ROOT / "frontend" / "dist"
  321. if not frontend_dist.exists():
  322. raise RuntimeError("--skip-frontend given but frontend/dist/ doesn't exist")
  323. else:
  324. frontend_dist = build_frontend()
  325. stage_backend(frontend_dist)
  326. stage_nssm()
  327. stage_ffmpeg()
  328. stage_service_scripts()
  329. write_version_file()
  330. log("")
  331. log("=" * 60)
  332. log("Staging complete.")
  333. log(f"Staged tree: {STAGING}")
  334. log("")
  335. log("Next: compile the Inno Setup script:")
  336. log(' "C:\\Program Files (x86)\\Inno Setup 6\\ISCC.exe" bambuddy.iss')
  337. log("")
  338. log(f"Installer will be written to: {BUILD_DIR / 'output'}")
  339. log("=" * 60)
  340. return 0
  341. if __name__ == "__main__":
  342. sys.exit(main())