Implemented standalone builds

This commit is contained in:
Ben committed 2026-07-05 09:24:07 +02:00
1 parent 43213f6220
commit f1c8e2c575
37 files changed
+5771 -10

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
# Build the frozen backend sidecar on Windows (PyInstaller) and stage it for
# Tauri as binaries\konduktor-sidecar-<target-triple>.exe.
# Output: backend\dist\konduktor-sidecar.exe + frontend\src-tauri\binaries\...
#
# Run from a PowerShell prompt with the backend venv's Python on PATH, e.g.:
# cd backend
# .\.venv\Scripts\Activate.ps1
# pip install -r requirements.txt -r requirements-build.txt
# .\build_sidecar.ps1
$ErrorActionPreference = "Stop"
$Root = $PSScriptRoot
Set-Location $Root
# Prefer the venv python if present, else whatever `python` resolves to.
$Py = Join-Path $Root ".venv\Scripts\python.exe"
if (-not (Test-Path $Py)) { $Py = "python" }
& $Py -m PyInstaller --noconfirm --clean konduktor-sidecar.spec
Write-Host "Built: $Root\dist\konduktor-sidecar.exe"
# Stage for Tauri: name with the host target triple + .exe so the shell plugin
# resolves it. (rustc must be on PATH.)
$Triple = (& rustc -vV | Select-String '^host: ').ToString().Replace('host: ', '').Trim()
$Dest = Join-Path $Root "..\frontend\src-tauri\binaries"
New-Item -ItemType Directory -Force -Path $Dest | Out-Null
Copy-Item "$Root\dist\konduktor-sidecar.exe" (Join-Path $Dest "konduktor-sidecar-$Triple.exe") -Force
Write-Host "Staged: $Dest\konduktor-sidecar-$Triple.exe"
+24
View File
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
# Build the frozen backend sidecar (single-file binary) with PyInstaller and
# stage it for Tauri as binaries/konduktor-sidecar-<target-triple>.
# Output: backend/dist/konduktor-sidecar + frontend/src-tauri/binaries/…
#
# One-time: pip install -r requirements-build.txt (into the backend venv).
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
cd "$ROOT"
PY="${PY:-.venv/bin/python}"
"$PY" -m PyInstaller --noconfirm --clean konduktor-sidecar.spec
echo "Built: $ROOT/dist/konduktor-sidecar"
# Stage for Tauri: the sidecar must be named with the host target triple so the
# shell plugin can resolve it in both dev and bundle. (rustc must be on PATH.)
if command -v rustc >/dev/null 2>&1; then
TRIPLE="$(rustc -vV | sed -n 's/host: //p')"
DEST="$ROOT/../frontend/src-tauri/binaries"
mkdir -p "$DEST"
cp "$ROOT/dist/konduktor-sidecar" "$DEST/konduktor-sidecar-$TRIPLE"
echo "Staged: $DEST/konduktor-sidecar-$TRIPLE"
else
echo "warning: rustc not found — skipped staging the Tauri sidecar binary" >&2
fi
+64
View File
@@ -0,0 +1,64 @@
# PyInstaller spec for the Konduktor backend sidecar (Step 1 of the standalone
# build). Produces a single-file binary that serves the FastAPI API on a free
# loopback port. Build with: pyinstaller konduktor-sidecar.spec (or build_sidecar.sh)
#
# The tricky bits this handles:
# - uvicorn imports its loop/protocol backends dynamically → collect_submodules
# - the standard extras (uvloop/httptools/websockets) are native → collect_all
# - traktor-nml-utils parses/serializes via xsdata, which discovers plugins at
# runtime → collect_submodules('xsdata')
# - python-multipart is imported as `multipart` by starlette → hidden import
from PyInstaller.utils.hooks import collect_submodules, collect_all
hiddenimports = []
datas = []
binaries = []
# Dynamically-imported subpackages.
for pkg in ("uvicorn", "xsdata"):
hiddenimports += collect_submodules(pkg)
# Native standard extras — grab modules + shared libs + any data.
for pkg in ("uvloop", "httptools", "websockets"):
d, b, h = collect_all(pkg)
datas += d
binaries += b
hiddenimports += h
# Imported by name, not statically discoverable.
hiddenimports += ["multipart", "anyio"]
a = Analysis(
["sidecar.py"],
pathex=[],
binaries=binaries,
datas=datas,
hiddenimports=hiddenimports,
hookspath=[],
hooksconfig={},
runtime_hooks=[],
# watchfiles is dev-reload only; the frozen sidecar never uses --reload.
excludes=["watchfiles"],
noarchive=False,
)
pyz = PYZ(a.pure)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.datas,
[],
name="konduktor-sidecar",
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=False,
console=True, # keep stdout so the host can read KONDUKTOR_PORT
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
+8 -1
View File
@@ -48,7 +48,14 @@ app = FastAPI(title="Konduktor API", version="0.4.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
allow_origins=[
"http://localhost:5173",
"http://127.0.0.1:5173",
# Packaged desktop app: the webview's origin when Tauri loads the built
# frontend (macOS uses tauri://, Windows/Linux use https://tauri.localhost).
"tauri://localhost",
"https://tauri.localhost",
],
allow_methods=["*"],
allow_headers=["*"],
)
+4
View File
@@ -0,0 +1,4 @@
# Build-only dependencies (not needed to run the backend in dev).
# Freezes the backend into a standalone sidecar binary for the desktop build.
# See konduktor-sidecar.spec and build_sidecar.sh.
pyinstaller>=6.16 # 6.16+ supports Python 3.14
+60
View File
@@ -0,0 +1,60 @@
"""PyInstaller entry point for the packaged backend sidecar.
The frozen build launches this. It picks a free localhost port, announces it on
stdout as `KONDUKTOR_PORT=<n>` (so the host process — Tauri — knows where to
send API calls), then serves the FastAPI app on loopback.
Dev is unchanged: `dev.sh` still runs `uvicorn konduktor.main:app` on :8000.
This file exists only for the frozen sidecar.
"""
from __future__ import annotations
import os
import socket
import sys
import threading
import uvicorn
from konduktor.main import app
def _watch_parent_and_exit() -> None:
"""Exit when our parent (the Tauri shell) goes away.
Tauri spawns us with a piped stdin and holds the write end open without
writing anything. If the host process quits — gracefully, via SIGKILL, or by
crashing — the OS closes that pipe and our stdin reads EOF. That's our cue to
shut down, so the backend can never be orphaned (the Rust-side kill-on-exit
only covers the graceful path). When run standalone from a terminal, stdin is
a TTY and this blocks harmlessly forever.
"""
try:
while sys.stdin.buffer.read(1):
pass
except Exception:
pass
os._exit(0)
def _free_port() -> int:
"""Grab an unused loopback port. There's a tiny bind→close→rebind race, but
it's harmless on 127.0.0.1 for a single local sidecar."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", 0))
return s.getsockname()[1]
def main() -> None:
# Die with our host so the backend is never left orphaned.
threading.Thread(target=_watch_parent_and_exit, daemon=True).start()
# Honor an explicit port (tests / the host may pin one) else pick a free one.
port = int(os.environ.get("KONDUKTOR_PORT") or _free_port())
# Announce the port BEFORE uvicorn starts logging, and flush, so the parent
# can read one clean line without racing the server's own output.
print(f"KONDUKTOR_PORT={port}", flush=True)
uvicorn.run(app, host="127.0.0.1", port=port, log_level="info")
if __name__ == "__main__":
main()