-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-virtualenvs.sh
executable file
·142 lines (127 loc) · 3.05 KB
/
check-virtualenvs.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
#!/bin/bash
usage="\
Usage: check-virtualenvs [-v] [-f|-n]
check-virtualenvs -h"
options="
Options:
-v be more verbose
-f fix the problems by overwriting outdated Python binaries
-n print the cp commands but do not execute them
"
verbose=0
fix=0
dry_run=0
while getopts hvfn OPT; do
case "$OPT" in
v)
verbose=1
;;
f)
fix=1
;;
n)
dry_run=1
;;
h)
echo "$usage"
echo "$options"
exit 0
;;
*)
echo "$usage" 1>&2
exit 1
;;
esac
done
purple=
green=
red=
blue=
reset=
if [ -t 1 ] && [ "$(tput colors)" -ge 8 ]; then
purple='\033[35m'
green='\033[32m'
red='\033[31m'
cyan='\033[36m'
reset='\033[0m'
fi
info() {
test $verbose -gt 0 && printf "%s\\n" "$*"
}
info_looking() {
test $verbose -gt 0 && printf "${purple}%s${reset}\\n" "$*"
}
info_good() {
test $verbose -gt 0 && printf "${green}%s${reset}\\n" "$*"
}
info_action() {
printf "${cyan}%s${reset}\\n" "$*"
}
warn() {
printf "${red}%s${reset}\\n" "$*" 1>&2
}
rc=0
check() {
local binary=$1
local system=$2
test -x "$binary" || return
test -L "$binary" && return
if ! test -e "$system"; then
warn "$binary exists but $system does not"
rc=1
elif cmp -s "$binary" "$system"; then
info_good "$binary is up to date"
else
if [ $dry_run -ne 0 ]; then
echo "cp $system $binary"
elif [ $fix -ne 0 ]; then
info_action "cp $system $binary"
cp "$system" "$binary" || rc=1
else
warn "$binary differs from $system"
rc=1
fi
fi
}
checkifone() {
binary=$1
system=$2
versions=$3
test -x "$binary" || return
test -L "$binary" && return
if [ "$versions" -ne 1 ]; then
warn "${binary%/bin/python*}/lib has multiple python versions, cannot check $binary"
else
check "$binary" "$system"
fi
}
ignore="\
^/usr/(local/)?lib/python|\
^/usr/lib/debug/|\
^/var/lib/docker/|\
^/var/lib/flatpak/|\
\\.local/share/flatpak/|\
\\.steam/|\
^/var/lib/lxd/|\
^/snap/\
"
info_looking "looking for virtualenvs"
locate --regexp "/lib/python[2-3][.][0-9][0-9]*$" | grep -vE "$ignore" | \
while read -r libdir; do
python=${libdir##*/} # /path/to/venv/lib/python3.8 -> python3.8
major=${python%.*} # python3.4 -> python3
libdir=${libdir%/$python} # /path/to/venv/lib
envdir=${libdir%/lib} # /path/to/venv
if [ -e "$libdir/pkgconfig" ]; then
info "skipping $envdir, it looks like a full Python installation"
continue
fi
# shellcheck disable=SC2010
versions=$(ls "$libdir"|grep -c ^python)
# shellcheck disable=SC2010
minor_versions=$(ls "$libdir"|grep -c "^$major")
checkifone "$envdir/bin/python" "/usr/bin/$python" "$versions"
checkifone "$envdir/bin/$major" "/usr/bin/$python" "$minor_versions"
check "$envdir/bin/$python" "/usr/bin/$python"
done
exit $rc