-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_exe.py
57 lines (50 loc) · 1.94 KB
/
build_exe.py
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
import os
import sys
import subprocess
def resource_path(relative_path):
"""Get absolute path to resource, works for dev and for PyInstaller"""
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
# Define important paths
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
FRONTEND_SCRIPT = "S2RM_frontend.py"
DIST_DIR = os.path.join(PROJECT_DIR, "dist")
# Files and directories that should be included in the .exe bundle
DATA_FILES = ['limited_stacks.json', 'icon.ico','raw_materials_table.json']
OTHER_FILES = ["constants.py", "S2RM_backend.py", "helpers.py"]
print(f"Using data files: {DATA_FILES}")
# Detect OS for correct PyInstaller format
if sys.platform == "win32":
ADD_DATA_FLAG = ";."
else:
ADD_DATA_FLAG = ":."
# Construct --add-data arguments for .json and other files
add_data_args = []
for file in DATA_FILES + OTHER_FILES:
file_path = os.path.join(PROJECT_DIR, file)
if os.path.exists(file_path):
add_data_args.extend(["--add-data", f"{file}{ADD_DATA_FLAG}"])
# Construct PyInstaller command (with --noconsole to hide terminal output)
pyinstaller_cmd = [
"pyinstaller", # Call pyinstaller directly
"--onefile", # Export as one file
"--icon", "icon.ico", # Specify icon
"--add-data", f"icon.ico;icon", # Ensure the icon is bundled
"--hidden-import", "PySide6",
"--distpath", DIST_DIR, # Specify output directory
"--name", "S2RM",
"--noconsole", # Hide the terminal window
FRONTEND_SCRIPT
] + add_data_args
# Run PyInstaller
print("\n🚀 Building .exe with PyInstaller...")
build_result = subprocess.run(pyinstaller_cmd)
# Check the results
if build_result.returncode == 0:
print(f"\n✅ Build complete! Executable and resources are in: {DIST_DIR}")
else:
print("\n❌ Build failed. Check PyInstaller logs.")