This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslackdeps-elf-python.sh
executable file
·374 lines (321 loc) · 9.15 KB
/
slackdeps-elf-python.sh
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
#!/bin/bash
#
# slackdeps - a script that finds Slackware package dependencies.
#
# Written by ivandi
# The _parse_python function is copied from depfinder (parsepython)
# depfinder is written by George Vlahavas from SalixOS
#
# Licensed under the GPL v3
LANG=C
LC_ALL=C
CWD=$(pwd)
TMP=/tmp/$(basename $0).$$
LIBS=$TMP/LIBS
FILES=$TMP/FILES
PYMODS=$TMP/PYMODS
DEPS=$TMP/DEPS
ADM=/var/lib/pkgtools
[ -d $ADM ] || ADM=/var/log
[ -d $ADM ] || exit 1
DEPENDENCIES=$CWD/pkg_dependencies
MISSING=$CWD/pkg_missing
PKG=""
_clean(){
rm -rf $TMP
}
_die(){
_clean
exit
}
_list_files_pkg(){
local f nf
for f in $(cat $ADM/packages/$(basename $PKG) | \
sed -n "/^\.\//,/^$/p" | \
grep -v "/$" | \
sed 's/^bin\/bash4/bin\/bash/g' | \
awk '{print "/"$1}' \
) ; do
if [ -f $f ] ; then
echo $f
else
nf=${f%.new}
[ -f $nf ] && echo $nf
fi
done \
> $FILES
}
_list_files(){
_list_files_pkg
}
_list_elf_files(){
local f
for f in $(cat $FILES | \
grep "^/bin/\|^/sbin/\|^/lib64/\|^/lib/\|^/usr/bin/\|^/usr/sbin/\|^/usr/lib64/\|^/usr/lib/\|^/usr/libexec/" \
) ; do
file $f | grep -qi ".*ELF.*LSB.*dynamically linked" && echo $f
done \
> $FILES-ELFFILES
}
_list_libs_ldd(){
local exe
for exe in $(<$FILES-ELFFILES) ; do
ldd $exe 2>/dev/null
done \
> $LIBS-$$
cat $LIBS-$$ | \
awk '/ => \// {print $3}' | \
xargs -r readlink -e | \
cut -b2- | \
sort -u \
> $LIBS
cat $LIBS-$$ | \
awk '/ => not found/ {print $1}' | \
sort -u \
> $LIBS-MISSING
}
_list_libs_readelf(){
local exe lib l
for exe in $(<$FILES-ELFFILES) ; do
readelf -d $exe 2>/dev/null
done \
> $LIBS-$$
for lib in $(cat $LIBS-$$ | \
grep "NEEDED" | grep "Shared library" | \
cut -f2 -d '[' | cut -f1 -d ']' | \
sort -u) ; do
l=$(find \
/lib{,64} \
/usr/lib{,64} \
/usr/lib{,64}/perl?/* \
/usr/lib{,64}/perl?/*/* \
/usr/lib{,64}/perl?/*/*/* \
-maxdepth 1 \
-name $lib -print -quit \
2>/dev/null)
if [ -n "$l" ] ; then
readlink -e $l | \
sort -u | \
cut -b2- \
>> $LIBS
else
echo $lib \
>> $LIBS-MISSING
fi
done
}
_list_libs(){
if [ -x /usr/bin/readelf ]; then
_list_libs_readelf
elif [ -x /usr/bin/ldd ]; then
_list_libs_ldd
fi
}
_depends_on_libs(){
local lib
if [ -n "$(<$LIBS)" ]; then
for lib in $(<$LIBS) ; do
grep -q "/$lib$" $FILES && continue
(
cd $ADM/packages
grep -l $lib * || \
grep -l "/$(basename $lib)$" *
) | \
rev | cut -f4- -d '-' | rev | \
grep -v "aaa_elflibs" | \
grep -v "glibc" | \
tr \\n \| | rev | cut -b2- | rev
done | \
sort -u \
> $DEPS-LIBS
fi
if [ -n "$(<$LIBS-MISSING)" ]; then
for lib in $(<$LIBS-MISSING) ; do
grep -q "/$lib" $FILES && continue
echo $lib
done | \
sort -u | \
awk '{print "solib:" $1}' \
> $DEPS-LIBS-MISSING
fi
}
_parse_python(){
# Copied from George Vlahavas's depfinder
sed "s/;/\n/g" | \
sed -e :a -e '/\\$/N; s/\\\n//; ta' | \
sed "s/[ \t]\+/ /g" | \
grep "^ *import \|^ *from .* import " | \
sed "s/#/@COMMENT@/" | \
sed "s/\(.*\)@COMMENT@.*/\1/" | \
sed "s/.*__import__('\(.*\)').*/import \1/" | \
sed "s/from \(.*\) import .*/\1/" | \
sed "s/^ *import \(.*\)/\1/" | \
sed "s/\(.*\) as .*/\1/" | \
sed "s/^[ \t]*//;s/[ \t]*$//" | \
sed "/ [^,]\+ /d" | \
sed "s/,/\n/g" | \
sed "s/^[ \t]*//" | \
sed "s/\./@MODULESEPARATOR@/" | \
sed "s/\(.*\)@MODULESEPARATOR@.*/\1/"
}
_list_python_files(){
local f
grep "\.py$" $FILES > $FILES-PYTHONFILES
for f in $(cat $FILES | \
grep -v "\.py$" | \
grep -v "/__pycache__/" | \
grep -v "\.pyc$\|\.pyo$\|\.so$" | \
grep "^/usr/lib64/python\|^/usr/lib/python\|^/usr/bin/\|^/usr/sbin/\|^/usr/libexec/\|^/usr/share/" \
) ; do
file $f | grep -qi "python" && echo $f
done \
>> $FILES-PYTHONFILES
}
_list_python_mods(){
local f
if [ -n "$(<$FILES-PYTHONFILES)" ]; then
for f in $(<$FILES-PYTHONFILES) ; do
cat $f | _parse_python
done | \
sort -u | \
grep -v "^$" \
> $PYMODS
fi
}
_depends_on_python(){
local f d fnd PYTHON PYTHONS
[ -x /usr/bin/python ] && PYTHONS="python"
[ -x /usr/bin/python3 ] && PYTHONS="$PYTHONS python3"
if [ -n "$(<$PYMODS)" ]; then
for PYTHON in $PYTHONS ; do
$PYTHON -c "import sys; print(sys.builtin_module_names)"
done | \
sed "s/'\|,\|(\|)//g" | \
sed "s/ /\n/g" | \
grep -v "^$" | \
sort -u \
> $PYMODS-BUILTIN
for PYTHON in $PYTHONS ; do
for d in $($PYTHON -c "import sys; print(sys.path)" | \
sed "s/'\|,\|\[\|\]//g" | \
sed "s/ /\n/g" | \
grep -v "^$"\
) ; do
[ -d $d ] || continue
echo $d
done
done | \
sort -u \
> $PYMODS-PYTHONDIRS
for f in $(<$PYMODS) ; do
grep -qw $f $PYMODS-BUILTIN && continue
grep -q "/$f\.py$\|/$f/__init__\.py$\|/$f\.so$" $FILES && continue
fnd=0
for d in $(<$PYMODS-PYTHONDIRS) ; do
if [ -f $d/$f.py ]; then
echo "$d/$f.py"
fnd=1
break
fi
if [ -f $d/$f/__init__.py ]; then
echo "$d/$f/__init__.py"
fnd=1
break
fi
if [ -f $d/$f.so ]; then
echo "$d/$f.so"
fnd=1
break
fi
done
[ $fnd -eq 0 ] && echo $f >> $PYMODS-MISSING
done | \
sort -u | \
cut -b2- \
> $PYMODS-FOUND
if [ -n "$(<$PYMODS-FOUND)" ]; then
for f in $(<$PYMODS-FOUND) ; do
(
cd $ADM/packages
grep -l $f * || \
grep -l "/$(basename $f)$" *
) | \
rev | cut -f4- -d '-' | rev | \
tr \\n \| | rev | cut -b2- | rev
done | \
sort -u \
> $DEPS-PYMODS
fi
if [ -n "$(<$PYMODS-MISSING)" ]; then
cat $PYMODS-MISSING | \
sort -u | \
awk '{print "pymod:" $1}' \
> $DEPS-PYMODS-MISSING
fi
fi
}
_find_deps(){
PKG=$1
rm -rf $TMP
mkdir -p $TMP
mkdir -p $DEPENDENCIES $MISSING
rm -f $DEPENDENCIES/${1}*
rm -f $MISSING/${1}*
touch \
$FILES $FILES-ELFFILES $FILES-PYTHONFILES \
$LIBS $LIBS-MISSING \
$PYMODS $PYMODS-FOUND $PYMODS-MISSING \
$DEPS-LIBS $DEPS-LIBS-MISSING $DEPS-PYMODS $DEPS-PYMODS-MISSING \
$DEPS $DEPS-MISSING
echo -n "Processing $PKG:"
_list_files
echo -n "."
if [ -x /usr/bin/ldd -o -x /usr/bin/readelf ]; then
(
_list_elf_files
echo -n "."
_list_libs
echo -n "."
_depends_on_libs
echo -n "."
)&
fi
if [ -x /usr/bin/python ]; then
(
_list_python_files
echo -n "."
_list_python_mods
echo -n "."
_depends_on_python
echo -n "."
)&
fi
wait
cat $DEPS-LIBS $DEPS-PYMODS > $DEPS
cat $DEPS-LIBS-MISSING $DEPS-PYMODS-MISSING > $DEPS-MISSING
[ -n "$(<$DEPS)" ] && cat $DEPS | sort -u > $DEPENDENCIES/$PKG
[ -n "$(<$DEPS-MISSING)" ] && cat $DEPS-MISSING | sort -u > $MISSING/$PKG
echo "DONE"
}
trap _die 1 2 3 9 15
if [ -n "$1" ] ; then
for p in $( ls -1 $ADM/packages/${1}* | xargs -r -n 1 basename ) ; do
_find_deps $p
done
else
PKGS=$( ls -1 $ADM/packages | \
grep -v "\-noarch\-" | \
grep -v "^glibc\-" | \
grep -v "^aaa_elflibs\-" | \
grep -v "^kernel\-" )
if [ -n "$(type -p parallel)" ] ; then
echo $PKGS | tr \ \\n | parallel --will-cite sh $0
elif [ -n "$(type -p xargs)" ] ; then
echo $PKGS | tr \ \\n | xargs -r -n 1 -P $(nproc) sh $0
else
for p in $PKGS ; do
sh $0 $p
done
fi
fi
_clean