-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_exe.py
254 lines (209 loc) · 6.82 KB
/
make_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
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
import os
import platform
import tempfile
import shutil
import stat
import struct
#### OPTIONS ####
NAME_OF_GAME = "Demo Game"
NAME_OF_GAME_SIMPLE = "demo"
ICON_PATH_ICO = None # "assets/icons/icon.ico"
ICON_PATH_ICNS = None # "assets/icons/icon.icns" # mac-style icons
SPLASH_IMAGE_PATH = None # "assets/splash.png"
ONEFILE_MODE = True
SHOW_CONSOLE = False
SHOW_TRACEBACK_ON_CRASH = True
ENTRY_POINT_FILE = "entry_point.py"
DATA_TO_BUNDLE = [
("assets", "assets"),
]
DATA_TO_COPY = [
# ("info.txt", "info.txt")
]
#### END OPTIONS ####
_WINDOWS = "Windows"
_LINUX = "Linux"
_MAC = "Darwin"
OS_SYSTEM_STR = platform.system()
if OS_SYSTEM_STR not in (_WINDOWS, _LINUX, _MAC):
raise ValueError("Unrecognized operating system: {}".format(OS_SYSTEM_STR))
if not ONEFILE_MODE:
# XXX using a splash image with ONEFILE_MODE = False seems to
# cause the exe to create a non-focused pygame window (that
# lands behind the file browser). So I'm disabling this for now.
# You don't really need a splash image with non-onefile anyways.
SPLASH_IMAGE_PATH = None
if OS_SYSTEM_STR == _MAC:
SPLASH_IMAGE_PATH = None # doesn't work on mac
ONEFILE_MODE = True # onedir doesn't really seem to work
SPEC_CONTENTS = f"""
# -*- mode: python ; coding: utf-8 -*-
# WARNING: This file is auto-generated (see make_exe.py)
a = Analysis(['{ENTRY_POINT_FILE}'],
pathex=[''],
binaries=[],
datas=[{", ".join(f"('{src}', '{dest}')" for (src, dest) in DATA_TO_BUNDLE)}],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=None)
pyz = PYZ(a.pure, a.zipped_data, cipher=None)
"""
STD_EXE_OPTS = f"""
name='{NAME_OF_GAME}',
console={SHOW_CONSOLE},
icon=~ICON_PATH~,
debug=False,
strip=False,
upx=True,
disable_windowed_traceback={not SHOW_TRACEBACK_ON_CRASH},
bootloader_ignore_signals=False
"""
# PREVENT YOUR DEATH. GO NO FURTHER
# There's nothing in this file worth dying for.
# Do not go beyond this point
if ONEFILE_MODE and SPLASH_IMAGE_PATH is None:
SPEC_CONTENTS += f"""
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[], # no idea
{STD_EXE_OPTS})
# for mac builds, which I guess requires onefile mode?
app = BUNDLE(exe,
name='{NAME_OF_GAME}.app',
icon=~ICON_PATH~,
bundle_identifier=None)
"""
elif not ONEFILE_MODE and SPLASH_IMAGE_PATH is None:
SPEC_CONTENTS += f"""
exe = EXE(pyz,
a.scripts,
[], # no idea what this is
exclude_binaries=True,
{STD_EXE_OPTS})
coll = COLLECT(
exe,
a.binaries,
a.zipfiles,
a.datas,
name='{NAME_OF_GAME}',
strip=False,
upx=True
)
"""
else: # splash mode (note that mac + splash doesn't work)
SPEC_CONTENTS += f"""
splash = Splash('{SPLASH_IMAGE_PATH}',
binaries=a.binaries,
datas=a.datas,
text_pos=None,
text_size=12,
minify_script=True)
"""
if ONEFILE_MODE:
SPEC_CONTENTS += f"""
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
splash,
splash.binaries,
[],
{STD_EXE_OPTS})
"""
else:
SPEC_CONTENTS += f"""
exe = EXE(pyz,
a.scripts,
splash,
[], # no idea what this is
exclude_binaries=True,
{STD_EXE_OPTS})
coll = COLLECT(
exe,
a.binaries,
a.zipfiles,
a.datas,
splash.binaries,
name='{NAME_OF_GAME}',
strip=False,
upx=True
)
"""
def _ask_yes_or_no_question(question):
print("") # newline to make it a little less claustrophobic
answer = None
while answer is None:
txt = input(" " + question + " (y/n): ")
if txt == "y" or txt == "Y":
answer = True
elif txt == "n" or txt == "N":
answer = False
print("")
return answer
def _calc_bit_count_str():
return "{}bit".format(struct.calcsize("P") * 8)
def _get_icon_path(os_version_str):
if os_version_str == _MAC:
return os.path.normpath(ICON_PATH_ICNS) if ICON_PATH_ICNS else None
else:
return os.path.normpath(ICON_PATH_ICO) if ICON_PATH_ICO else None
def do_it():
if OS_SYSTEM_STR == _MAC:
pretty_os_str = "Mac" # darwin is weird
else:
pretty_os_str = OS_SYSTEM_STR
os_bit_count_str = _calc_bit_count_str()
spec_filename = "output.spec"
print("INFO: creating spec file {}".format(spec_filename))
icon_path = _get_icon_path(OS_SYSTEM_STR)
with open(spec_filename, "w") as f:
f.write(SPEC_CONTENTS.replace("~ICON_PATH~", f"'{icon_path}'" if icon_path else "None"))
dist_dir = os.path.join("dist", "{}_{}_{}".format(
NAME_OF_GAME_SIMPLE,
pretty_os_str.lower(),
os_bit_count_str.lower()))
if os.path.exists(dist_dir):
ans = _ask_yes_or_no_question("Overwrite {}?".format(dist_dir))
if ans:
print("INFO: deleting pre-existing build {}".format(dist_dir))
shutil.rmtree(str(dist_dir), ignore_errors=True)
else:
print("INFO: user opted to not overwrite pre-existing build, exiting")
return
dist_dir_subdir = os.path.join(dist_dir, NAME_OF_GAME_SIMPLE)
with tempfile.TemporaryDirectory() as temp_dir:
print("INFO: created temp directory: {}".format(temp_dir))
print("INFO: launching pyinstaller...\n")
# note that this call blocks until the process is finished
os.system("pyinstaller {} --distpath {} --workpath {}".format(
spec_filename, dist_dir_subdir, temp_dir))
print("\nINFO: cleaning up {}".format(temp_dir))
print("INFO: cleaning up {}".format(spec_filename))
if os.path.exists(str(spec_filename)):
os.remove(str(spec_filename))
if OS_SYSTEM_STR == _LINUX:
print("INFO: chmod'ing execution permissions to all users (linux)")
exe_path = os.path.join(dist_dir_subdir, NAME_OF_GAME)
if not os.path.exists(str(exe_path)):
raise ValueError("couldn't find exe to apply exec permissions: {}".format(exe_path))
else:
st = os.stat(str(exe_path))
os.chmod(str(exe_path), st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
for src_path, dest_path in DATA_TO_COPY:
if not os.path.exists(src_path):
raise ValueError("couldn't find data to copy: {}".format(src_path))
else:
full_dest_path = os.path.join(dist_dir_subdir, dest_path)
print("INFO: copying {} to {}".format(src_path, full_dest_path))
shutil.copy2(src_path, full_dest_path)
print("\nINFO: make_exe.py has finished")
if __name__ == "__main__":
do_it()