-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcompile.py
More file actions
428 lines (318 loc) · 14.4 KB
/
compile.py
File metadata and controls
428 lines (318 loc) · 14.4 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
Copyright 2024 Netflix Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Module which contains the functionality to compile the python application into its platform-specific executable and
installer
"""
import platform
import os
import shutil
import subprocess
import importlib.util
import sys
import xml.etree.ElementTree as ET
from types import ModuleType
from typing import List
def get_current_folder() -> str:
""" Get the current folder of this script
Returns: The current folder of this script
"""
return os.path.dirname(os.path.realpath(__file__))
def get_packages_folder() -> str:
""" Get the src folder of this project
Returns: The src folder of this project
"""
return os.path.join(get_current_folder(), "packages")
def get_python_package_folder() -> str:
""" Get the python package folder of this project
Returns: The python package folder of this project
"""
return os.path.join(get_packages_folder(), "open_vp_cal", "src", "open_vp_cal")
def import_module_from_filepath(filepath: str) -> ModuleType:
""" Import a module from a given filepath
Args:
filepath: The filepath to the module to import
Returns: The imported module
"""
spec = importlib.util.spec_from_file_location("module.name", filepath)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def get_version_from_python_package() -> str:
""" Get the version number from the python package
Returns: The version number from the python package
"""
python_folder = get_python_package_folder()
init_file = os.path.join(python_folder, "__init__.py")
init_module = import_module_from_filepath(init_file)
return init_module.__version__
def get_python_package_resource_folder() -> str:
""" Get the python package resource folder of this project
Returns: The python package resource folder of this project
"""
return os.path.join(get_python_package_folder(), "resources")
def get_additional_data_from_resources() -> List[str]:
""" Get a list of additional data flags for the --add-data command of the pyinstaller
Returns: A list of additional data flags for the --add-data command of the pyinstaller
"""
add_data = []
platform_sep = get_additional_data_seperator()
target_folder = os.path.join(".", "open_vp_cal", "resources", ".")
resources_folder = get_python_package_resource_folder()
resources = os.listdir(resources_folder)
for resource in resources:
input_file = os.path.join(resources_folder, resource)
if os.path.isdir(input_file):
continue
add_data.append(f"{input_file}{platform_sep}{target_folder}")
return add_data
def get_additional_data_seperator() -> str:
""" Get the path seperator used by pyinstaller for the platform we are running on
Returns: The path seperator used by pyinstaller for the platform we are running on
"""
if platform.system() == 'Windows':
return ";"
return ":"
def add_key_value_to_plist(plist_path: str, key_name: str, bool_value: bool) -> None:
""" Adds a boolean value to the XML file, plist of the app when compiled on osx to force dak mode
Args:
plist_path: The path to the plist file
key_name: The name of the key to add
bool_value: The value of the key to add
"""
# Parse the existing XML file
tree = ET.parse(plist_path)
root = tree.getroot()
# Assuming root is a 'plist' element, 'dict' will be the first child
dict_element = root[0]
# Now we can add a new key and boolean pair to the dict
# We'll create a new 'key' element, and then a new 'true' or 'false' element immediately after it
key_element = ET.SubElement(dict_element, 'key')
key_element.text = key_name # The name of your key
if bool_value:
ET.SubElement(dict_element, 'true')
else:
ET.SubElement(dict_element, 'false')
# Now the new key/value pair has been added to the XML data in memory
# We can write the data back out to the Info.plist file
tree.write(plist_path)
def get_iscc_app_vars(new_version: str, icon_path:str) -> dict[str, str]:
""" Updates the version in the ISS file which is used to define the build process for the installer on windows
Args:
filename: The filename of the .iss filepath
new_version: The new version we want to update too
icon_path: The path to the icon file
Returns: A dictionary of the variables to be used in the .iss file
"""
return {
"MyAppVersion" : new_version,
"MyAppIconPath" : icon_path,
"MyRepoPath" : get_current_folder(),
}
def create_windows_installer(iss_file_path: str, iss_vars: dict[str, str]) -> int:
""" Based on the given .iss file path, we run the inno setup compiler to create the window's installer
Args:
iss_file_path: The path to the .iss file to use to create the installer
Returns: The return code of the inno setup compiler
"""
# Path to your Inno Setup Compiler - adjust if necessary
inno_compiler_path = r"C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
# Create the command
command = f'"{inno_compiler_path}" "{iss_file_path}" ' + " ".join([f"-D{key}={value}" for key, value in iss_vars.items()])
# Execute the command
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
# Print output in real time
while True:
output = process.stdout.readline()
if output == b'' and process.poll() is not None:
break
if output:
print(output.strip().decode('utf-8'))
# Wait for the process to finish and get the return code.
return_code = process.wait()
return return_code
def check_python_is_64_bit() -> bool:
""" Checks the version of python we have installed is 64 bit
Returns: True if python is 64 bit, False if not
"""
return sys.maxsize > 2 ** 32
def check_python_version() -> bool:
""" Checks the version of python we have installed is 3.11
Returns: True if python is 3.11, False if not
"""
major_minor_version = '.'.join(platform.python_version().split('.')[:2])
return '3.11' == major_minor_version
def is_git_installed() -> bool:
""" Checks if git is installed
Returns: True if git is installed, False if not
"""
try:
# The "stdout" and "stderr" arguments will make subprocess capture the output and error
# The "shell" argument is set to True to run the command in the shell
subprocess.check_output("git --version", stderr=subprocess.STDOUT, shell=True)
return True
except Exception:
return False
def is_pkgconfig_installed() -> bool:
""" Checks if pkg-config is installed on platforms which are not windows
Returns: True if pkg-config is installed, False if not
"""
# Check if not running on Windows
if platform.system() != 'Windows':
try:
# Check if pkg-config is available
subprocess.run(['pkg-config', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except FileNotFoundError:
return False
return True
def main() -> int:
""" The main function which is called when this script is run
Returns: The return code of the process
"""
check_dependencies()
debug = False
app_name = "OpenVPCal"
version = get_version_from_python_package()
icon_file_path = os.path.join(get_python_package_resource_folder(), "icon.ico")
paths = get_python_package_folder()
additional_data = get_additional_data_from_resources()
entry_script = os.path.join(paths, "main.py")
platform_sep = get_additional_data_seperator()
additional_python_modules = {
"spg": os.path.join(get_packages_folder(), "spg", "src", "spg"),
"stageassets": os.path.join(get_packages_folder(), "stageassets", "src", "stageassets"),
"spg_icvfxpatterns": os.path.join(get_packages_folder(), "spg_icvfxpatterns", "src", "spg_icvfxpatterns"),
}
if debug:
cmds = ["pyinstaller", "--debug", "all"]
else:
cmds = ["pyinstaller"]
if platform.system() != 'Windows':
cmds.append("-w")
cmds.extend(
["--icon", icon_file_path, "--name", app_name, "--noconfirm", "--clean", "--onedir", "--paths", paths]
)
for folder_name, additional_module in additional_python_modules.items():
cmds.append("--add-data")
cmds.append(f"{additional_module}{platform_sep}{folder_name}")
for add_data in additional_data:
cmds.append("--add-data")
cmds.append(add_data)
cmds.append(entry_script)
process = subprocess.Popen(cmds, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
print(process.stdout.read())
# Wait for the process to finish and get the return code.
return_code = process.wait()
if platform.system() == 'Darwin':
certificate_name = os.getenv("CODE_SIGNING_CERTIFICATE", "") or os.getenv("APPLE_SIGN_IDENTITY", "")
if certificate_name:
return_code = osx_sign_app_and_build_dmg(
app_name, certificate_name, version)
else:
print("WARNING - No CODE_SIGNING_CERTIFICATE environment variable set. Skipping code signing.")
if platform.system() == 'Windows':
return_code = build_windows_installer([], version, icon_file_path)
if platform.system() == 'Linux':
current_script_directory = get_current_folder()
distribution_folder = os.path.join(current_script_directory, "dist", "OpenVPCal")
zip_file_name = os.path.join(current_script_directory, "Output", f"OpenVPCal-{version}-Ubuntu-x86_64.zip")
shutil.make_archive(zip_file_name.replace('.zip', ''), 'zip', distribution_folder)
print('Return code:', return_code)
return return_code
def build_windows_installer(manual_paths, version, icon_path) -> int:
""" Builds the window's installer and ensures the manual files are copied to the distribution folder
Args:
manual_paths: The third party library paths we need to include
version: The version of the app we are building so we update the installer compilation instructions
icon_path: The path to icon file
Returns: The return code of the process
"""
current_script_directory = get_current_folder()
distribution_folder = os.path.join(current_script_directory, "dist", "OpenVPCal")
for manual_path in manual_paths:
target_file = os.path.join(
distribution_folder,
os.path.basename(manual_path)
)
shutil.copy(manual_path, target_file)
iss_file_name = os.path.join(current_script_directory, "OpenVPCal.iss")
iss_vars = get_iscc_app_vars(version, icon_path)
return_code = create_windows_installer(
iss_file_name,
iss_vars
)
# make a zip file of the distribution_folder called OpenVPCal-{version}-x86.zip
zip_file_name = os.path.join(current_script_directory, "Output", f"OpenVPCal-{version}-Windows-x86_64.zip")
shutil.make_archive(zip_file_name.replace('.zip', ''), 'zip', distribution_folder)
return return_code
def remove_ds_store(root_dir):
for dir_path, dir_names, filenames in os.walk(root_dir):
if '.DS_Store' in filenames:
ds_store_path = os.path.join(dir_path, '.DS_Store')
try:
os.remove(ds_store_path)
print(f'Successfully deleted: {ds_store_path}')
except Exception as e:
print(f'Failed to delete {ds_store_path}: {e}')
def osx_sign_app_and_build_dmg(app_name: str, certificate_name: str, version: str) -> int:
""" Signs the app and builds the dmg for osx
Args:
app_name: The app name
certificate_name: The name of the certificate to sign the app
version: The version number we are releasing
"""
arch = "universal"
if platform.processor() == "arm":
arch = "arm"
current_script_directory = get_current_folder()
app_path = os.path.join(current_script_directory, "dist/OpenVPCal.app")
dmg_path = os.path.join(current_script_directory, f"dist/OpenVPCal-{version}-{arch}.dmg")
remove_ds_store(app_path)
# Ensure we pick up the system theme
pinfo_file_path = os.path.join(app_path, "Contents", "Info.plist")
add_key_value_to_plist(pinfo_file_path, "NSRequiresAquaSystemAppearance", False)
# Sign The .App
# Resign The App As We Changed The pList
app_path_executable = os.path.join(app_path, "Contents", "MacOS", app_name)
print("Re Signing The App")
process = subprocess.Popen(["codesign", "--deep", "--force", "--sign", certificate_name, app_path_executable],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
print(process.stdout.read())
# Wait for the process to finish
process.wait()
print("Creating The DMG")
process = subprocess.Popen(["hdiutil", "create", "-srcfolder", app_path, dmg_path],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
print(process.stdout.read())
# Wait for the process to finish and get the return code.
process.wait()
# Code Sign The DMG also
print("Signing The DMG")
process = subprocess.Popen(["codesign", "--sign", certificate_name, dmg_path],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
print(process.stdout.read())
# Wait for the process to finish and get the return code.
return_code = process.wait()
return return_code
def check_dependencies() -> None:
""" Checks the dependencies of this script are met before it runs
"""
if not check_python_is_64_bit():
raise RuntimeError("Python must be 64 bit")
if not check_python_version():
raise RuntimeError("Python must be 3.11")
if not is_git_installed():
raise RuntimeError("Git must be installed")
if not is_pkgconfig_installed():
raise RuntimeError("pkg-config must be installed")
if __name__ == "__main__":
main()