-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-python-abpaths.sh
More file actions
executable file
·177 lines (151 loc) · 4.11 KB
/
fix-python-abpaths.sh
File metadata and controls
executable file
·177 lines (151 loc) · 4.11 KB
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
#!/usr/bin/env bash
set -o errexit
trap "declare -p BASH_COMMAND; echo FAILED" ERR
function main() {
local path_before="$1"
local path_after="$2"
if [ -z "$path_before" -o -z "$path_after" ]; then
echo "USAGE: $(basename "$0") <path_before> <path_after>" >&2
exit 1
fi
if [ "${path_before: -1}" != "/" ]; then
local path_before="$path_before/"
fi
local path_after="$(normalize_path "$path_after")"
local IFS=$'\n'
local did_show_target_file=
for target_file in $(find_all_executable_files "$path_after"); do
for library_path in $(list_required_libs "$target_file"); do
startswith "$library_path" "$path_before" || continue
local relative_path="${library_path:${#path_before}}"
local num_folders_up="${target_file:${#path_after}}"
local num_folders_up="${num_folders_up#/}"
local num_folders_up="${num_folders_up//[^\/]}"
local prefix=$(repeat_str "${#num_folders_up}" ../)
if [ "$did_show_target_file" != 1 ]; then
if [ -n "$did_show_target_file" ]; then
echo
fi
echo "$target_file"
local did_show_target_file=1
fi
local replacement_library_path="@loader_path/${prefix}$relative_path"
printf '\t%q -> %q\n' "$library_path" "$replacement_library_path"
install_name_tool -change "$library_path" "$replacement_library_path" "$target_file"
done
if [ "$did_show_target_file" == 1 ]; then
local did_show_target_file=0
fi
done
}
function repeat_str() {
local num="$1"
local str="$2"
[[ "$num" =~ ^[0-9]+$ ]] || {
printf "Error: first parameter must be a positive integer: " >&2
declare -p num >&2
return 1
}
[ "$num" -eq 0 ] && return
printf "%.0s$str" $(seq 1 "$num")
}
function startswith() {
test "${1:0:${#2}}" == "$2"
}
function normalize_path() {
cd "$1" && pwd
}
function list_required_libs() {
local IFS=$'\n'
local -a libs=(
$(/Library/Developer/CommandLineTools/usr/bin/dyldinfo -dylibs "$1")
)
if [ "${libs[0]}" == "for arch x86_64:" ]; then
unset libs[0]
local libs=( "${libs[@]}" )
declare -p libs
fi
if [ "${libs[0]}" != "attributes dependent dylibs" ]; then
echo "Unrecognized output from dyldinfo" >&2
declare -p libs >&2
return 1
fi
unset libs[0]
local line_pattern='^[[:space:]]+([^[:space:]]+)$'
for library_path in "${libs[@]}"; do
if [[ "$library_path" =~ $line_pattern ]]; then
echo "${BASH_REMATCH[1]}"
else
echo "Unrecognized output from dyldinfo" >&2
declare -p library_path >&2
return 1
fi
done
}
function find_all_executable_files() {
local -a find_args=(
-type f
-not -name 'README'
-not -name 'Makefile'
-not -name '*.h'
-not -name '*.html'
-not -name '*.py'
-not -name '*.pyc'
-not -name '*.js'
-not -name '*.txt'
-not -name '*.xml'
-not -name '*.png'
-not -name '*.css'
-not -name '*.plist'
-not -name '*.tcl'
-not -name '*.a'
-not -name '*.rst'
-not -name '*.c'
-not -name '*.exe'
-not -name '*.bat'
-not -name '*.o'
-not -name '*.sh'
-not -name '*.zip'
-not -name '*.aiff'
-not -name '*.wav'
-not -name '*.tar'
-not -name '*.xsl'
-not -name '*.dtd'
-not -name '*.jpg'
-not -name '*.bmp'
-not -name '*.tiff'
-not -name '*.gif'
-not -name '*.fish'
-not -name '*.csh'
-not -name '*.ps1'
-not -name '*.psm1'
-not -name '*.pickle'
-not -name '*.ico'
-not -name '*.eps'
-not -name '*.ini'
-not -name '*.TXT'
-not -name '*.wixproj'
-not -name '*.sln'
-not -name '*.vcxproj'
-not -name '*.cpp'
-not -name '*.doc'
-not -name '*.decTest'
-not -path '*/lib/tcl8.6/encoding/*.enc'
-not -path '*/lib/tcl8.6/msgs/*.msg'
-not -path '*/lib/tk8.6/msgs/*.msg'
-not -path '*/share/doc/python*/examples/Tools/*/*.proj'
-not -path '*/share/doc/python*/examples/Tools/*/*.props'
-not -path '*/share/doc/python*/examples/Tools/*/*.wxs'
-not -path '*/share/doc/python*/examples/Tools/*/*.wxl'
-not -path '*/share/doc/python*/examples/Tools/*/*.wxl_template'
-not -path '*/share/doc/python*/examples/Tools/*/*.nuspec'
)
local IFS=$'\n'
for f in $(find "$1" "${find_args[@]}"); do
local m="$(file --brief --mime-type "$f")"
if [ "${m%%$'\n'*}" == "application/x-mach-binary" ]; then
echo "$f"
fi
done
}
main "$@"