This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Uninstall.sh
236 lines (200 loc) · 6.16 KB
/
Uninstall.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
#!/bin/bash
# BSD 2-Clause License
#
# Copyright (c) 2019, Allied Vision Technologies GmbH
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# global parameters parsed from command line flags
DEBUG=false
while getopts "d" flag; do
case "${flag}" in
d) DEBUG=true ;;
*) ;;
esac
done
function inside_virtual_env
{
if [ -z "$VIRTUAL_ENV" ]; then
echo "false"
else
echo "true"
fi
}
function get_python_versions
{
DETECTED_PYTHONS=()
# Check if the script was run from a virtual environment and set search path for binary accordingly
if [ "$(inside_virtual_env)" = true ]; then
if [ "$DEBUG" = true ] ; then
echo "Detected active virtual environment" >&2
fi
SEARCH_PATH="$VIRTUAL_ENV"/bin
else
if [ "$DEBUG" = true ] ; then
echo "No virtual environment detected" >&2
fi
SEARCH_PATH=$(echo "$PATH" | tr ":" " ")
fi
if [ "$DEBUG" = true ] ; then
echo "Searching for python in $SEARCH_PATH" >&2
fi
# iterate over all detected python binaries and check if they are viable installations
for P in $(whereis -b -B $SEARCH_PATH -f python | tr " " "\n" | grep "python[[:digit:]]\.[[:digit:]]\.\?[[:digit:]]\?$" | sort -V)
do
# 1) Remove results that are links (venv executables are often links so we allow those)
if [ -L "$P" ] && [ "$(inside_virtual_env)" = false ]
then
if [ "$DEBUG" = true ] ; then
echo "$P was a link" >&2
fi
continue
fi
# 2) Remove results that are directories
if [ -d "$P" ]
then
if [ "$DEBUG" = true ] ; then
echo "$P was a directory" >&2
fi
continue
fi
# 3) Remove results that offer no pip support.
$P -m pip > /dev/null 2>&1
if [ $? -ne 0 ]
then
if [ "$DEBUG" = true ] ; then
echo "$P did not have pip support" >&2
fi
continue
fi
# 4) Remove results where VimbaPython is not installed
if [ $($P -m pip list --format=columns | grep "VimbaPython" | wc -l) -ne 1 ]
then
if [ "$DEBUG" = true ] ; then
echo "$P did not have VimbaPython installed" >&2
fi
continue
fi
DETECTED_PYTHONS+=("$P")
done
echo "${DETECTED_PYTHONS[@]}"
}
echo "#################################"
echo "# VimbaPython uninstall script. #"
echo "#################################"
#########################
# Perform sanity checks #
#########################
if [ $UID -ne 0 ] && [ "$(inside_virtual_env)" = false ]
then
echo "Error: Uninstallation requires root privileges. Abort."
exit 1
fi
PWD=$(pwd)
PWD=${PWD##*/}
if [[ "$PWD" != "VimbaPython" ]]
then
echo "Error: Please execute Uninstall.sh within VimbaPython directory."
exit 1
fi
PYTHONS=$(get_python_versions)
if [ -z "$PYTHONS" ]
then
echo "Can't remove VimbaPython. No installation was found."
exit 0
fi
#############################################
# Determine python to uninstall VimbaPython #
#############################################
# List all given interpreters and create an Index
echo "VimbaPython is installed for the following interpreters:"
ITER=0
for ITEM in ${PYTHONS[@]}
do
echo " $ITER: $ITEM"
LAST=$ITER
ITER=$(expr $ITER + 1)
done
# Read and verfiy user input
while true
do
echo -n "Enter python version to uninstall VimbaPython (0 - $LAST, all: a, default: a): "
read TMP
# Set TMP to default value if nothing was entered.
if [ -z $TMP ]
then
TMP="a"
fi
# Check if Input was "a". If so skip further Input verification.
if [ "$TMP" == "a" ]
then
echo " Removing all installations of VimbaPython."
ITER=$TMP
break
else
# Check if Input was a number. If so: assign it.
if [ $TMP -eq $TMP ] 2>/dev/null
then
ITER=$TMP
else
echo " Error: Given input was not a number. Try again."
continue
fi
# Verify Input range
if [ 0 -le $ITER -a $ITER -le $LAST ]
then
break
else
echo " Error: Given input is not between 0 and $LAST. Try again."
fi
fi
done
# Search for selected python interpreter
IDX=0
PYTHON=""
for ITEM in ${PYTHONS[@]}
do
if [ "$ITER" == "a" ]
then
PYTHON=$PYTHONS
break
elif [ $IDX -eq $ITER ]
then
PYTHON=$ITEM
break
else
IDX=$(expr $IDX + 1)
fi
done
# Remove VimbaPython via pip
for P in ${PYTHON[@]}
do
echo ""
echo "Remove VimbaPython for $P"
$P -m pip uninstall --yes VimbaPython
if [ $? -eq 0 ]
then
echo "VimbaPython removal for $P was successful."
else
echo "Error: VimbaPython removal for $P failed. Please check pip output for details."
fi
done