-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyinstailor.py
325 lines (247 loc) · 10.1 KB
/
pyinstailor.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
324
325
'''
pyinstailor is a tailor to replace files directly in the executable
file generated by PyInstaller. Sometimes the script is changed a
little, for example, only refine log messages, no dependency changed,
no analysis is required. In this case, pyinstailor could extract the
executable, replace the old script with new one, then repack it.
Usage
pyinstailor.py [-h] [-d] [-s N] executable [files]
Examples
* List all the available items in the archive
pyinstailor dist/foo.exe
* Replace entry script "foo.py" in the bundle "foo.exe"
pyinstailor dist/foo.exe foo.py
* If entry scrip is in other path, strip the first N path
pyinstailor -s 1 dist/foo.exe src/foo.py
* Replace package file "reader/__init__.py" in the bundle "foo.exe"
pyinstailor dist/foo.exe reader/__init__.py
* Strip the first path in the file path
pyinstailor -s 2 dist/foo.exe ../../reader/__init__.py
This tool doesn't support PyInstaller 2.0, and it's not full test.
'''
import argparse
import logging
import marshal
import os
import shutil
import struct
import sys
import zlib
from subprocess import Popen
from PyInstaller.archive.writers import ZlibArchiveWriter, CArchiveWriter
from PyInstaller.archive.readers import CArchiveReader
from PyInstaller.loader.pyimod02_archive import ZlibArchiveReader, PYZ_TYPE_PKG
from PyInstaller.compat import is_darwin, is_linux
logger = logging.getLogger('pyinstailor')
def makedirs(path, exist_ok=False):
if not (exist_ok and os.path.exists(path)):
os.makedirs(path)
class ZlibArchive(ZlibArchiveReader):
def checkmagic(self):
""" Overridable.
Check to see if the file object self.lib actually has a file
we understand.
"""
self.lib.seek(self.start) # default - magic is at start of file.
if self.lib.read(len(self.MAGIC)) != self.MAGIC:
raise RuntimeError("%s is not a valid %s archive file"
% (self.path, self.__class__.__name__))
if self.lib.read(len(self.pymagic)) != self.pymagic:
print("Warning: pyz is from a different Python version")
self.lib.read(4)
class CArchiveWriter2(CArchiveWriter):
def add(self, entry):
patched, dlen, ulen, flag, typcd, nm, pathnm = entry
where = self.lib.tell()
logger.debug('Handle item "%s"', nm)
if is_darwin and patched and typcd == 'b':
from PyInstaller.depend import dylib
dylib.mac_set_relative_dylib_deps(pathnm, os.path.basename(pathnm))
fh = open(pathnm, 'rb')
filedata = fh.read()
fh.close()
if patched:
logger.info('Patch item "%s" with "%s"', nm, pathnm)
if typcd in ('s', 'M'):
code = compile(filedata, '<%s>' % nm, 'exec')
filedata = marshal.dumps(code)
ulen = len(filedata)
else:
ulen = len(filedata)
if flag == 1 and patched:
comprobj = zlib.compressobj(self.LEVEL)
self.lib.write(comprobj.compress(filedata))
self.lib.write(comprobj.flush())
else:
self.lib.write(filedata)
dlen = self.lib.tell() - where
self.toc.add(where, dlen, ulen, flag, typcd, nm)
def get_carchive_info(filepath):
PYINST_COOKIE_SIZE = 24 + 64 # For pyinstaller 2.1+
fp = open(filepath, 'rb')
size = os.stat(filepath).st_size
fp.seek(size - PYINST_COOKIE_SIZE, os.SEEK_SET)
# Read CArchive cookie
magic, lengthofPackage, toc, tocLen, pyver, pylibname = \
struct.unpack('!8siiii64s', fp.read(PYINST_COOKIE_SIZE))
fp.close()
# Overlay is the data appended at the end of the PE
pos = size - lengthofPackage
return pos, pylibname.decode()
def repack_pyz(pyz, items, cipher=None):
logger.info('Patching PYZ file "%s"', pyz)
arch = ZlibArchive(pyz)
updated = 0
def compile_code(name, pyfile):
logger.info('Compile %s', pyfile)
with open(pyfile, 'r') as f:
return compile(f.read(), '<%s>' % name, 'exec')
code_dict = {}
logic_toc = []
for name in arch.toc:
logger.debug('Extract %s', name)
typ, obj = arch.extract(name)
if name in items:
logger.info('Update item "%s"', name)
code_dict[name] = compile_code(name, items[name])
items.pop(name)
updated += 1
else:
code_dict[name] = obj
pathname = '__init__.py' if typ == PYZ_TYPE_PKG else name
logic_toc.append((name, pathname, 'PYMODULE'))
ZlibArchiveWriter(pyz, logic_toc, code_dict=code_dict, cipher=cipher)
logger.info('Patch PYZ done')
return updated
def repack_exe(path, output, logic_toc):
logger.info('Repacking EXE "%s"', output)
offset, pylib_name = get_carchive_info(output)
logger.info('Get archive info (%d, "%s")', offset, pylib_name)
pkgname = os.path.join(path, 'PKG-patched')
logging.info('Patch PKG file "%s"', pkgname)
CArchiveWriter2(pkgname, logic_toc, pylib_name=pylib_name)
if is_linux:
logger.info('Update section pydata in EXE')
Popen(['objcopy', '--update-section', 'pydata=%s' % pkgname, output])
else:
logger.info('Update patched PKG in EXE')
with open(output, 'r+b') as outf:
# Keep bootloader
outf.seek(offset, os.SEEK_SET)
# write the patched archive
with open(pkgname, 'rb') as infh:
shutil.copyfileobj(infh, outf, length=64*1024)
outf.truncate()
if is_darwin:
# Fix Mach-O header for codesigning on OS X.
logger.info('Fixing EXE for code signing "%s"', output)
import PyInstaller.utils.osx as osxutils
osxutils.fix_exe_for_code_signing(output)
logger.info('Generate patched bundle "%s" successfully', output)
def repacker(executable, items):
logger.info('Repack PyInstaller bundle "%s"', executable)
name, ext = os.path.splitext(os.path.basename(executable))
arch = CArchiveReader(executable)
logic_toc = []
path = os.path.join(name + '_extracted')
logger.info('Extracted bundle files to "%s"', path)
makedirs(path, exist_ok=True)
for toc in arch.toc:
logger.debug('toc: %s', toc)
dpos, dlen, ulen, flag, typcd, nm = toc
pathnm = os.path.join(path, nm)
makedirs(os.path.dirname(pathnm), exist_ok=True)
with arch.lib:
arch.lib.seek(arch.pkg_start + dpos)
with open(pathnm, 'wb') as f:
f.write(arch.lib.read(dlen))
if nm.endswith('.pyz') and typcd in ('z', 'Z'):
logger.info('Extract pyz file "%s"', pathnm)
patched = repack_pyz(pathnm, items)
elif nm in items:
patched = 1
pathnm = items[nm]
else:
patched = 0
logic_toc.append((patched, dlen, ulen, flag, typcd, nm, pathnm))
output = os.path.join(name + '-patched' + ext)
logger.info('Copy "%s" to "%s"', executable, output)
shutil.copy2(executable, output)
repack_exe(path, output, logic_toc)
def print_archive_items(executable):
logger.info('PyInstaller bundle is "%s"', executable)
path = os.path.join(os.path.basename(executable) + '_extracted')
logger.info('Extracted bundle files to "%s"', path)
makedirs(path, exist_ok=True)
logger.info('Got items from PKG')
arch = CArchiveReader(executable)
pyzlist = []
for toc in arch.toc:
logger.debug('toc: %s', toc)
dpos, dlen, ulen, flag, typcd, nm = toc
if nm.endswith('.pyz') and typcd in ('z', 'Z'):
pathnm = os.path.join(path, nm)
makedirs(os.path.dirname(pathnm), exist_ok=True)
with arch.lib:
arch.lib.seek(arch.pkg_start + dpos)
with open(pathnm, 'wb') as f:
f.write(arch.lib.read(dlen))
pyzlist.append(pathnm)
else:
logger.info(' %s (%d)', nm, ulen)
for pyz in pyzlist:
logger.info('Got items from "%s"', pyz)
arch = ZlibArchive(pyz)
for name in arch.toc:
logger.info(' %s', name)
def build_updated_items(files, strip=None):
items = {}
for filename in files:
if filename.find(os.pathsep) > 0:
name, pathnm = filename.split(os.pathsep, 1)
pathnm = os.path.normpath(filename)
else:
pathnm = os.path.normpath(filename)
namelist = pathnm.split(os.sep)
if strip is None:
name = namelist[-2 if pathnm.endswith('__init__.py') else -1]
else:
name = '.'.join(namelist[strip:])
for suffix in ('.py', '__init__.py'):
if name.endswith(suffix):
name = name[:-len(suffix)].strip('.')
items[name] = pathnm
def excepthook(type, value, traceback):
if hasattr(value, 'args') and isinstance(str, value.args[0]):
logger.error(*value.args)
else:
logger.error('%s', value)
sys.exit(1)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug',
default=False,
action='store_true',
dest='debug',
help='print debug log (default: %(default)s)')
parser.add_argument('-s', '--strip',
type=int, default=0, metavar='N',
help='strip the first Nth path from filename')
parser.add_argument('executable', metavar='executable',
help="PyInstaller archive")
parser.add_argument('files',
nargs='?',
help='updated files')
args = parser.parse_args(sys.argv[1:])
if args.debug:
logger.setLevel(logging.DEBUG)
else:
sys.excepthook = excepthook
if args.files is None:
print_archive_items(args.executable)
return
items = build_updated_items(args.files, strip=args.strip)
logging.info('Being updated items %s', items.keys())
repacker(args.executable, items)
if __name__ == '__main__':
main()