This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyinstaller_exe.py
221 lines (175 loc) · 6.01 KB
/
pyinstaller_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
#!/usr/bin/env python3
"""Generate .exe files with PyInstaller."""
from os import devnull, getcwd, listdir, makedirs, remove
from os.path import basename, exists, join
from platform import architecture
from shutil import copy, copytree, rmtree
from subprocess import STDOUT, call
from requests import certs, get
from bbarchivist.bbconstants import (CAP, COMMITDATE, JSONDIR, LONGVERSION, VERSION)
from bbarchivist.utilities import get_seven_zip, prep_seven_zip
__author__ = "Thurask"
__license__ = "WTFPL v2"
__copyright__ = "2016-2019 Thurask"
def write_versions():
"""
Write temporary version files.
"""
with open("version.txt", "w") as afile:
afile.write(VERSION)
with open("longversion.txt", "w") as afile:
afile.write("{0}\n{1}".format(LONGVERSION, COMMITDATE))
def clean_versions():
"""
Remove temporary version files.
"""
remove("version.txt")
remove("longversion.txt")
def is_64bit():
"""
Check if system is 64-bit.
"""
is64 = True if architecture()[0] == "64bit" else False
return is64
def bit_tail():
"""
String form of 64-bit checking.
"""
tail = "x64" if is_64bit() else "x86"
return tail
def bitsdir(indir):
"""
Create directories based on indir segregated on bit type.
:param indir: Directory to modify.
:type indir: str
"""
indirx = "{0}-64".format(indir) if is_64bit() else indir
if exists(indirx):
clean_outdir(indirx)
makedirs(indirx)
return indirx
def get_ucrt_dlls():
"""
Get some magic voodoo Windows DLLs.
"""
tail = bit_tail()
pfiles = "Program Files (x86)" if tail == "x64" else "Program Files"
folder = join("C:\\", pfiles, "Windows Kits", "10", "Redist", "ucrt", "DLLs", tail)
return folder
def generate_specs():
"""
Generate pyinstaller spec files.
"""
scripts = ["archivist", "autolookup", "barlinker", "carrierchecker", "certchecker", "devloader", "downloader", "droidlookup", "droidscraper", "escreens", "kernchecker", "lazyloader", "linkgen", "metachecker", "swlookup", "tclscan", "tcldelta", "tclnewprd"]
here = getcwd().replace("\\", "\\\\")
dlldir = get_ucrt_dlls().replace("\\", "\\\\")
tail = bit_tail()
for script in scripts:
template = "# -*- mode: python -*-\n\nblock_cipher = None\n\n\na = Analysis(['bbarchivist\\\\scripts\\\\{0}.py'],\n pathex=['{1}', '{2}'],\n binaries=None,\n datas=None,\n hiddenimports=[],\n hookspath=[],\n runtime_hooks=[],\n excludes=[],\n win_no_prefer_redirects=False,\n win_private_assemblies=False,\n cipher=block_cipher)\npyz = PYZ(a.pure, a.zipped_data,\n cipher=block_cipher)\nexe = EXE(pyz,\n a.scripts,\n a.binaries,\n a.zipfiles,\n a.datas,\n name='{0}',\n debug=False,\n strip=False,\n upx=False,\n console=True )\n".format(script, here, dlldir)
with open("{0}.{1}.spec".format(script, tail), "w") as afile:
afile.write(template)
def clean_specs():
"""
Remove pyinstaller spec files.
"""
tail = bit_tail()
specs = [x for x in listdir() if x.endswith("{0}.spec".format(tail))]
for spec in specs:
remove(spec)
def get_sevenzip():
"""
Get 7-Zip.
"""
szver = "1900"
szurl = "http://www.7-zip.org/a/7z{0}-extra.7z".format(szver)
psz = prep_seven_zip()
if psz:
get_sevenzip_write(szurl)
else:
print("GO TO {0} AND DO IT MANUALLY".format(szurl))
raise SystemError
def get_sevenzip_write(szurl):
"""
Download 7-Zip file.
:param szurl: Link to 7z download.
:type szurl: str
"""
szexe = get_seven_zip()
szfile = basename(szurl)
with open(szfile, "wb") as afile:
req = get(szurl, stream=True)
for chunk in req.iter_content(chunk_size=1024):
afile.write(chunk)
cmd = "{0} x {1} -o7z".format(szexe, szfile)
with open(devnull, "wb") as dnull:
call(cmd, stdout=dnull, stderr=STDOUT, shell=True)
remove(basename(szurl))
def call_specs(distdir, builddir):
"""
Call pyinstaller to make specs.
:param distdir: Path to distribute files.
:type distdir: str
:param builddir: Path to build files.
:type builddir: str
"""
tail = bit_tail()
specs = [x for x in listdir() if x.endswith("{0}.spec".format(tail))]
for spec in specs: # use UPX 3.93 or up
cmd = "pyinstaller --onefile --workpath {2} --distpath {1} {0}".format(spec, distdir, builddir)
call(cmd, shell=True)
def sz_wrapper(outdir):
"""
Copy 7-Zip to outdir.
:param outdir: Output directory.
:type outdir: str
"""
try:
get_sevenzip()
except SystemError:
pass
else:
sz_wrapper_writer(outdir)
def sz_wrapper_writer(outdir):
"""
Copy 7-Zip to outdir, the actual function.
:param outdir: Output directory.
:type outdir: str
"""
copy(join("7z", "7za.exe"), outdir)
if is_64bit():
copy(join("7z", "x64", "7za.exe"), join(outdir, "7za64.exe"))
rmtree("7z", ignore_errors=True)
def copy_json(outdir):
"""
Copy JSON folder to outdir.
:param outdir: Output directory.
:type outdir: str
"""
copytree(JSONDIR, join(outdir, "json"))
def clean_outdir(outdir):
"""
Nuke outdir, if it exists.
:param outdir: Output directory.
:type outdir: str
"""
if exists(outdir):
rmtree(outdir, ignore_errors=True)
def main():
"""
Create .exes with dynamic spec files.
"""
outdir = bitsdir("pyinst-dist")
builddir = bitsdir("pyinst-build")
write_versions()
generate_specs()
call_specs(outdir, builddir)
copy("version.txt", outdir)
copy("longversion.txt", outdir)
copy(CAP.location, outdir)
copy_json(outdir)
copy(certs.where(), join(outdir, "cacerts.pem"))
sz_wrapper(outdir)
clean_versions()
clean_specs()
if __name__ == "__main__":
main()