Repository navigation
Expand file tree
/
Copy pathbuild_exe.py
More file actions
392 lines (336 loc) · 13.8 KB
/
Copy pathbuild_exe.py
File metadata and controls
392 lines (336 loc) · 13.8 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
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python3
"""
Build script to create standalone executable for PyTGS GUI
Run: python build_exe.py
"""
import os
import sys
import shutil
import subprocess
from pathlib import Path
import re
from datetime import datetime
# ============================================
# CONFIGURATION - EDIT THIS FOR EACH BUILD
# ============================================
VERSION = "1.0.1" # <-- CHANGE THIS FOR EACH BUILD
# ============================================
def sanitize_version_for_filename(version):
"""Convert version string to a safe filename (replace special chars)"""
return version.replace('-', '_')
def update_version_in_file(version):
"""Update version number in the GUI file"""
gui_file = "tgs_gui.py"
if not os.path.exists(gui_file):
print(f"Warning: {gui_file} not found, skipping version update")
return
try:
with open(gui_file, 'r', encoding='utf-8') as f:
content = f.read()
# Update the window title to include version
old_title_pattern = r'self\.root\.title\("PyTGS v[\d\.\-a-zA-Z]+ - Transient Grating Analyser"\)'
new_title = f'self.root.title("PyTGS v{version} - Transient Grating Analyser")'
content = re.sub(old_title_pattern, new_title, content)
# Add or update version variable
if re.search(r'VERSION = ', content):
content = re.sub(r'VERSION = "[\d\.\-a-zA-Z]+"', f'VERSION = "{version}"', content)
else:
version_var_pattern = r'(class TGSApp:.*?)(def __init__)'
version_insert = r'\1 VERSION = "' + version + r'"\n\n \2'
content = re.sub(version_var_pattern, version_insert, content, flags=re.DOTALL)
with open(gui_file, 'w', encoding='utf-8') as f:
f.write(content)
print(f" Updated version to {version} in {gui_file}")
except Exception as e:
print(f" Warning: Could not update version in GUI file: {e}")
def build_executable(version):
"""Build the standalone executable"""
print("\n" + "=" * 50)
print("Building executable...")
print("=" * 50)
main_script = "tgs_gui.py"
if not os.path.exists(main_script):
print(f" Error: {main_script} not found!")
return False
safe_version = sanitize_version_for_filename(version)
icon_path = os.path.abspath("dihomodyne_beams_icon.ico")
icon_param = []
if os.path.exists(icon_path):
print(f" Using icon: {icon_path}")
icon_param = [f"--icon={icon_path}"]
else:
print(" Warning: Icon not found, continuing without icon...")
# More thorough cleanup of previous build files
print("\nCleaning previous build files...")
dirs_to_clean = ['build', 'dist', '__pycache__']
for dir_name in dirs_to_clean:
dir_path = Path(dir_name)
if dir_path.exists():
try:
shutil.rmtree(dir_path, ignore_errors=True)
print(f" Removed {dir_name}/")
except Exception as e:
print(f" Could not remove {dir_name}: {e}")
for file in Path(".").glob("*.spec"):
try:
file.unlink()
print(f" Removed: {file.name}")
except:
pass
# Build command with additional hidden imports and exclusions
cmd = [
sys.executable, "-m", "PyInstaller",
f"--name=PyTGS_v{safe_version}",
"--windowed",
"--onefile",
f"--icon={icon_path}",
"--add-data=config.yaml;.",
f"--add-data={icon_path};.",
# Basic hidden imports
"--hidden-import=PIL",
"--hidden-import=PIL.Image",
"--hidden-import=PIL.ImageTk",
"--hidden-import=yaml",
"--hidden-import=tkinter",
"--hidden-import=ttkbootstrap",
# Matplotlib - tk backend
"--hidden-import=matplotlib",
"--hidden-import=matplotlib.backends.backend_tkagg",
"--hidden-import=matplotlib.figure",
"--hidden-import=matplotlib.pyplot",
# NumPy - basic only
"--hidden-import=numpy",
"--hidden-import=numpy.core._methods",
"--hidden-import=numpy.linalg.lapack_lite",
# SciPy - include all dependencies needed for scipy.signal
"--hidden-import=scipy",
"--hidden-import=scipy.fft",
"--hidden-import=scipy.optimize",
"--hidden-import=scipy.special",
"--hidden-import=scipy.linalg",
"--hidden-import=scipy.spatial",
"--hidden-import=scipy.sparse",
"--hidden-import=scipy.interpolate",
"--hidden-import=scipy.signal",
"--hidden-import=scipy.ndimage",
"--hidden-import=scipy.stats",
# PyVISA and backends
"--hidden-import=pyvisa",
"--hidden-import=pyvisa_py",
"--hidden-import=pyvisa-py",
"--hidden-import=pyvisa_py.tcpip",
"--hidden-import=pyvisa_py.usb",
"--hidden-import=pyvisa_py.serial",
# Your modules
"--hidden-import=src.analysis.signal_process",
"--hidden-import=src.analysis.fft",
"--hidden-import=src.analysis.lorentzian",
"--hidden-import=src.analysis.tgs",
"--hidden-import=src.core.path",
"--hidden-import=src.core.utils",
"--hidden-import=src.core.plots",
"--hidden-import=src.analysis.functions",
# Safe exclusions (test modules, unused GUI backends, etc.)
"--exclude-module=matplotlib.tests",
"--exclude-module=numpy.f2py.tests",
"--exclude-module=scipy._lib.array_api_compat.torch",
"--exclude-module=pyvisa.testsuite",
"--exclude-module=pytest",
"--exclude-module=torch",
"--exclude-module=PyQt5",
"--exclude-module=PySide2",
"--exclude-module=PyQt6",
"--exclude-module=PySide6",
"--exclude-module=tkinter.test",
"--exclude-module=setuptools",
"--exclude-module=pkg_resources",
"--exclude-module=distutils",
"--exclude-module=ctypes.test",
"--exclude-module=distutils.tests",
"--exclude-module=lib2to3",
# Collect data for these packages
"--collect-all=ttkbootstrap",
"--collect-all=pyvisa",
"--collect-all=pyvisa_py",
"--collect-data=pyvisa",
"--collect-data=pyvisa_py",
"--path", sys.path[0],
"--noconfirm",
main_script
]
if icon_param:
onefile_index = cmd.index("--onefile")
cmd[onefile_index + 1:onefile_index + 1] = icon_param
print("\nRunning PyInstaller...")
print("This may take a few minutes...\n")
try:
# Run PyInstaller without capturing output to see real-time progress
result = subprocess.run(cmd, capture_output=False, text=True)
# Check return code
if result.returncode != 0:
print(f"\n PyInstaller returned error code: {result.returncode}")
return False
# Check if executable was created successfully
exe_path = Path(f"dist/PyTGS_v{safe_version}.exe")
if exe_path.exists() and exe_path.stat().st_size > 1000000: # Should be > 1MB
print(f"\n SUCCESS! Executable built: {exe_path}")
print(f" File size: {exe_path.stat().st_size / 1024 / 1024:.1f} MB")
return True
else:
print("\n Build failed - executable not found or too small")
return False
except Exception as e:
print(f"\n Build failed with error: {e}")
import traceback
traceback.print_exc()
return False
def cleanup_old_builds(version):
"""Remove old build artifacts to save space"""
print("\n" + "=" * 50)
print("Cleaning up old build files...")
print("=" * 50)
# Remove build directory with retry logic
build_dir = Path("build")
if build_dir.exists():
try:
shutil.rmtree(build_dir, ignore_errors=True)
print(" Removed build directory")
except Exception as e:
print(f" Could not remove build directory: {e}")
print(" Trying alternative cleanup...")
try:
import glob
for item in build_dir.glob('**/*'):
try:
if item.is_file():
item.unlink()
except:
pass
shutil.rmtree(build_dir, ignore_errors=True)
print(" Build directory cleaned")
except:
print(" Build directory will be cleaned on next run")
# Remove spec files
for spec_file in Path(".").glob("*.spec"):
try:
spec_file.unlink()
print(f" Removed: {spec_file.name}")
except:
pass
# Remove pycache directories
for pycache in Path(".").glob("**/__pycache__"):
try:
shutil.rmtree(pycache, ignore_errors=True)
except:
pass
# Remove old version executables (keep current version)
safe_version = sanitize_version_for_filename(version)
dist_dir = Path("dist")
if dist_dir.exists():
for exe in dist_dir.glob("PyTGS_*.exe"):
if f"PyTGS_v{safe_version}.exe" not in str(exe):
try:
exe.unlink()
print(f" Removed old executable: {exe.name}")
except:
pass
def check_dependencies():
"""Check if all required packages are installed"""
print("\n" + "=" * 50)
print("Checking dependencies...")
print("=" * 50)
# Map of pip package names to import names
package_import_map = {
'pyyaml': 'yaml',
'numpy': 'numpy',
'matplotlib': 'matplotlib',
'scipy': 'scipy',
'pillow': 'PIL',
'pyinstaller': None, # Not needed for runtime, just for building
'ttkbootstrap': 'ttkbootstrap',
'pyvisa': 'pyvisa',
'pyvisa-py': 'pyvisa_py' # Note: imports with underscore
}
missing = []
for package, import_name in package_import_map.items():
if import_name is None:
continue
try:
__import__(import_name)
print(f" {package} (imports as {import_name})")
except ImportError:
print(f" {package} (imports as {import_name}) - NOT FOUND")
missing.append(package)
if missing:
print(f"\n Warning: Missing packages: {', '.join(missing)}")
print(" Install with: pip install " + ' '.join(missing))
response = input("\nContinue anyway? (y/n): ")
if response.lower() != 'y':
return False
else:
print("\n All required packages found!")
# Also check for pyinstaller availability (optional)
try:
import PyInstaller
print(f" pyinstaller (available for building)")
except ImportError:
print(f" pyinstaller not found - building may fail")
return True
def main():
print("=" * 60)
print("PyTGS Standalone Builder")
print("=" * 60)
print(f"\nBuilding version: {VERSION}")
# Check Python version
if sys.version_info < (3, 8):
print(" Error: Python 3.8 or higher is required")
sys.exit(1)
# Check if running from correct directory
if not os.path.exists("tgs_gui.py"):
print(" Error: tgs_gui.py not found in current directory")
print(" Please run this script from the directory containing tgs_gui.py")
sys.exit(1)
# Check dependencies
if not check_dependencies():
print("\n Build cancelled due to missing dependencies")
sys.exit(1)
# Update version in GUI
update_version_in_file(VERSION)
# Build executable
build_success = build_executable(VERSION)
# Clean up old builds
cleanup_old_builds(VERSION)
# Final summary
print("\n" + "=" * 60)
print("BUILD SUMMARY")
print("=" * 60)
safe_version = sanitize_version_for_filename(VERSION)
if build_success:
print(f"\n Version {VERSION} built successfully!")
print(f"\nOutput file:")
print(f" - Executable: dist/PyTGS_v{safe_version}.exe")
print("\nTo distribute:")
print(f" Give users the PyTGS_v{safe_version}.exe file")
print(" They can run it directly - no extraction needed!")
print("\nNote for users needing oscilloscope connection:")
print(" - The executable includes pyvisa-py for pure Python VISA communication")
print(" - For USB/GPIB support, NI-VISA Runtime may still be required")
print(" - TCP/IP connections work without additional software")
else:
print(f"\n Build failed for version {VERSION}")
print("\nCommon issues and solutions:")
print(" 1. Make sure all dependencies are installed:")
print(" pip install pyyaml numpy matplotlib scipy pillow pyinstaller ttkbootstrap pyvisa pyvisa-py")
print(" 2. Check that config.yaml exists in the current directory")
print(" 3. Try building with administrator privileges")
print(" 4. Temporarily disable antivirus software")
print("\n" + "=" * 60)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\nBuild cancelled by user.")
sys.exit(1)
except Exception as e:
print(f"\nUnexpected error: {e}")
sys.exit(1)