build.py 13 KB

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