-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexlab_wizard.spec
More file actions
380 lines (319 loc) · 13.3 KB
/
Copy pathexlab_wizard.spec
File metadata and controls
380 lines (319 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# -*- mode: python ; coding: utf-8 -*-
"""PyInstaller spec for ExLab-Wizard. Backend Spec §15.1.
Builds **three console-script entry points** sharing a single
``_internal/`` directory via PyInstaller's ``MERGE`` directive:
* ``ExLab-Wizard-Tray`` -- long-lived tray + server (§15.3.1).
* ``ExLab-Wizard-Window`` -- on-demand pywebview window subprocess (§15.3.2).
* ``ExLab-Wizard`` -- CLI alias / desktop-launcher target (§15.3.3).
The artifact is a ``--onedir`` bundle (the spec rejects ``--onefile``;
§15.1) so plugin- and window-subprocess re-launch via ``sys.executable``
hits the bundled CPython directly without re-extracting on each spawn.
The spec is invoked by both the local-build wrappers
(``scripts/build_local.{sh,ps1}``) and the CI matrix
(``.github/workflows/build.yml``).
Usage:
pyinstaller exlab_wizard.spec
"""
# ruff: noqa
# pyinstaller injects ``Analysis``, ``PYZ``, ``EXE``, ``COLLECT``, ``MERGE``,
# ``BUNDLE`` at runtime; static analysers cannot see them.
from __future__ import annotations
import sys
from pathlib import Path
# ---------------------------------------------------------------------------
# Paths and constants
# ---------------------------------------------------------------------------
# Repo root (PyInstaller runs the spec with cwd at the repo root).
REPO_ROOT = Path(".").resolve()
# Read the version from the package so the binary's metadata matches
# ``pyproject.toml`` automatically. Backend Spec §15.6.
# Src-layout: the package lives under ``src/`` so prepend that directory
# rather than the repo root.
sys.path.insert(0, str(REPO_ROOT / "src"))
from exlab_wizard import __version__ as APP_VERSION # noqa: E402
# CFBundleIdentifier on macOS. Backend Spec §15.1 / §15.7.1.
APP_BUNDLE_ID = "com.lab.exlab-wizard"
APP_NAME = "ExLab-Wizard"
# Three entry-point script paths (all live under the package). Each
# script wraps a ``main()`` callable so PyInstaller can produce a thin
# launcher that drops directly into our code.
ENTRY_TRAY = "src/exlab_wizard/tray/main.py"
ENTRY_WINDOW = "src/exlab_wizard/window/main.py"
ENTRY_CLI = "src/exlab_wizard/__main__.py"
# Output executable names (per the §15.1 table). PyInstaller writes
# these into ``dist/`` together with the shared ``_internal/`` directory.
EXE_TRAY = "ExLab-Wizard-Tray"
EXE_WINDOW = "ExLab-Wizard-Window"
EXE_CLI = "ExLab-Wizard"
# ---------------------------------------------------------------------------
# Hidden imports
# ---------------------------------------------------------------------------
#
# These are libraries that PyInstaller's static analyser cannot follow
# reliably (dynamic ``importlib`` calls, plugin-style entry points,
# platform-specific backends pulled in lazily). Keep this list tight and
# documented; every entry has a §-reference for why it is needed.
HIDDEN_IMPORTS: list[str] = [
# NiceGUI mounts on FastAPI; some sub-modules are imported via
# registry hooks (§4.1, §15.1).
"nicegui",
# pywebview platform shims load the native webview lazily (§15.5).
"webview",
# pystray platform backends (Cocoa/Win32/AppIndicator) are picked at
# import time but PyInstaller misses them (§15.5).
"pystray",
# plyer notifications backends (§15.7.3).
"plyer",
# keyring backends -- the OS-specific backend is selected at runtime
# (§7.4); PyInstaller cannot follow the entry-point lookup.
"keyring",
"keyring.backends",
# Copier renders templates with Jinja2; some Copier sub-modules pull
# in plugin-style helpers via ``importlib`` (§5).
"copier",
# ruamel.yaml round-trip loader for ``config.yaml`` (§9.1).
"ruamel.yaml",
]
# ---------------------------------------------------------------------------
# Bundled data
# ---------------------------------------------------------------------------
#
# ``--add-data`` pairs of ``(source, dest_in_bundle)``. PyInstaller copies
# each source into the bundle at the named destination. Backend Spec
# §15.4: bundled starter content lives under ``_internal/`` on disk too,
# so the runtime resolution of ``sys._MEIPASS / "_internal" / "..."`` is
# identical in dev and bundled mode.
DATAS: list[tuple[str, str]] = []
def _resolve_nicegui_assets_dir() -> Path | None:
"""Return the directory that holds NiceGUI's static CSS/JS assets.
NiceGUI ships its own bundled static files alongside the package;
these need to ride into the PyInstaller bundle so the offline UI
renders without internet access (§15.4 / §15.5). We resolve the
location dynamically via ``nicegui.__file__`` instead of hard-coding
a version-specific path.
"""
try:
import nicegui # type: ignore[import-not-found]
except ImportError:
return None
pkg_root = Path(nicegui.__file__).resolve().parent
# NiceGUI's static dir is typically ``static/`` next to ``__init__.py``.
static = pkg_root / "static"
return static if static.is_dir() else None
_nicegui_static = _resolve_nicegui_assets_dir()
if _nicegui_static is not None:
DATAS.append((str(_nicegui_static), "nicegui/static"))
# Bundled starter content (§15.4). The directories may be empty in v1
# (each carries a ``.gitkeep``); we still emit the ``--add-data`` so the
# runtime path resolution succeeds without conditionals.
DATAS.append(("_internal/templates", "_internal/templates"))
DATAS.append(("_internal/plugins", "_internal/plugins"))
# Static UI assets (SVG icons, etc.) served at ``/assets`` by the
# NiceGUI app via ``ui/theme.py:register_static_assets``.
DATAS.append(("assets", "assets"))
# ---------------------------------------------------------------------------
# Bundled binaries
# ---------------------------------------------------------------------------
#
# TODO(v1.1): Bundle the rclone binary inside the artifact at
# ``_internal/bin/rclone[.exe]`` per Backend Spec §15.5. For v1 we leave
# rclone as a system-installed dependency (the NASSync transport probes
# the system PATH; `Settings → NAS Cleanup` surfaces a clear error if
# missing). When we revisit:
#
# * Pin a specific rclone minor; vendor the per-OS binary under
# ``_internal/bin/`` in the build tree.
# * Add ``--add-binary`` entries below, one per target OS, gated on the
# build host's ``sys.platform``.
# * Update §15.5 to drop the "system rclone" wording.
#
# Same pattern for the Windows ``WebView2Loader.dll`` and the Linux
# GTK-WebKit hooks (§15.5): these are present in the host OS in v1.
#
# Reference: design_specs/design_spec_sections/15_Distribution.md §15.5
BINARIES: list[tuple[str, str]] = []
# ---------------------------------------------------------------------------
# Per-OS metadata + icons
# ---------------------------------------------------------------------------
ICON_DIR = REPO_ROOT / "assets"
def _resolve_icon() -> str | None:
"""Pick the per-OS icon if present; otherwise return None.
PyInstaller accepts ``None`` for ``icon`` (uses its default). Real
icons land in ``assets/icons/`` in a follow-up populating step;
Phase 15 commits the spec without binding to a specific filename.
macOS ``BUNDLE`` rejects non-``.icns`` icons, and Windows ``EXE``
rejects non-``.ico`` icons. On Linux any image works (PyInstaller
treats the icon as decorative). We only return a path that matches
the platform's required extension; otherwise return None and let
PyInstaller emit its default icon. The SVG repo logo is NOT a
valid platform icon for any of these targets and is never returned.
"""
if sys.platform == "darwin":
candidate = ICON_DIR / "icons" / "ExLabWizard.icns"
elif sys.platform == "win32":
candidate = ICON_DIR / "icons" / "ExLabWizard.ico"
else:
candidate = ICON_DIR / "icons" / "ExLabWizard.png"
return str(candidate) if candidate.exists() else None
ICON_PATH = _resolve_icon()
# Windows app manifest XML (DPI awareness, requestedExecutionLevel).
# PyInstaller injects this when ``manifest=`` is passed to ``EXE``.
WINDOWS_MANIFEST = """<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
<assemblyIdentity
type='win32'
name='com.lab.exlab-wizard'
version='{version}.0'
processorArchitecture='*'/>
<trustInfo xmlns='urn:schemas-microsoft-com:asm.v3'>
<security>
<requestedPrivileges>
<requestedExecutionLevel level='asInvoker' uiAccess='false'/>
</requestedPrivileges>
</security>
</trustInfo>
<application xmlns='urn:schemas-microsoft-com:asm.v3'>
<windowsSettings>
<dpiAware xmlns='http://schemas.microsoft.com/SMI/2005/WindowsSettings'>true</dpiAware>
</windowsSettings>
</application>
</assembly>
""".format(version=APP_VERSION)
# ---------------------------------------------------------------------------
# Per-entry-point Analysis + PYZ + EXE
# ---------------------------------------------------------------------------
def _make_analysis(script: str) -> "Analysis": # type: ignore[name-defined]
"""Construct a PyInstaller ``Analysis`` for one entry-point script."""
return Analysis( # noqa: F821 -- injected by PyInstaller
[script],
pathex=[str(REPO_ROOT / "src")],
binaries=BINARIES,
datas=DATAS,
hiddenimports=HIDDEN_IMPORTS,
hookspath=[],
runtime_hooks=[],
excludes=[],
noarchive=False,
)
a_tray = _make_analysis(ENTRY_TRAY)
a_window = _make_analysis(ENTRY_WINDOW)
a_cli = _make_analysis(ENTRY_CLI)
# MERGE de-duplicates the shared CPython interpreter and third-party
# libraries across the three entry points so they all draw from a single
# ``_internal/`` directory at runtime. Backend Spec §15.1.
MERGE( # noqa: F821 -- injected by PyInstaller
(a_tray, EXE_TRAY, EXE_TRAY),
(a_window, EXE_WINDOW, EXE_WINDOW),
(a_cli, EXE_CLI, EXE_CLI),
)
pyz_tray = PYZ(a_tray.pure, a_tray.zipped_data) # noqa: F821
pyz_window = PYZ(a_window.pure, a_window.zipped_data) # noqa: F821
pyz_cli = PYZ(a_cli.pure, a_cli.zipped_data) # noqa: F821
# Tray exe: ``console=True`` on every OS so log writes from third-party
# libs (uvicorn startup banner, plugin worker stderr passthrough) are
# visible if the operator launches from a terminal. The pywebview
# subprocess exits as soon as the window closes, so its console window
# vanishes immediately on Windows -- ``console=False`` suppresses the
# brief flash.
exe_tray = EXE( # noqa: F821
pyz_tray,
a_tray.scripts,
[],
exclude_binaries=True,
name=EXE_TRAY,
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=False,
console=True,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon=ICON_PATH,
manifest=(WINDOWS_MANIFEST if sys.platform == "win32" else None),
)
exe_window = EXE( # noqa: F821
pyz_window,
a_window.scripts,
[],
exclude_binaries=True,
name=EXE_WINDOW,
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=False,
console=False,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon=ICON_PATH,
manifest=(WINDOWS_MANIFEST if sys.platform == "win32" else None),
)
exe_cli = EXE( # noqa: F821
pyz_cli,
a_cli.scripts,
[],
exclude_binaries=True,
name=EXE_CLI,
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=False,
console=True,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon=ICON_PATH,
manifest=(WINDOWS_MANIFEST if sys.platform == "win32" else None),
)
# ---------------------------------------------------------------------------
# COLLECT -- package each entry point into the shared dist tree
# ---------------------------------------------------------------------------
coll = COLLECT( # noqa: F821 -- injected by PyInstaller
exe_tray,
a_tray.binaries,
a_tray.datas,
exe_window,
a_window.binaries,
a_window.datas,
exe_cli,
a_cli.binaries,
a_cli.datas,
strip=False,
upx=False,
upx_exclude=[],
name=APP_NAME,
)
# ---------------------------------------------------------------------------
# macOS .app bundle wrapper
# ---------------------------------------------------------------------------
#
# Backend Spec §15.1: the macOS artifact is an ``ExLab-Wizard.app`` bundle.
# The bundle's ``Contents/MacOS/`` carries all three executables; the
# tray executable is named in ``CFBundleExecutable`` so a finder
# double-click hits the CLI alias which decides whether to open or spawn.
if sys.platform == "darwin":
app = BUNDLE( # noqa: F821 -- injected by PyInstaller
coll,
name=f"{APP_NAME}.app",
icon=ICON_PATH,
bundle_identifier=APP_BUNDLE_ID,
version=APP_VERSION,
info_plist={
"CFBundleIdentifier": APP_BUNDLE_ID,
"CFBundleName": APP_NAME,
"CFBundleDisplayName": APP_NAME,
"CFBundleVersion": APP_VERSION,
"CFBundleShortVersionString": APP_VERSION,
"CFBundleExecutable": EXE_CLI,
# Backend Spec §15.2: v1 ships unsigned. Set ``LSUIElement``
# to false so the dock icon shows up; the tray's pystray
# icon lives in the menu bar regardless.
"LSUIElement": False,
"NSHighResolutionCapable": True,
"NSRequiresAquaSystemAppearance": False,
},
)