- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathrecursive-lipo
executable file
·182 lines (149 loc) · 7.16 KB
/
recursive-lipo
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
#!/usr/bin/env python3
importargparse
importfilecmp
importos
importos.path
importshutil
fromswift_build_support.swift_build_supportimportshell
defmerge_file_lists(src_root_dirs, skip_files):
"""Merges the file lists recursively from all src_root_dirs supplied,
returning the union of all file paths found.
Files matching skip_files are ignored and skipped.
"""
file_list= []
forsrc_root_dirinsrc_root_dirs:
forsrc_dir, dirs, filesinos.walk(src_root_dir):
rel_dir=os.path.relpath(src_dir, src_root_dir)
rel_files= [
os.path.join(rel_dir, file) forfileinfiles+dirs
iffilenotinskip_files]
file_list.extend(
filter(lambdafile: filenotinfile_list, rel_files))
returnfile_list
# Determine if the file paths contain any overlapping architectures.
defoverlapping_lipo_architectures(file_paths, lipo_executable):
lipo_cmd= [lipo_executable, "-archs"]
known_archs= []
forfileinfile_paths:
archs=shell.capture(lipo_cmd+ [file], echo=False).split(' ')
forarchinarchs:
arch=arch.strip()
ifarchinknown_archs:
returnTrue
known_archs.append(arch)
returnFalse
# Determine whether we should consider a file as installable.
defis_installable(file):
returnos.path.exists(file) oros.path.islink(file)
# Determine the path to the first file and where it should be installed.
defget_first_file_and_dest_path(file, src_root_dirs, dest_root_dir):
fordirinsrc_root_dirs:
orig_file=os.path.join(dir, file)
ifis_installable(orig_file):
dest_path=os.path.join(
dest_root_dir, os.path.relpath(orig_file, dir))
return (orig_file, dest_path)
returnNone
defmerge_lipo_files(src_root_dirs, file_list, dest_root_dir, verbose=False,
lipo_executable="lipo"):
"""Recursively merges and runs lipo on all files from file_list in
src_root_dirs to destRoorDir.
Any path in file_list that's a regular file or symlink is copied or lipo'ed
into the corresponding location in dest_root_dir. If the file is unique or
identical in all src_root_dirs, it's copied verbatim. If it's different,
it's lipo'ed together from all src_root_dirs into a fat binary.
Any path in file_list that's a directory in src_root_dirs results in a
corresponding subdirectory in dest_root_dir.
"""
lipo_cmd= [lipo_executable, "-create"]
forfileinfile_list:
file_paths= [
os.path.join(dir, file) fordirinsrc_root_dirs
ifis_installable(os.path.join(dir, file))]
iflen(file_paths) ==0:
print("-- Warning: Can't locate source file %s"%file)
continue
first_file_and_dest=get_first_file_and_dest_path(
file, src_root_dirs, dest_root_dir)
orig_file=first_file_and_dest[0]
dest_path=first_file_and_dest[1]
ifall([os.path.islink(item) foriteminfile_paths]):
# It's a symlink in all found instances, copy the link.
print("-- Creating symlink %s"%dest_path)
# Remove symlink if it already exists
ifos.path.islink(dest_path):
os.remove(dest_path)
os.symlink(os.readlink(file_paths[0]), dest_path)
elifall([os.path.isdir(item) foriteminfile_paths]):
# It's a subdir in all found instances. Create the destination
# subdir.
print("-- Creating subdir %s"%dest_path)
ifnotos.path.isdir(dest_path):
os.makedirs(dest_path)
elifall([os.path.isfile(item) foriteminfile_paths]):
# It's a regular file in all found instances, see if they're
# identical.
ifall([filecmp.cmp(item, file_paths[0]) foriteminfile_paths]):
# All instances are identical, just copy the unique file.
print("-- Copying file %s"%dest_path)
shutil.copy2(orig_file, dest_path)
elifall([os.access(item, os.X_OK) foriteminfile_paths]):
ifoverlapping_lipo_architectures(file_paths, lipo_executable):
# lipo will fail due to overlapping architectures, so
# copy the file directly.
print("-- Copying file verbatim %s"%dest_path)
shutil.copy2(orig_file, dest_path)
else:
# Multiple instances are different and executable, try lipo.
ifverbose:
print("-- Running lipo %s to %s"%
(file_paths, dest_path))
else:
print("-- Running lipo %s"%dest_path)
shell.call((lipo_cmd+ ["-output", dest_path] +file_paths),
echo=verbose)
else:
# Multiple instances are different, and they're not executable.
print(
"-- Warning: non-executable source files are different, "+
"skipping: %s"%file_paths)
else:
print("-- Warning: Unsupported file type, skipping: %s"%file_paths)
defmain():
parser=argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""
This script merges and applies 'lipo' to directories. For each file present in
any source directory, it will create a file in the destination directory.
If all the copies of the file in the source directories are the same,
the file is copied directly to the destination. If there are different
files in different directories, but the files are executable,
lipo is run to merge the files together. Otherwise, a warning is produced.
""")
parser.add_argument("-v", "--verbose", action='store_true',
help="Verbose output and logging")
parser.add_argument("--lipo", metavar="<path-to-lipo>",
default="lipo",
help="Path to lipo executable, default is \"lipo\"")
parser.add_argument("--skip-files", metavar="<skip-files>",
default=".DS_Store",
help="Files to ignore and skip merge/copy, default "+
"is \".DS_Store\"")
required_group=parser.add_argument_group("required arguments")
required_group.add_argument("--destination", metavar="<dest-path>",
required=True,
help="The merge destination directory")
required_group.add_argument("src_root_dirs", metavar="<src-path>",
nargs='+',
help="The source directories to merge-lipo")
args=parser.parse_args()
skip_files=args.skip_files.split()
file_list=merge_file_lists(args.src_root_dirs, skip_files)
ifargs.verbose:
print("Discovered files and dirs: %s"%file_list)
merge_lipo_files(
args.src_root_dirs, file_list, args.destination, args.verbose,
args.lipo)
return0
if__name__=='__main__':
main()