-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.py
executable file
·323 lines (253 loc) · 10.4 KB
/
compile.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
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
#!/usr/bin/env python
# lintrans - The linear transformation visualizer
# Copyright (C) 2021-2022 D. Dyson (DoctorDalek1963)
# This program is licensed under GNU GPLv3, available here:
# <https://www.gnu.org/licenses/gpl-3.0.html>
"""A simple compile script for users to compile lintrans themselves, also used by the GitHub action."""
import argparse
import os
import re
import shlex
import shutil
import sys
from importlib import import_module
from importlib.util import find_spec
from textwrap import dedent
from typing import List
def check_dependencies() -> None:
"""Check that all dependencies are installed and if they're not, print an error and the command to install them."""
# This list contains tuples of (import_name, package_name, version_attr)
# We look for the import_name spec and if we can't find it, then we require the user to install package_name
# PyQt5 seems to be a bit complicated, so we look for PyQt5.QtCore to make sure the pacakge is properly installed
dependencies = [
('nptyping', 'nptyping==2.5.0', '__version__'),
('numpy', 'numpy==1.26.4', '__version__'),
('packaging', 'packaging==24.0', '__version__'),
('PIL', 'Pillow==10.3.0', '__version__'),
('PyInstaller', 'pyinstaller==5.13.2', '__version__'),
('PyQt5.QtCore', 'pyqt5==5.15.9', 'PYQT_VERSION_STR'),
]
unmet = []
# Thanks to David Beazley for teaching me how Python imports work
# https://www.youtube.com/watch?v=0oTh1CXRaQ0
for import_name, package_name, version_attr in dependencies:
# We don't have to import the module, we can just check if we COULD import it
if find_spec(import_name) is None:
unmet.append(shlex.quote(package_name))
else:
# Even if it's installed, we need the right version
# (I'm looking at you, nptyping)
expected_version = package_name.split('==')[1]
# This line imports the module and checks its version attribute, all programmatically
if getattr(import_module(import_name), version_attr) != expected_version:
unmet.append(shlex.quote(package_name))
lintrans_needed = find_spec('lintrans') is None
if len(unmet) == 0 and not lintrans_needed:
return
command = f'{sys.executable} -m pip install {" ".join(unmet)}'
if lintrans_needed:
if command.endswith(' '):
command = command[:-1]
command += ' -e .'
print(' ERROR: Unmet dependencies '.center(os.get_terminal_size().columns, '='))
print()
print('Please run the following command to install the needed dependencies:')
print(' ' + command)
print()
print('Then run this script again to compile.')
print()
sys.exit(1)
check_dependencies()
from PyInstaller.__main__ import run as run_pyi
import lintrans
OS_NAME_DICT = {
'darwin': 'macOS',
'linux': 'Linux',
'win32': 'Windows'
}
class Compiler:
"""A simple class to encapsulate compilation logic."""
def __init__(
self, *,
fullname: bool,
version_name: str
):
"""Create a Compiler object."""
self.version_name = version_name
self.platform = sys.platform
if fullname:
self.filename = f'lintrans-{OS_NAME_DICT[self.platform]}-{self.version_name}'
else:
self.filename = 'lintrans'
print(f'Created {self!r}')
def __repr__(self) -> str:
"""Return a simple repr of the object."""
return f'Compiler(filename={self.filename}, version_name={self.version_name}, platform={self.platform})'
def _windows_generate_version_info(self) -> None:
"""Generate version_info.txt for Windows."""
if (m := re.match(r'v?(\d+)\.(\d+)\.(\d+)(-[^ ]+)?', self.version_name)) is not None:
major, minor, patch, dev_part = m.groups()
else:
raise ValueError('Tag name must match format')
if dev_part is not None:
flags = '0x2'
else:
flags = '0x0'
version_tuple = f'{major}, {minor}, {patch}, 0'
print(f'Generating Windows version file with tuple=({version_tuple}) and dev_part={dev_part}')
version_info = dedent(f'''
VSVersionInfo(
ffi=FixedFileInfo(
filevers=({version_tuple}),
prodvers=({version_tuple}),
mask=0x3f,
flags={flags},
OS=0x40004,
fileType=0x1,
subtype=0x0,
date=(0, 0)
),
kids=[
StringFileInfo(
[
StringTable(
'040904B0',
kids=[
StringStruct('CompanyName', 'D. Dyson (DoctorDalek1963)'),
StringStruct('FileDescription', 'lintrans'),
StringStruct('FileVersion', '{self.version_name}'),
StringStruct('InternalName', 'lintrans'),
StringStruct('LegalCopyright', '(C) D. Dyson (DoctorDalek1963) under GPLv3'),
StringStruct('OriginalFilename', '{self.filename}.exe'),
StringStruct('ProductName', 'lintrans'),
StringStruct('ProductVersion', '{self.version_name}')
]
)
]
),
VarFileInfo([VarStruct('Translation', [2057, 1200])])
]
)
'''[1:])
with open('version_info.txt', 'w', encoding='utf-8') as f:
f.write(version_info)
print('Version file written to version_info.txt')
def _macos_replace_info_plist(self) -> None:
"""Replace the Info.plist file in the macOS app."""
short_version_name = self.version_name
if (m := re.match(r'v?(\d+\.\d+\.\d+)(-[^ ]+)?', short_version_name)) is not None:
short_version_name = m.group(1)
print(f'Generating macOS Info.plist with short_version_name={short_version_name}')
new_info_plist = dedent(f'''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"
<plist version="1.0">
<dict>
<key>CFBundleDisplayName</key>
<string>lintrans</string>
<key>CFBundleExecutable</key>
<string>lintrans</string>
<key>CFBundleIconFile</key>
<string>icon-windowed.icns</string>
<key>CFBundleIdentifier</key>
<string>lintrans</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>lintrans</string>
<key>CFBundleType</key>
<string>APPL</string>
<key>CFBundleVersion</key>
<string>{self.version_name}</string>
<key>CFBundleShortVersionString</key>
<string>{short_version_name}</string>
<key>NSHighResolutionCapable</key>
<true/>
<key>NSHumanReadableCopyright</key>
<string>(C) D. Dyson (DoctorDalek1963) under GPLv3</string>
</dict>
</plist>
'''[1:])
with open(os.path.join(self.filename + '.app', 'Contents', 'Info.plist'), 'w', encoding='utf-8') as f:
f.write(new_info_plist)
print(f'Info.plist replaced in {self.filename}.app')
def _get_pyi_args(self) -> List[str]:
"""Return the common args for PyInstaller."""
path_to_icon = os.path.join(os.path.dirname(__file__), 'src', 'lintrans', 'gui', 'assets', 'icon.jpg')
icon_dest = os.path.join('.', 'lintrans', 'gui', 'assets')
return [
'src/lintrans/__main__.py',
'--onefile',
'--windowed',
'--distpath=./dist',
'--workpath=./build',
'--noconfirm',
'--clean',
f'--name={self.filename}',
'--icon',
path_to_icon,
'--add-data',
os.pathsep.join([path_to_icon, icon_dest])
]
def _compile_macos(self) -> None:
"""Compile for macOS."""
run_pyi(self._get_pyi_args())
new_path = self.filename + '.app'
if os.path.isfile(new_path):
os.remove(new_path)
os.rename(os.path.join('dist', self.filename + '.app'), new_path)
self._macos_replace_info_plist()
def _compile_linux(self) -> None:
"""Compile for Linux."""
run_pyi(self._get_pyi_args())
if os.path.isfile(self.filename):
os.remove(self.filename)
os.rename(os.path.join('dist', self.filename), self.filename)
def _compile_windows(self) -> None:
"""Compile for Windows."""
self._windows_generate_version_info()
assert os.path.isfile('version_info.txt'), 'version_info.txt must exist for Windows compilation'
run_pyi([
*self._get_pyi_args(),
'--version-file',
'version_info.txt'
])
os.remove('version_info.txt')
new_path = self.filename + '.exe'
if os.path.isfile(new_path):
os.remove(new_path)
os.rename(os.path.join('dist', self.filename + '.exe'), new_path)
def compile(self) -> None:
"""Compile for the appropriate operating system."""
print(f'Compiling for platform={self.platform}')
if self.platform == 'darwin':
self._compile_macos()
elif self.platform == 'linux':
self._compile_linux()
elif self.platform == 'win32':
self._compile_windows()
else:
raise ValueError(f'Unsupported operating system "{self.platform}"')
print('Compilation finished')
shutil.rmtree('dist')
shutil.rmtree('build')
os.remove(self.filename + '.spec')
print('Auxiliary files cleaned up')
def main() -> None:
"""Run any pre-compilation, and then compile."""
parser = argparse.ArgumentParser(
description='Compile this version of lintrans for your operating system',
add_help=True
)
parser.add_argument(
'-f', '--fullname',
required=False,
default=False,
action='store_true',
help='whether to use the fullname for compilation (lintrans-platform-version) or the short name (lintrans)'
)
args = parser.parse_args()
compiler = Compiler(fullname=args.fullname, version_name=lintrans.__version__)
compiler.compile()
if __name__ == '__main__':
main()