build.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. return target
  96. def install_requirements(python_dir: Path) -> None:
  97. """Install Bambuddy's requirements.txt into the embedded Python."""
  98. py = python_dir / "python.exe"
  99. requirements = REPO_ROOT / "requirements.txt"
  100. log(f"installing requirements.txt into {python_dir}")
  101. subprocess.run(
  102. [
  103. str(py),
  104. "-m",
  105. "pip",
  106. "install",
  107. "--no-warn-script-location",
  108. "-r",
  109. str(requirements),
  110. ],
  111. check=True,
  112. )
  113. def build_frontend() -> Path:
  114. """Run ``npm ci && npm run build`` and return the dist path."""
  115. frontend = REPO_ROOT / "frontend"
  116. dist = frontend / "dist"
  117. log("running npm ci in frontend/")
  118. npm = shutil.which("npm")
  119. if not npm:
  120. raise RuntimeError("npm not found on PATH — install Node.js 22 LTS")
  121. subprocess.run([npm, "ci"], cwd=frontend, check=True, shell=False)
  122. log("running npm run build in frontend/")
  123. subprocess.run([npm, "run", "build"], cwd=frontend, check=True, shell=False)
  124. if not dist.exists():
  125. raise RuntimeError(f"expected frontend build output at {dist}")
  126. return dist
  127. def stage_backend(frontend_dist: Path) -> None:
  128. """Copy backend source + frontend bundle into the staging tree.
  129. The runtime layout under STAGING/app/ mirrors a Bambuddy checkout:
  130. ``backend/`` (source), ``static/`` (frontend bundle served by FastAPI).
  131. """
  132. app = STAGING / "app"
  133. if app.exists():
  134. shutil.rmtree(app)
  135. app.mkdir(parents=True)
  136. # Backend source — copy the package tree, skip caches/tests/migrations
  137. log("staging backend source")
  138. shutil.copytree(
  139. REPO_ROOT / "backend",
  140. app / "backend",
  141. ignore=shutil.ignore_patterns(
  142. "__pycache__",
  143. "*.pyc",
  144. "tests",
  145. ".pytest_cache",
  146. ),
  147. )
  148. # Frontend bundle — FastAPI's StaticFiles mounts from app/static
  149. log("staging frontend bundle")
  150. shutil.copytree(frontend_dist, app / "static")
  151. def stage_nssm() -> None:
  152. target = STAGING / "bin"
  153. target.mkdir(parents=True, exist_ok=True)
  154. zip_path = download(NSSM_URL, DOWNLOADS / f"nssm-{NSSM_VERSION}.zip")
  155. extract = DOWNLOADS / f"nssm-{NSSM_VERSION}-extracted"
  156. if not extract.exists():
  157. unzip(zip_path, extract)
  158. # The zip nests as nssm-2.24/win64/nssm.exe
  159. src = next(extract.rglob("win64/nssm.exe"))
  160. log(f"staging nssm.exe from {src}")
  161. shutil.copy(src, target / "nssm.exe")
  162. def stage_ffmpeg() -> None:
  163. target = STAGING / "bin"
  164. target.mkdir(parents=True, exist_ok=True)
  165. zip_path = download(FFMPEG_URL, DOWNLOADS / "ffmpeg-win64-gpl.zip")
  166. extract = DOWNLOADS / "ffmpeg-extracted"
  167. if not extract.exists():
  168. unzip(zip_path, extract)
  169. src = next(extract.rglob("bin/ffmpeg.exe"))
  170. log(f"staging ffmpeg.exe from {src}")
  171. shutil.copy(src, target / "ffmpeg.exe")
  172. # ffprobe is used by some camera/timelapse paths
  173. ffprobe = next(extract.rglob("bin/ffprobe.exe"), None)
  174. if ffprobe is not None:
  175. shutil.copy(ffprobe, target / "ffprobe.exe")
  176. def stage_service_scripts() -> None:
  177. """Copy the service install/uninstall .bat files into staging."""
  178. service_src = INSTALLER_DIR / "service"
  179. service_dst = STAGING / "service"
  180. if service_dst.exists():
  181. shutil.rmtree(service_dst)
  182. shutil.copytree(service_src, service_dst)
  183. def write_version_file() -> None:
  184. """Write the installer version as both a plain VERSION file and an
  185. Inno Setup include file so the .iss script can pick it up at compile
  186. time without a fragile file-read hack.
  187. Reads from pyproject.toml's [project] version line for the source of
  188. truth. Falls back to ``0.0.0+dev`` if not parseable.
  189. """
  190. version = "0.0.0+dev"
  191. pyproject = REPO_ROOT / "pyproject.toml"
  192. if pyproject.exists():
  193. for line in pyproject.read_text().splitlines():
  194. line = line.strip()
  195. if line.startswith("version =") or line.startswith('version="'):
  196. # version = "0.1.5" -> 0.1.5
  197. version = line.split("=", 1)[1].strip().strip('"').strip("'")
  198. break
  199. (STAGING / "VERSION").write_text(version)
  200. # Inno Setup include — bambuddy.iss does `#include "build\staging\version.iss"`
  201. iss_version = STAGING / "version.iss"
  202. iss_version.write_text(f'#define MyAppVersion "{version}"\n')
  203. log(f"staged VERSION = {version}")
  204. def main() -> int:
  205. parser = argparse.ArgumentParser(description=__doc__)
  206. parser.add_argument(
  207. "--skip-frontend",
  208. action="store_true",
  209. help="Skip frontend build (use existing frontend/dist/)",
  210. )
  211. parser.add_argument(
  212. "--skip-pip",
  213. action="store_true",
  214. help="Skip pip install (use existing staged Python)",
  215. )
  216. parser.add_argument(
  217. "--allow-non-windows",
  218. action="store_true",
  219. help=(
  220. "Override the Windows-only guard. Only useful if you have a "
  221. "working wine + windows-python toolchain. Not exercised by CI."
  222. ),
  223. )
  224. args = parser.parse_args()
  225. if sys.platform != "win32" and not args.allow_non_windows:
  226. log("ERROR: this build script must run on Windows.")
  227. log("")
  228. log("It downloads a Windows embeddable Python distribution and")
  229. log("pip-installs Bambuddy's requirements.txt against it — both")
  230. log("require executing python.exe, which only runs on Windows.")
  231. log("")
  232. log("Supported build paths:")
  233. log(" 1. GitHub Actions: trigger '.github/workflows/windows-")
  234. log(" installer.yml' (Actions tab -> Windows Installer ->")
  235. log(" Run workflow). Downloads the .exe as a workflow artifact.")
  236. log(" 2. Windows VM / box: clone, install Python 3.13 + Node 22 +")
  237. log(" Inno Setup 6, run this script.")
  238. log("")
  239. log("Unsupported escape hatch (cross-build under Wine): rerun with")
  240. log("--allow-non-windows. Requires wine + a Windows Python in $PATH")
  241. log("via wine python.exe — fragile and not exercised by CI.")
  242. return 1
  243. BUILD_DIR.mkdir(parents=True, exist_ok=True)
  244. DOWNLOADS.mkdir(parents=True, exist_ok=True)
  245. STAGING.mkdir(parents=True, exist_ok=True)
  246. python_dir = stage_embedded_python()
  247. if not args.skip_pip:
  248. install_requirements(python_dir)
  249. if args.skip_frontend:
  250. frontend_dist = REPO_ROOT / "frontend" / "dist"
  251. if not frontend_dist.exists():
  252. raise RuntimeError("--skip-frontend given but frontend/dist/ doesn't exist")
  253. else:
  254. frontend_dist = build_frontend()
  255. stage_backend(frontend_dist)
  256. stage_nssm()
  257. stage_ffmpeg()
  258. stage_service_scripts()
  259. write_version_file()
  260. log("")
  261. log("=" * 60)
  262. log("Staging complete.")
  263. log(f"Staged tree: {STAGING}")
  264. log("")
  265. log("Next: compile the Inno Setup script:")
  266. log(' "C:\\Program Files (x86)\\Inno Setup 6\\ISCC.exe" bambuddy.iss')
  267. log("")
  268. log(f"Installer will be written to: {BUILD_DIR / 'output'}")
  269. log("=" * 60)
  270. return 0
  271. if __name__ == "__main__":
  272. sys.exit(main())