-
Notifications
You must be signed in to change notification settings - Fork 35
/
util.py
842 lines (670 loc) · 23.1 KB
/
util.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
from __future__ import print_function
import errno
import os
import stat
import subprocess
import sys
import time
import warnings
import zipfile
from distutils import log
import macholib.util
import pkg_resources
from macholib.util import is_platform_file
from modulegraph import zipio
from modulegraph.find_modules import PY_SUFFIXES
try:
unicode
except NameError:
unicode = str
# Deprecated functionality
def os_path_islink(path):
warnings.warn("Use zipio.islink instead of os_path_islink", DeprecationWarning)
return zipio.islink(path)
def os_path_isdir(path):
warnings.warn("Use zipio.isdir instead of os_path_isdir", DeprecationWarning)
return zipio.islink(path)
def os_readlink(path):
warnings.warn("Use zipio.readlink instead of os_readlink", DeprecationWarning)
return zipio.islink(path)
def get_zip_data(path_to_zip, path_in_zip):
warnings.warn("Use zipio.open instead of get_zip_data", DeprecationWarning)
zf = zipfile.ZipFile(path_to_zip)
return zf.read(path_in_zip)
def path_to_zip(path):
"""
Returns (pathtozip, pathinzip). If path isn't in a zipfile pathtozip
will be None
"""
warnings.warn("Don't use this function", DeprecationWarning)
zf = zipfile.ZipFile(path_to_zip)
orig_path = path
from distutils.errors import DistutilsFileError
if os.path.exists(path):
return (None, path)
else:
rest = ""
while not os.path.exists(path):
path, r = os.path.split(path)
if not path:
raise DistutilsFileError("File doesn't exist: %s" % (orig_path,))
rest = os.path.join(r, rest)
if not os.path.isfile(path):
# Directory really doesn't exist
raise DistutilsFileError("File doesn't exist: %s" % (orig_path,))
try:
zf = zipfile.ZipFile(path)
zf.close()
except zipfile.BadZipfile:
raise DistutilsFileError("File doesn't exist: %s" % (orig_path,))
if rest.endswith("/"):
rest = rest[:-1]
return path, rest
def get_mtime(path, mustExist=True):
"""
Get mtime of a path, even if it is inside a zipfile
"""
warnings.warn("Don't use this function", DeprecationWarning)
try:
return zipio.getmtime(path)
except IOError:
if not mustExist:
return -1
raise
# End deprecated functionality
gConverterTab = {}
def find_converter(source):
if not gConverterTab:
for ep in pkg_resources.iter_entry_points("py2app.converter"):
function = ep.load()
if not hasattr(function, "py2app_suffix"):
print(
"WARNING: %s does not have 'py2app_suffix' attribute" % (function)
)
continue
gConverterTab[function.py2app_suffix] = function
basename, suffix = os.path.splitext(source)
try:
return gConverterTab[suffix]
except KeyError:
return None
def copy_resource(source, destination, dry_run=0, symlink=0):
"""
Copy a resource file into the application bundle
"""
converter = find_converter(source)
if converter is not None:
converter(source, destination, dry_run=dry_run)
return
if os.path.isdir(source):
if not dry_run:
if not os.path.exists(destination):
os.mkdir(destination)
for fn in zipio.listdir(source):
copy_resource(
os.path.join(source, fn),
os.path.join(destination, fn),
dry_run=dry_run,
symlink=symlink,
)
else:
if symlink:
if not dry_run:
make_symlink(os.path.abspath(source), destination)
else:
copy_file(source, destination, dry_run=dry_run, preserve_mode=True)
def copy_file(
source,
destination,
preserve_mode=False,
preserve_times=False,
update=False,
dry_run=0,
):
while True:
try:
_copy_file(
source, destination, preserve_mode, preserve_times, update, dry_run
)
return
except IOError as exc:
if exc.errno != errno.EAGAIN:
raise
log.info(
"copying file %s failed due to spurious EAGAIN, "
"retrying in 2 seconds",
source,
)
time.sleep(2)
def _copy_file(
source,
destination,
preserve_mode=False,
preserve_times=False,
update=False,
dry_run=0,
):
log.info("copying file %s -> %s", source, destination)
with zipio.open(source, "rb") as fp_in:
if not dry_run:
if os.path.isdir(destination):
destination = os.path.join(destination, os.path.basename(source))
if os.path.exists(destination):
os.unlink(destination)
with open(destination, "wb") as fp_out:
data = fp_in.read()
fp_out.write(data)
if preserve_mode:
mode = None
if hasattr(zipio, "getmode"):
mode = zipio.getmode(source)
elif os.path.isfile(source):
mode = stat.S_IMODE(os.stat(source).st_mode)
if mode is not None:
os.chmod(destination, mode)
if preserve_times:
mtime = zipio.getmtime(source)
os.utime(destination, (mtime, mtime))
def make_symlink(source, target):
if os.path.islink(target):
os.unlink(target)
os.symlink(source, target)
def newer(source, target):
"""
distutils.dep_utils.newer with zipfile support
"""
try:
return zipio.getmtime(source) > zipio.getmtime(target)
except IOError:
return True
def find_version(fn):
"""
Try to find a __version__ assignment in a source file
"""
return "0.0.0"
import compiler
from compiler.ast import Assign, AssName, Const, Module, Stmt
ast = compiler.parseFile(fn)
if not isinstance(ast, Module):
raise ValueError("expecting Module")
statements = ast.getChildNodes()
if not (len(statements) == 1 and isinstance(statements[0], Stmt)):
raise ValueError("expecting one Stmt")
for node in statements[0].getChildNodes():
if not isinstance(node, Assign):
continue
if not len(node.nodes) == 1:
continue
assName = node.nodes[0]
if not (
isinstance(assName, AssName)
and isinstance(node.expr, Const)
and assName.flags == "OP_ASSIGN"
and assName.name == "__version__"
):
continue
return node.expr.value
else:
raise ValueError("Version not found")
def in_system_path(filename):
"""
Return True if the file is in a system path
"""
return macholib.util.in_system_path(filename)
if sys.version_info[0] == 2:
def fsencoding(s, encoding=sys.getfilesystemencoding()): # noqa: M511,B008
return macholib.util.fsencoding(s, encoding=encoding)
else:
def fsencoding(s, encoding=sys.getfilesystemencoding()): # noqa: M511,B008
return s
def make_exec(path):
mask = os.umask(0)
os.umask(mask)
os.chmod(path, os.stat(path).st_mode | (0o111 & ~mask))
def makedirs(path):
path = fsencoding(path)
if not os.path.exists(path):
os.makedirs(path)
def mergecopy(src, dest):
return macholib.util.mergecopy(src, dest)
def mergetree(src, dst, condition=None, copyfn=mergecopy):
"""Recursively merge a directory tree using mergecopy()."""
return macholib.util.mergetree(src, dst, condition=condition, copyfn=copyfn)
def move(src, dst):
return macholib.util.move(src, dst)
def copy2(src, dst):
return macholib.util.copy2(src, dst)
def fancy_split(s, sep=","):
# a split which also strips whitespace from the items
# passing a list or tuple will return it unchanged
if s is None:
return []
if hasattr(s, "split"):
return [item.strip() for item in s.split(sep)]
return s
class FileSet(object):
# A case insensitive but case preserving set of files
def __init__(self, iterable=None):
self._dict = {}
if iterable is not None:
for arg in iterable:
self.add(arg)
def __repr__(self):
return "<FileSet %s at %x>" % (self._dict.values(), id(self))
def add(self, fname):
self._dict[fname.upper()] = fname
def remove(self, fname):
del self._dict[fname.upper()]
def __contains__(self, fname):
return fname.upper() in self._dict.keys()
def __getitem__(self, index):
key = self._dict.keys()[index]
return self._dict[key]
def __len__(self):
return len(self._dict)
def copy(self):
res = FileSet()
res._dict.update(self._dict)
return res
LOADER = """
def __load():
import imp, os, sys
ext = %r
for path in sys.path:
if not path.endswith('lib-dynload'):
continue
ext_path = os.path.join(path, ext)
if os.path.exists(ext_path):
mod = imp.load_dynamic(__name__, ext_path)
break
else:
raise ImportError(repr(ext) + " not found")
__load()
del __load
"""
def make_loader(fn):
return LOADER % fn
def byte_compile(
py_files, optimize=0, force=0, target_dir=None, verbose=1, dry_run=0, direct=None
):
if direct is None:
direct = __debug__ and optimize == 0
# "Indirect" byte-compilation: write a temporary script and then
# run it with the appropriate flags.
if not direct:
from distutils.util import execute, spawn
from tempfile import NamedTemporaryFile
if verbose:
print("writing byte-compilation script")
if not dry_run:
with NamedTemporaryFile(suffix=".py", delete=False, mode="w",
encoding="utf-8") as script:
script_name = script.name
script.write(
"""
from py2app.util import byte_compile
from modulegraph.modulegraph import *
files = [
"""
)
for f in py_files:
script.write(repr(f) + ",\n")
script.write("]\n")
script.write(
"""
byte_compile(files, optimize=%r, force=%r,
target_dir=%r,
verbose=%r, dry_run=0,
direct=1)
"""
% (optimize, force, target_dir, verbose)
)
# Ensure that py2app is on PYTHONPATH, this ensures that
# py2app.util can be found even when we're running from
# an .egg that was downloaded by setuptools
import py2app
pp = os.path.dirname(os.path.dirname(py2app.__file__))
if "PYTHONPATH" in os.environ:
pp = "%s:%s" % (pp, os.environ["PYTHONPATH"])
cmd = ["/usr/bin/env", "PYTHONPATH=%s" % (pp,), sys.executable, script_name]
if optimize == 1:
cmd.insert(3, "-O")
elif optimize == 2:
cmd.insert(3, "-OO")
spawn(cmd, verbose=verbose, dry_run=dry_run)
execute(
os.remove,
(script_name,),
"removing %s" % script_name,
verbose=verbose,
dry_run=dry_run,
)
else:
from distutils.dir_util import mkpath
from py_compile import compile
for mod in py_files:
# Terminology from the py_compile module:
# cfile - byte-compiled file
# dfile - purported source filename (same as 'file' by default)
if mod.filename == mod.identifier:
cfile = os.path.basename(mod.filename)
dfile = cfile + (__debug__ and "c" or "o")
else:
cfile = mod.identifier.replace(".", os.sep)
if sys.version_info[:2] <= (3, 4):
if mod.packagepath:
dfile = (
cfile + os.sep + "__init__.py" + (__debug__ and "c" or "o")
)
else:
dfile = cfile + ".py" + (__debug__ and "c" or "o")
else:
if mod.packagepath:
dfile = cfile + os.sep + "__init__.pyc"
else:
dfile = cfile + ".pyc"
if target_dir:
cfile = os.path.join(target_dir, dfile)
if force or newer(mod.filename, cfile):
if verbose:
print("byte-compiling %s to %s" % (mod.filename, dfile))
if not dry_run:
mkpath(os.path.dirname(cfile))
suffix = os.path.splitext(mod.filename)[1]
if suffix in (".py", ".pyw"):
fn = cfile + ".py"
with zipio.open(mod.filename, "rb") as fp_in:
with open(fn, "wb") as fp_out:
fp_out.write(fp_in.read())
compile(fn, cfile, dfile)
os.unlink(fn)
elif suffix in PY_SUFFIXES:
# Minor problem: This will happily copy a file
# <mod>.pyo to <mod>.pyc or <mod>.pyc to
# <mod>.pyo, but it does seem to work.
copy_file(mod.filename, cfile, preserve_times=True)
else:
raise RuntimeError("Don't know how to handle %r" % mod.filename)
else:
if verbose:
print(
"skipping byte-compilation of %s to %s" % (mod.filename, dfile)
)
SCMDIRS = ["CVS", ".svn", ".hg", ".git"]
def skipscm(ofn):
ofn = fsencoding(ofn)
fn = os.path.basename(ofn)
if fn in SCMDIRS:
return False
return True
def skipfunc(junk=(), junk_exts=(), chain=()):
junk = set(junk)
junk_exts = set(junk_exts)
chain = tuple(chain)
def _skipfunc(fn):
if os.path.basename(fn) in junk:
return False
elif os.path.splitext(fn)[1] in junk_exts:
return False
for func in chain:
if not func(fn):
return False
else:
return True
return _skipfunc
JUNK = [".DS_Store", ".gdb_history", "build", "dist"] + SCMDIRS
JUNK_EXTS = [".pbxuser", ".pyc", ".pyo", ".swp"]
skipjunk = skipfunc(JUNK, JUNK_EXTS)
def get_magic(platform=sys.platform):
if platform == "darwin":
import struct
import macholib.mach_o
return [
struct.pack("!L", macholib.mach_o.MH_MAGIC),
struct.pack("!L", macholib.mach_o.MH_CIGAM),
struct.pack("!L", macholib.mach_o.MH_MAGIC_64),
struct.pack("!L", macholib.mach_o.MH_CIGAM_64),
struct.pack("!L", macholib.mach_o.FAT_MAGIC),
]
elif platform == "linux2":
return ["\x7fELF"]
elif platform == "win32":
return ["MZ"]
return None
def iter_platform_files(path, is_platform_file=macholib.util.is_platform_file):
"""
Iterate over all of the platform files in a directory
"""
for root, _dirs, files in os.walk(path):
for fn in files:
fn = os.path.join(root, fn)
if is_platform_file(fn):
yield fn
def strip_files(files, dry_run=0, verbose=0):
"""
Strip the given set of files
"""
if dry_run:
return
return macholib.util.strip_files(files)
def copy_tree(
src,
dst,
preserve_mode=1,
preserve_times=1,
preserve_symlinks=0,
update=0,
verbose=0,
dry_run=0,
condition=None,
):
"""
Copy an entire directory tree 'src' to a new location 'dst'. Both
'src' and 'dst' must be directory names. If 'src' is not a
directory, raise DistutilsFileError. If 'dst' does not exist, it is
created with 'mkpath()'. The end result of the copy is that every
file in 'src' is copied to 'dst', and directories under 'src' are
recursively copied to 'dst'. Return the list of files that were
copied or might have been copied, using their output name. The
return value is unaffected by 'update' or 'dry_run': it is simply
the list of all files under 'src', with the names changed to be
under 'dst'.
'preserve_mode' and 'preserve_times' are the same as for
'copy_file'; note that they only apply to regular files, not to
directories. If 'preserve_symlinks' is true, symlinks will be
copied as symlinks (on platforms that support them!); otherwise
(the default), the destination of the symlink will be copied.
'update' and 'verbose' are the same as for 'copy_file'.
"""
assert isinstance(src, (str, unicode)), repr(src)
assert isinstance(dst, (str, unicode)), repr(dst)
from distutils import log
from distutils.dep_util import newer
from distutils.dir_util import mkpath
from distutils.errors import DistutilsFileError
src = fsencoding(src)
dst = fsencoding(dst)
if condition is None:
condition = skipscm
if not dry_run and not zipio.isdir(src):
raise DistutilsFileError("cannot copy tree '%s': not a directory" % src)
try:
names = zipio.listdir(src)
except os.error as exc:
(errno, errstr) = exc.args
if dry_run:
names = []
else:
raise DistutilsFileError("error listing files in '%s': %s" % (src, errstr))
if not dry_run:
mkpath(dst)
outputs = []
for n in names:
src_name = os.path.join(src, n)
dst_name = os.path.join(dst, n)
if (condition is not None) and (not condition(src_name)):
continue
# Note: using zipio's internal _locate function throws an IOError on
# dead symlinks, so handle it here.
if os.path.islink(src_name) and not os.path.exists(
os.path.join(src, os.readlink(src_name))
):
continue
if preserve_symlinks and zipio.islink(src_name):
link_dest = zipio.readlink(src_name)
log.info("linking %s -> %s", dst_name, link_dest)
if not dry_run:
if update and not newer(src, dst_name):
pass
else:
make_symlink(link_dest, dst_name)
outputs.append(dst_name)
elif zipio.isdir(src_name) and not os.path.isfile(src_name):
# ^^^ this odd tests ensures that resource files that
# happen to be a zipfile won't get extracted.
outputs.extend(
copy_tree(
src_name,
dst_name,
preserve_mode,
preserve_times,
preserve_symlinks,
update,
dry_run=dry_run,
condition=condition,
)
)
else:
copy_file(
src_name,
dst_name,
preserve_mode,
preserve_times,
update,
dry_run=dry_run,
)
outputs.append(dst_name)
return outputs
def walk_files(path):
for _root, _dirs, files in os.walk(path):
for f in files:
yield f
def find_app(app):
dpath = os.path.realpath(app)
if os.path.exists(dpath):
return dpath
if os.path.isabs(app):
return None
for path in os.environ.get("PATH", "").split(":"):
dpath = os.path.realpath(os.path.join(path, app))
if os.path.exists(dpath):
return dpath
return None
def check_output(command_line):
p = subprocess.Popen(command_line, stdout=subprocess.PIPE)
stdout, _ = p.communicate()
xit = p.wait()
if xit != 0:
raise subprocess.CalledProcessError(xit, command_line)
return stdout
_tools = {}
def _get_tool(toolname):
if toolname not in _tools:
if os.path.exists("/usr/bin/xcrun"):
try:
_tools[toolname] = check_output(["/usr/bin/xcrun", "-find", toolname])[
:-1
]
except subprocess.CalledProcessError:
raise IOError("Tool %r not found" % (toolname,))
else:
# Support for Xcode 3.x and earlier
if toolname == "momc":
choices = [
(
"/Library/Application Support/Apple/"
"Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/"
"Contents/Resources/momc"
),
(
"/Developer/Library/Xcode/Plug-ins/"
"XDCoreDataModel.xdplugin/Contents/Resources/momc"
),
"/Developer/usr/bin/momc",
]
elif toolname == "mapc":
choices = [
(
"/Developer/Library/Xcode/Plug-ins/"
"XDMappingModel.xdplugin/"
"Contents/Resources/mapc",
),
"/Developer/usr/bin/mapc",
]
else:
raise IOError("Tool %r not found" % (toolname,))
for fn in choices:
if os.path.exists(fn):
_tools[toolname] = fn
break
else:
raise IOError("Tool %r not found" % (toolname,))
return _tools[toolname]
def momc(src, dst):
subprocess.check_call([_get_tool("momc"), src, dst])
def mapc(src, dst):
subprocess.check_call([_get_tool("mapc"), src, dst])
def _macho_find(path):
for basename, _dirs, files in os.walk(path):
for fn in files:
path = os.path.join(basename, fn)
if is_platform_file(path):
yield path
def _dosign(*path):
subprocess.check_call(
(
"codesign",
"-s",
"-",
"--preserve-metadata=identifier,entitlements,flags,runtime",
"-f",
"-vvvv",
)
+ path,
)
def codesign_adhoc(bundle):
"""
(Re)sign a bundle
Signing should be done "depth-first", sign
libraries before signing the libraries/executables
linking to them.
The current implementation is a crude hack,
but is better than nothing. Signing properly requires
performing a topological sort using dependencies.
"codesign" will resign the entire bundle, but only
if partial signatures are valid.
"""
# try:
# _dosign(bundle)
# return
# except subprocess.CalledProcessError:
# pass
platfiles = list(_macho_find(bundle))
print("sign", platfiles)
while platfiles:
for file in platfiles:
failed = []
try:
_dosign(file)
except subprocess.CalledProcessError:
failed.append(file)
if failed == platfiles:
raise RuntimeError("Cannot sign bundle %r" % (bundle,))
platfiles = failed
for _ in range(5):
try:
_dosign(bundle)
break
except subprocess.CalledProcessError:
time.sleep(1)
continue