build.py 13 KB

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