-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathMouser.spec
More file actions
331 lines (301 loc) · 10.3 KB
/
Mouser.spec
File metadata and controls
331 lines (301 loc) · 10.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
# -*- mode: python ; coding: utf-8 -*-
"""
PyInstaller spec file for Mouser
Produces a single-directory portable build in dist/Mouser/
Run: pyinstaller Mouser.spec
"""
import os
import sys
import shutil
import json
import subprocess
import PySide6
block_cipher = None
ROOT = os.path.abspath(".")
PYSIDE6_DIR = os.path.dirname(PySide6.__file__)
BUILD_INFO_PATH = os.path.join(ROOT, "build", "mouser_build_info.json")
def _load_app_version() -> str:
version_path = os.path.join(ROOT, "core", "version.py")
namespace = {"__file__": version_path}
with open(version_path, encoding="utf-8") as version_file:
exec(version_file.read(), namespace)
return namespace["APP_VERSION"]
def _run_git(args):
try:
return subprocess.check_output(
["git", *args],
cwd=ROOT,
stderr=subprocess.DEVNULL,
text=True,
timeout=0.5,
).strip()
except (OSError, subprocess.SubprocessError):
return ""
def _git_dirty():
try:
result = subprocess.run(
["git", "status", "--porcelain", "--untracked-files=no"],
cwd=ROOT,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
timeout=0.5,
check=False,
)
except (OSError, subprocess.SubprocessError):
return False
return result.returncode == 0 and bool(result.stdout.strip())
def _write_build_info(version: str) -> str:
commit = os.environ.get("MOUSER_GIT_COMMIT", "").strip() or _run_git(["rev-parse", "HEAD"])
dirty_env = os.environ.get("MOUSER_GIT_DIRTY")
if dirty_env:
dirty = dirty_env.strip().lower() in {"1", "true", "yes", "on"}
else:
dirty = _git_dirty()
os.makedirs(os.path.dirname(BUILD_INFO_PATH), exist_ok=True)
with open(BUILD_INFO_PATH, "w", encoding="utf-8") as build_info_file:
json.dump(
{
"version": version,
"commit": commit,
"dirty": dirty,
},
build_info_file,
)
return BUILD_INFO_PATH
APP_VERSION = _load_app_version()
BUILD_INFO_DATA = _write_build_info(APP_VERSION)
a = Analysis(
["main_qml.py"],
pathex=[ROOT],
binaries=[],
datas=[
# QML UI files
(os.path.join(ROOT, "ui", "qml"), os.path.join("ui", "qml")),
# Image assets
(os.path.join(ROOT, "images"), "images"),
(BUILD_INFO_DATA, "."),
],
hiddenimports=[
# conditional / lazy imports PyInstaller may miss
"hid",
"logging.handlers",
"ctypes.wintypes",
"ui.locale_manager",
# PySide6 QML runtime
"PySide6.QtQuick",
"PySide6.QtQuickControls2",
"PySide6.QtQml",
"PySide6.QtNetwork",
"PySide6.QtOpenGL",
"PySide6.QtSvg",
],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[
# ── Aggressively trim unneeded PySide6 modules ──
"PySide6.QtWebEngine",
"PySide6.QtWebEngineCore",
"PySide6.QtWebEngineWidgets",
"PySide6.QtWebChannel",
"PySide6.QtWebSockets",
"PySide6.Qt3DCore",
"PySide6.Qt3DRender",
"PySide6.Qt3DInput",
"PySide6.Qt3DLogic",
"PySide6.Qt3DAnimation",
"PySide6.Qt3DExtras",
"PySide6.QtMultimedia",
"PySide6.QtMultimediaWidgets",
"PySide6.QtBluetooth",
"PySide6.QtNfc",
"PySide6.QtPositioning",
"PySide6.QtLocation",
"PySide6.QtSensors",
"PySide6.QtSerialPort",
"PySide6.QtSerialBus",
"PySide6.QtTest",
"PySide6.QtPdf",
"PySide6.QtPdfWidgets",
"PySide6.QtCharts",
"PySide6.QtDataVisualization",
"PySide6.QtRemoteObjects",
"PySide6.QtScxml",
"PySide6.QtSql",
"PySide6.QtTextToSpeech",
"PySide6.QtQuick3D",
"PySide6.QtVirtualKeyboard",
"PySide6.QtGraphs",
"PySide6.Qt5Compat",
# ── PySide6 designer / tools (not needed at runtime) ──
"PySide6.QtDesigner",
"PySide6.QtHelp",
"PySide6.QtUiTools",
"PySide6.QtXml",
"PySide6.QtConcurrent",
"PySide6.QtDBus",
"PySide6.QtStateMachine",
"PySide6.QtHttpServer",
"PySide6.QtSpatialAudio",
# ── Other unused stdlib modules ──
"unittest",
"xmlrpc",
"pydoc",
"doctest",
"tkinter",
"test",
"distutils",
"setuptools",
"ensurepip",
"lib2to3",
"idlelib",
"turtledemo",
"turtle",
"sqlite3",
"multiprocessing",
],
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
# ── Filter out massive Qt DLLs and data we don't need ──────────────────
# The PySide6 hooks copy EVERYTHING (WebEngine=193MB, 3D, Charts, etc.).
# We only need: Core, Gui, Widgets, Qml, Quick, QuickControls2 (Material),
# OpenGL, Network, ShaderTools, and a few essentials.
_qt_keep = {
# Core Qt
"Qt6Core", "Qt6Gui", "Qt6Widgets", "Qt6Network", "Qt6OpenGL",
# QML / Quick
"Qt6Qml", "Qt6QmlCore", "Qt6QmlMeta", "Qt6QmlModels",
"Qt6QmlNetwork", "Qt6QmlWorkerScript",
"Qt6Quick", "Qt6QuickControls2", "Qt6QuickControls2Impl",
"Qt6QuickControls2Basic", "Qt6QuickControls2BasicStyleImpl",
"Qt6QuickControls2Material", "Qt6QuickControls2MaterialStyleImpl",
"Qt6QuickTemplates2", "Qt6QuickLayouts", "Qt6QuickEffects",
"Qt6QuickShapes",
# Rendering
"Qt6ShaderTools", "Qt6Svg",
# PySide6 runtime
"pyside6.abi3", "pyside6qml.abi3", "shiboken6.abi3",
# VC runtime
"MSVCP140", "MSVCP140_1", "MSVCP140_2",
"VCRUNTIME140", "VCRUNTIME140_1",
}
def _should_keep(name):
"""Return True if this binary/data entry should be kept."""
# Always keep non-PySide6 files
if "PySide6" not in name and "pyside6" not in name.lower():
return True
# Check the filename (last component)
base = os.path.basename(name)
stem = os.path.splitext(base)[0]
# Keep if it's in our whitelist
if stem in _qt_keep:
return True
# Keep all .pyd files (Python extensions — small and needed)
if base.endswith(".pyd"):
return True
# Keep plugin dirs we need (platforms, imageformats, styles, iconengines)
for keep in ("platforms", "imageformats", "styles", "iconengines",
"platforminputcontexts"):
if keep in name:
return True
# Keep QML dirs we need
for keep_qml in ("QtCore", "QtQml", "QtQuick", "QtNetwork"):
pat = os.path.join("qml", keep_qml)
if pat in name.replace("/", os.sep):
return True
# Drop everything else (WebEngine, 3D, Charts, Multimedia, etc.)
return False
a.binaries = [b for b in a.binaries if _should_keep(b[0])]
a.datas = [d for d in a.datas if _should_keep(d[0])]
exe = EXE(
pyz,
a.scripts,
[], # not one-file (faster startup, easier debugging)
exclude_binaries=True,
name="Mouser",
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=False, # UPX OFF — decompression at startup is very slow
console=False, # windowed app (no terminal)
icon=os.path.join(ROOT, "images", "logo.ico"),
uac_admin=False, # does NOT require admin
)
coll = COLLECT(
exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=False, # UPX OFF — faster cold start
upx_exclude=[],
name="Mouser",
)
# ── Post-build cleanup: remove Qt QML/plugin dirs we don't need ──────────
# PyInstaller's hooks copy the entire PySide6 QML tree; we only need
# QtQuick/Controls + Material, QtQml, QtQuick/Layouts, QtQuick/Templates,
# QtQuick/Window. Everything else is dead weight that slows startup.
_dist = os.path.join("dist", "Mouser", "_internal", "PySide6")
# QML dirs to KEEP (everything else under qml/ is deleted)
_keep_qml = {
"QtCore", "QtQml", "QtQuick", "QtNetwork",
}
# Under QtQuick, keep only what the app uses
_keep_qtquick = {
"Controls", "Layouts", "Templates", "Window",
}
# Plugin dirs to KEEP
_keep_plugins = {
"iconengines", "imageformats", "platforms",
"platforminputcontexts", "styles",
}
def _cleanup():
qml_root = os.path.join(_dist, "qml")
if os.path.isdir(qml_root):
for d in os.listdir(qml_root):
if d not in _keep_qml:
p = os.path.join(qml_root, d)
if os.path.isdir(p):
shutil.rmtree(p, ignore_errors=True)
print(f" [cleanup] removed qml/{d}")
# Trim inside QtQuick
qtquick = os.path.join(qml_root, "QtQuick")
if os.path.isdir(qtquick):
for d in os.listdir(qtquick):
if d not in _keep_qtquick:
p = os.path.join(qtquick, d)
if os.path.isdir(p):
shutil.rmtree(p, ignore_errors=True)
print(f" [cleanup] removed qml/QtQuick/{d}")
plugins_root = os.path.join(_dist, "plugins")
if os.path.isdir(plugins_root):
for d in os.listdir(plugins_root):
if d not in _keep_plugins:
p = os.path.join(plugins_root, d)
if os.path.isdir(p):
shutil.rmtree(p, ignore_errors=True)
print(f" [cleanup] removed plugins/{d}")
# Remove translations (not needed)
trans = os.path.join(_dist, "translations")
if os.path.isdir(trans):
shutil.rmtree(trans, ignore_errors=True)
print(" [cleanup] removed translations/")
print("[Mouser] Post-build cleanup...")
_cleanup()
print("[Mouser] Cleanup done.")
# ── macOS App Bundle ───────────────────────────────────────────────────
if sys.platform == 'darwin':
app = BUNDLE(
coll,
name='Mouser.app',
icon='images/AppIcon.icns',
bundle_identifier='com.mouser.app',
info_plist={
'CFBundleShortVersionString': APP_VERSION,
'CFBundleVersion': APP_VERSION,
'LSUIElement': True, # Runs in background (menu bar app)
'NSHighResolutionCapable': True,
},
)