-
Notifications
You must be signed in to change notification settings - Fork 2
/
pyinstaller_windows.spec
339 lines (306 loc) · 10.2 KB
/
pyinstaller_windows.spec
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
# -*- mode: python -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Future Modules:
from __future__ import annotations
# Built-in Modules:
import glob
import hashlib
import os
import pathlib
import shutil
import tempfile
from datetime import datetime
from typing import Any, Union
# Third-party Modules:
import PyInstaller.config
import speechlight
from knickknacks.iterables import padList
from PyInstaller.building.api import COLLECT, EXE, PYZ
from PyInstaller.building.build_main import Analysis
from PyInstaller.building.datastruct import TOC
# Mapper Modules:
from mapper import __version__ as APP_VERSION
APP_NAME: str = "Mapper Proxy"
APP_AUTHOR: str = "Nick Stockton"
APP_VERSION_TYPE: str
ORIG_DEST: str = os.path.realpath(os.path.expanduser(DISTPATH)) # type: ignore[name-defined] # NOQA: F821
RUN_FILE: str = "run_mapper_proxy.py"
isTag: bool = False
if "+" in APP_VERSION:
APP_VERSION, APP_VERSION_TYPE = APP_VERSION.split("+", 1)
APP_VERSION_TYPE = "+" + APP_VERSION_TYPE
else:
APP_VERSION_TYPE = ""
isTag = True
print(f"Using version {APP_VERSION}{APP_VERSION_TYPE}.")
# APP_VERSION_CSV should be a string containing a comma separated list of numbers in the version.
# For example, "17, 4, 5, 0" if the version is 17.4.5.
APP_VERSION_CSV: str = ", ".join(padList(APP_VERSION.split("."), padding="0", count=4, fixed=True))
APP_DEST: str = os.path.normpath(
os.path.join(
ORIG_DEST,
os.pardir,
(APP_NAME if not isTag else f"{APP_NAME}_V{APP_VERSION}{APP_VERSION_TYPE}")
.replace("-", "_")
.replace(" ", "_"),
)
)
ZIP_FILE: str
if isTag:
ZIP_FILE = APP_DEST + ".zip"
else:
ZIP_FILE = os.path.normpath(os.path.join(ORIG_DEST, os.pardir, "MapperProxy.zip"))
VERSION_FILE: str = os.path.normpath(
os.path.join(os.path.realpath(os.path.expanduser(tempfile.gettempdir())), "mpm_version.ignore")
)
PyInstaller.config.CONF["distpath"] = APP_DEST
excludes: list[str] = [
"_gtkagg",
"_tkagg",
"bsddb",
"curses",
"pywin.debugger",
"pywin.debugger.dbgcon",
"pywin.dialogs",
"tcl",
"Tkconstants",
"tkinter.constants",
"Tkinter",
"tkinter",
"multiprocessing",
"bz2",
"lzma",
"_testcapi",
"pdbunittest",
"difflib",
"pyreadline",
"optparse",
"numpy",
"PIL",
"xml",
]
dll_excludes: TOC = TOC(
[
("api-ms-win-core-console-l1-1-0.dll", None, None),
("api-ms-win-core-datetime-l1-1-0.dll", None, None),
("api-ms-win-core-debug-l1-1-0.dll", None, None),
("api-ms-win-core-errorhandling-l1-1-0.dll", None, None),
("api-ms-win-core-file-l1-1-0.dll", None, None),
("api-ms-win-core-file-l1-2-0.dll", None, None),
("api-ms-win-core-file-l2-1-0.dll", None, None),
("api-ms-win-core-handle-l1-1-0.dll", None, None),
("api-ms-win-core-heap-l1-1-0.dll", None, None),
("api-ms-win-core-interlocked-l1-1-0.dll", None, None),
("api-ms-win-core-libraryloader-l1-1-0.dll", None, None),
("api-ms-win-core-localization-l1-2-0.dll", None, None),
("api-ms-win-core-memory-l1-1-0.dll", None, None),
("api-ms-win-core-namedpipe-l1-1-0.dll", None, None),
("api-ms-win-core-processenvironment-l1-1-0.dll", None, None),
("api-ms-win-core-processthreads-l1-1-0.dll", None, None),
("api-ms-win-core-processthreads-l1-1-1.dll", None, None),
("api-ms-win-core-profile-l1-1-0.dll", None, None),
("api-ms-win-core-rtlsupport-l1-1-0.dll", None, None),
("api-ms-win-core-string-l1-1-0.dll", None, None),
("api-ms-win-core-synch-l1-1-0.dll", None, None),
("api-ms-win-core-synch-l1-2-0.dll", None, None),
("api-ms-win-core-sysinfo-l1-1-0.dll", None, None),
("api-ms-win-core-timezone-l1-1-0.dll", None, None),
("api-ms-win-core-util-l1-1-0.dll", None, None),
("api-ms-win-crt-conio-l1-1-0.dll", None, None),
("api-ms-win-crt-convert-l1-1-0.dll", None, None),
("api-ms-win-crt-environment-l1-1-0.dll", None, None),
("api-ms-win-crt-filesystem-l1-1-0.dll", None, None),
("api-ms-win-crt-heap-l1-1-0.dll", None, None),
("api-ms-win-crt-locale-l1-1-0.dll", None, None),
("api-ms-win-crt-math-l1-1-0.dll", None, None),
("api-ms-win-crt-multibyte-l1-1-0.dll", None, None),
("api-ms-win-crt-process-l1-1-0.dll", None, None),
("api-ms-win-crt-runtime-l1-1-0.dll", None, None),
("api-ms-win-crt-stdio-l1-1-0.dll", None, None),
("api-ms-win-crt-string-l1-1-0.dll", None, None),
("api-ms-win-crt-time-l1-1-0.dll", None, None),
("api-ms-win-crt-utility-l1-1-0.dll", None, None),
("tcl86t.dll", None, None),
("tk86t.dll", None, None),
("ucrtbase.dll", None, None),
("mfc140u.dll", None, None),
]
)
block_cipher: Union[Any, None] = None
version_data: str = f"""
# UTF-8
#
# For more details about fixed file info 'ffi' see:
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
VSVersionInfo(
ffi=FixedFileInfo(
# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
# Set not needed items to zero 0.
filevers=({APP_VERSION_CSV}),
prodvers=(1, 0, 0, 1),
# Contains a bitmask that specifies the valid bits 'flags'r
mask=0x3f,
# Contains a bitmask that specifies the Boolean attributes of the file.
flags=0x0,
# The operating system for which this file was designed.
# 0x4 - NT and there is no need to change it.
OS=0x40004,
# The general type of file.
# 0x1 - the file is an application.
fileType=0x1,
# The function of the file.
# 0x0 - the function is not defined for this fileType
subtype=0x0,
# Creation date and time stamp.
date=(0, 0)
),
kids=[
StringFileInfo(
[
StringTable(
u'040904B0',
[StringStruct(u'CompanyName', u'LFileDescript'),
StringStruct(u'', u'{APP_NAME} V{APP_VERSION}{APP_VERSION_TYPE}'),
StringStruct(u'FileVersion', u'{APP_VERSION}'),
StringStruct(u'LegalCopyright', u'{APP_AUTHOR}'),
StringStruct(u'OriginalFilename', u'{APP_NAME}.exe'),
StringStruct(u'ProductName', u'{APP_NAME}'),
StringStruct(u'ProductVersion', u'{APP_VERSION}')])
]),
VarFileInfo([VarStruct(u'Translation', [1033, 1200])])
]
)
"""
# Remove old build files.
shutil.rmtree(ORIG_DEST, ignore_errors=True)
shutil.rmtree(APP_DEST, ignore_errors=True)
if os.path.exists(RUN_FILE) and not os.path.isdir(RUN_FILE):
os.remove(RUN_FILE)
if os.path.exists(ZIP_FILE) and not os.path.isdir(ZIP_FILE):
os.remove(ZIP_FILE)
shutil.rmtree(VERSION_FILE, ignore_errors=True)
with open(VERSION_FILE, "w", encoding="utf-8") as f:
f.write(version_data)
run_data: str = """
from __future__ import annotations
from mapper.main import run
if __name__ == "__main__":
run()
""".lstrip()
with open(RUN_FILE, "w", encoding="utf-8") as f:
f.write(run_data)
a: Analysis = Analysis(
[RUN_FILE],
pathex=[os.path.normpath(os.path.join(APP_DEST, os.pardir))],
binaries=[],
datas=[],
hiddenimports=["certifi", "decimal", "uuid"],
hookspath=[],
runtime_hooks=[],
excludes=excludes,
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz: PYZ = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe: EXE = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name=APP_NAME,
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=False, # Not using UPX for the moment, as it can raise false positives in some antivirus software.
runtime_tmpdir=None,
console=True,
version=VERSION_FILE,
)
coll: COLLECT = COLLECT(
exe,
a.binaries - dll_excludes,
a.zipfiles,
a.datas,
strip=False,
upx=False,
name="",
)
# Remove junk.
shutil.rmtree(ORIG_DEST, ignore_errors=True)
shutil.rmtree(os.path.normpath(os.path.join(APP_DEST, os.pardir, "__pycache__")), ignore_errors=True)
wp: str = os.path.realpath(os.path.expanduser(workpath)) # type: ignore[name-defined] # NOQA: F821
shutil.rmtree(wp, ignore_errors=True)
# The directory above workpath should now be empty.
# Using os.rmdir to remove it instead of shutil.rmtree for safety.
os.rmdir(os.path.normpath(os.path.join(wp, os.pardir)))
shutil.rmtree(os.path.join(APP_DEST, "Include"), ignore_errors=True)
shutil.rmtree(os.path.join(APP_DEST, "lib2to3", "tests"), ignore_errors=True)
include_files: list[tuple[list[str], str]] = [
(
[
os.path.normpath(os.path.join(APP_DEST, os.pardir, "LICENSE.txt")),
os.path.normpath(os.path.join(APP_DEST, os.pardir, "README.md")),
],
".",
),
(
glob.glob(os.path.join(os.path.realpath(os.path.expanduser(speechlight.LIB_DIRECTORY)), "*.dll")),
"speech_libs",
),
(
glob.glob(os.path.normpath(os.path.join(APP_DEST, os.pardir, "src", "mapper_data", "*.sample"))),
"mapper_data",
),
(
glob.glob(os.path.normpath(os.path.join(APP_DEST, os.pardir, "src", "mapper_data", "*.schema"))),
"mapper_data",
),
(
glob.glob(os.path.normpath(os.path.join(APP_DEST, os.pardir, "src", "mapper_data", "tiles", "*"))),
"mapper_data/tiles",
),
]
dest_dir: str
for files, destination in include_files:
dest_dir = os.path.join(APP_DEST, destination)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
for src in files:
if os.path.exists(src) and not os.path.isdir(src):
shutil.copy(src, dest_dir)
# Oldest allowed date for zip is 1980-01-01 0:00.
zip_epoch: int = int(datetime(1980, 1, 1, 0, 0, 0).timestamp())
source_epoch: int = int(os.getenv("SOURCE_DATE_EPOCH", zip_epoch))
pdest = pathlib.Path(APP_DEST)
os.utime(pdest.resolve(), times=(source_epoch, source_epoch))
for child in pdest.rglob("*"):
os.utime(child.resolve(), times=(source_epoch, source_epoch))
print(f"Compressing the distribution to {ZIP_FILE}.")
shutil.make_archive(
base_name=os.path.splitext(ZIP_FILE)[0],
format="zip",
root_dir=os.path.normpath(os.path.join(APP_DEST, os.pardir)),
base_dir=os.path.basename(APP_DEST),
owner=None,
group=None,
)
if os.path.exists(RUN_FILE) and not os.path.isdir(RUN_FILE):
os.remove(RUN_FILE)
shutil.rmtree(APP_DEST, ignore_errors=True)
print("Generating checksums.")
hashes: dict[str, hashlib._Hash] = {
"sha256": hashlib.sha256(),
}
block_size: int = 2**16
with open(ZIP_FILE, "rb") as zf:
for block in iter(lambda: zf.read(block_size), b""):
for hash in hashes.values():
hash.update(block)
for hashtype, hash in hashes.items():
with open(f"{ZIP_FILE}.{hashtype}", "w", encoding="utf-8") as f:
f.write(f"{hash.hexdigest().lower()} *{os.path.basename(ZIP_FILE)}\n")
print("Done.")