-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
99 lines (85 loc) · 3.1 KB
/
build.py
File metadata and controls
99 lines (85 loc) · 3.1 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
import os
import sys
import platform
import subprocess
from pathlib import Path
import shutil
# -------- User Configuration --------
MAIN_SCRIPT = "main.py" # Change if your main script is different
ASSETS_SRC = Path("assets") # Relative to this script's location
ICON_PATH = ASSETS_SRC / "images" / "logo.ico" # Path to your icon file for exe
DIST_DIR = Path("dist")
BUILD_DIR = Path("build")
# ------------------------------------
def check_pyinstaller():
try:
import PyInstaller # noqa: F401
except ImportError:
print("PyInstaller not found. Installing...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "pyinstaller"])
def build_command(current_platform):
# Set --add-data flag syntax and icon flag
if os.name == "nt": # Windows
add_data = f"{ASSETS_SRC}{os.pathsep}assets"
icon_flag = ["--icon", str(ICON_PATH)] if ICON_PATH.exists() else []
else: # POSIX (Linux/macOS)
add_data = f"{ASSETS_SRC}:assets"
# .ico icons are not supported on Linux/macOS for PyInstaller, use .icns for macOS if available
if current_platform == "darwin" and ICON_PATH.with_suffix(".icns").exists():
icon_flag = ["--icon", str(ICON_PATH.with_suffix(".icns"))]
else:
icon_flag = []
cmd = [
"pyinstaller",
"--onefile",
"--windowed",
"--add-data",
add_data,
*icon_flag,
MAIN_SCRIPT,
]
return cmd
def copy_assets_to_dist():
"""
Ensure the assets folder is present in the dist directory after build.
"""
dist_assets = DIST_DIR / "assets"
if dist_assets.exists():
shutil.rmtree(dist_assets)
shutil.copytree(ASSETS_SRC, dist_assets)
print(f"Copied {ASSETS_SRC} to {dist_assets}.")
def clean():
if DIST_DIR.exists():
shutil.rmtree(DIST_DIR)
if BUILD_DIR.exists():
shutil.rmtree(BUILD_DIR)
for spec in Path(".").glob("*.spec"):
spec.unlink()
print("Cleaned previous builds.")
def main():
print("One-click PyInstaller build script.")
print("This script must be run on the OS you want to build for.")
print("---------------------------------------------------------")
print("1. Clean previous build")
print("2. Build for current platform")
print("---------------------------------------------------------")
clean()
check_pyinstaller()
current_platform = platform.system().lower()
cmd = build_command(current_platform)
print(f"Running: {' '.join(map(str, cmd))}")
subprocess.check_call(cmd)
# Copy assets folder to dist so it is available for the onefile binary
copy_assets_to_dist()
if current_platform == "windows":
print("Your EXE and assets are in the dist/ folder.")
elif current_platform == "linux":
print("Your Linux binary and assets are in the dist/ folder.")
elif current_platform == "darwin":
print("Your macOS binary and assets are in the dist/ folder.")
else:
print(f"Unsupported platform: {current_platform}")
sys.exit(1)
print("Done!")
if __name__ == "__main__":
main()