-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_exe.py
More file actions
134 lines (116 loc) · 4.51 KB
/
Copy pathbuild_exe.py
File metadata and controls
134 lines (116 loc) · 4.51 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
"""
Build script to create a standalone executable for Stay Awake application.
"""
import os
import subprocess
import PyInstaller.__main__
import shutil
from PIL import Image
def create_multisize_ico_from_individual_files():
"""Create a multi-size ICO file from individual size-specific ICO files."""
print("Creating multi-size ICO from individual ICO files...")
# Map of sizes to filenames
size_files = {
16: 'icon 16.ico',
24: 'icon 24.ico',
32: 'icon 32.ico',
48: 'icon 48.ico',
64: 'icon 64.ico',
96: 'icon 96.ico',
128: 'icon 128.ico',
256: 'icon 256.ico',
512: 'icon.ico' # Main icon file is 512x512
}
# Load all available sizes
images = []
available_sizes = []
for size, filename in size_files.items():
if os.path.exists(filename):
try:
img = Image.open(filename)
# Ensure it's RGBA
if img.mode != 'RGBA':
img = img.convert('RGBA')
images.append(img)
available_sizes.append(size)
print(f" Loaded {filename} ({size}x{size})")
except Exception as e:
print(f" Warning: Could not load {filename}: {e}")
if images:
# Save combined multi-size ICO
multisize_path = 'windows_icon.ico'
images[0].save(
multisize_path,
format='ICO',
sizes=[(s, s) for s in available_sizes],
append_images=images[1:]
)
print(f"Created multi-size ICO: {multisize_path} with sizes: {available_sizes}")
return multisize_path
else:
print("No individual ICO files found")
return None
def build_exe():
"""Build the executable using PyInstaller."""
print("Building Stay Awake executable...")
# First, create multi-size ICO from individual files
multisize_ico = create_multisize_ico_from_individual_files()
# Define PyInstaller options
args = [
'stay_awake.py', # Your main script
'--name=StayAwake', # Name of the executable
'--noconsole', # No console window
'--onefile', # Single file executable
'--clean', # Clean cache before building
]
# Use the multi-size ICO we just created, or fall back to individual files
if multisize_ico and os.path.exists(multisize_ico):
icon_path = os.path.abspath(multisize_ico)
args.append(f'--icon={icon_path}')
print(f"Using multi-size ICO: {icon_path}")
elif os.path.exists('icon.ico'):
icon_path = os.path.abspath('icon.ico')
args.append(f'--icon={icon_path}')
print(f"Using main icon.ico: {icon_path}")
else:
print("No icon specified")
# Add data files if needed (configs, etc.)
if os.path.exists('default_config.json'):
args.append('--add-data=default_config.json;.')
# Add icon files as data files so they're available at runtime
if os.path.exists('windows_icon.ico'):
args.append('--add-data=windows_icon.ico;.')
print("Adding windows_icon.ico as data file")
elif os.path.exists('icon.ico'):
args.append('--add-data=icon.ico;.')
print("Adding icon.ico as data file")
# Run PyInstaller
PyInstaller.__main__.run(args)
print("Build completed. Executable is in the dist folder.")
# Open the directory containing the executable
dist_dir = os.path.abspath('dist')
if os.path.exists(dist_dir):
print(f"Executable location: {dist_dir}")
# Display startup instructions
print("\n=== Adding to Windows Startup ===")
print("To have Stay Awake start automatically when Windows boots:")
print("1. Press Win + R and type 'shell:startup'")
print("2. Create a shortcut to StayAwake.exe in this folder")
print("3. See STARTUP_GUIDE.md for more detailed instructions\n")
# Open explorer to the dist directory
try:
if os.name == 'nt': # Windows
os.startfile(dist_dir)
else:
import subprocess
subprocess.call(['open', dist_dir])
except:
pass # Ignore if can't open explorer
if __name__ == "__main__":
# Check if Pillow is installed for icon conversion
try:
import PIL
except ImportError:
print("Pillow not installed. Attempting to install...")
subprocess.check_call(['pip', 'install', 'Pillow'])
build_exe()