Skip to content

Commit 58f83bc

Browse files
committed
Refine code
1 parent 68f232c commit 58f83bc

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

src/cli/repack.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ def autoclean_output(output, autoclean=True):
6262
exlist = set([base_prefix, prefix, exec_prefix])
6363
expats = [os.path.join(os.path.normpath(x), '*') for x in exlist]
6464
65-
src = {src}
66-
sn = len(src) + 1
65+
src = {src!r}
66+
resfile = {resfile!r}
67+
hookscript = {hookscript!r}
68+
69+
nsrc = len(src) + 1
6770
rsrc = os.path.relpath(src)
6871
rsrc = '' if rsrc == '.' else rsrc
69-
srcpat = os.path.join(src, '*')
72+
psrc = os.path.join(src, '*')
7073
7174
hiddenimports = set([])
7275
plist = set([])
@@ -76,11 +79,11 @@ def autoclean_output(output, autoclean=True):
7679
if name.startswith('pyi_rth'):
7780
continue
7881
hiddenimports.add(name)
79-
if fnmatch(path, srcpat) and not any([fnmatch(path, x) for x in expats]):
82+
if fnmatch(path, psrc) and not any([fnmatch(path, x) for x in expats]):
8083
if name.find('.') == -1 and os.path.basename(path) != '__init__.py':
81-
mlist.append(os.path.join(rsrc, path[sn:]))
84+
mlist.append(os.path.join(rsrc, path[nsrc:]))
8285
else:
83-
pkgname = os.path.dirname(path[sn:]).split(os.sep)[0]
86+
pkgname = os.path.dirname(path[nsrc:]).split(os.sep)[0]
8487
plist.add(os.path.join(rsrc, pkgname))
8588
8689
for name, path, kind in a.binaries:
@@ -91,9 +94,9 @@ def autoclean_output(output, autoclean=True):
9194
break
9295
hiddenimports.add(name.replace(os.sep, '.'))
9396
94-
with open({resfile}, 'wb') as f:
97+
with open(resfile, 'wb') as f:
9598
marshal.dump(mlist + list(plist), f)
96-
with open({hookscript}, 'w') as f:
99+
with open(hookscript, 'w') as f:
97100
f.write("hiddenimports=[%s]" % ", ".join([repr(x) for x in hiddenimports]))
98101
'''
99102

@@ -212,9 +215,9 @@ def patch_specfile(self, specfile, hookscript, resfile):
212215
raise RuntimeError('unsupported specfile "%s"' % specfile)
213216

214217
lines.append(spec_patch_code.format(
215-
src=repr(os.path.abspath(os.path.dirname(self.script))),
216-
hookscript=repr(os.path.abspath(hookscript)),
217-
resfile=repr(os.path.abspath(resfile))))
218+
src=os.path.abspath(os.path.dirname(self.script)),
219+
hookscript=os.path.abspath(hookscript),
220+
resfile=os.path.abspath(resfile)))
218221

219222
with open(specfile, 'w', encoding='utf-8') as f:
220223
f.write(''.join(lines))
@@ -228,37 +231,38 @@ def check(self):
228231
229232
def apply_pyarmor_patch():
230233
231-
srcpath = {srcpath}
232-
obfpath = {obfpath}
233-
pkgname = {rtname}
234+
srcpath = {srcpath!r}
235+
obfpath = {obfpath!r}
236+
pkgname = {rtname!r}
234237
pkgpath = os.path.join(obfpath, pkgname)
235-
extpath = os.path.join(pkgname, {extname})
238+
extpath = os.path.join(pkgname, {extname!r})
236239
237240
if hasattr(a.pure, '_code_cache'):
238241
code_cache = a.pure._code_cache
239242
else:
240243
from PyInstaller.config import CONF
241244
code_cache = CONF['code_cache'].get(id(a.pure))
242245
243-
src = os.path.normcase(srcpath)
244-
n = len(src) + 1
246+
srclist = [os.path.normcase(x) for x in srcpath]
247+
def match_obfuscated_script(orgpath):
248+
for x in srclist:
249+
if os.path.normcase(orgpath).startswith(x):
250+
return os.path.join(obfpath, orgpath[len(x)+1:])
245251
246252
count = 0
247253
for i in range(len(a.scripts)):
248-
if os.path.normcase(a.scripts[i][1]).startswith(src):
249-
x = os.path.join(obfpath, a.scripts[i][1][n:])
250-
if os.path.exists(x):
251-
a.scripts[i] = a.scripts[i][0], x, a.scripts[i][2]
252-
count += 1
254+
x = match_obfuscated_script(a.scripts[i][1])
255+
if x and os.path.exists(x):
256+
a.scripts[i] = a.scripts[i][0], x, a.scripts[i][2]
257+
count += 1
253258
if count == 0:
254259
raise RuntimeError('No obfuscated script found')
255260
256261
for i in range(len(a.pure)):
257-
if os.path.normcase(a.pure[i][1]).startswith(src):
258-
x = os.path.join(obfpath, a.pure[i][1][n:])
259-
if os.path.exists(x):
260-
code_cache.pop(a.pure[i][0], None)
261-
a.pure[i] = a.pure[i][0], x, a.pure[i][2]
262+
x = match_obfuscated_script(a.pure[i][1])
263+
if x and os.path.exists(x):
264+
code_cache.pop(a.pure[i][0], None)
265+
a.pure[i] = a.pure[i][0], x, a.pure[i][2]
262266
263267
a.pure.append((pkgname, os.path.join(pkgpath, '__init__.py'), 'PYMODULE'))
264268
a.binaries.append((extpath, os.path.join(obfpath, extpath), 'EXTENSION'))
@@ -302,10 +306,10 @@ def build(self):
302306
raise RuntimeError('no found extension `pyarmor_runtime`')
303307

304308
patch = manual_spec_patch.format(
305-
srcpath=repr(os.path.abspath(os.path.dirname(self.script))),
306-
obfpath=repr(os.path.abspath(self.obfpath)),
307-
rtname=repr(self.ctx.runtime_package_name),
308-
extname=repr(extname))
309+
srcpath=[os.path.abspath(os.path.dirname(self.script))],
310+
obfpath=os.path.abspath(self.obfpath),
311+
rtname=self.ctx.runtime_package_name,
312+
extname=extname)
309313

310314
with open(self.specfile, 'r', encoding='utf-8') as f:
311315
lines = f.readlines()

0 commit comments

Comments
 (0)