-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpython.sh
368 lines (323 loc) · 8.57 KB
/
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
# ----------------------------------------------------------------------------
# Helper functions: thanks Homebrew! (https://github.com/Homebrew/install)
# ----------------------------------------------------------------------------
if [[ -t 1 ]]
then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"
do
printf " "
printf "%s" "${arg// /\ }"
done
}
tty_mkbold() { tty_escape "1;$1"; }
tty_underline="$(tty_escape "4;39")"
tty_blue="$(tty_mkbold 34)"
tty_red="$(tty_mkbold 31)"
tty_green="$(tty_mkbold 32)"
tty_bold="$(tty_mkbold 39)"
tty_reset="$(tty_escape 0)"
ohai() {
printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
waitforit() {
printf "${tty_bold}%s${tty_reset}" "$(shell_join "$@")"
start_spinner
}
clear_wait() {
stop_spinner
echo -ne "\033[1K"
printf "\r"
}
bold() {
printf "${tty_bold}%s${tty_reset}\n" "$(shell_join "$@")"
}
tick() {
printf "${tty_green}v${tty_reset} %s\n" "$(shell_join "$@")"
}
cross() {
printf "${tty_red}x${tty_reset} %s\n" "$(shell_join "$@")"
}
getc() {
local save_state
save_state="$(/bin/stty -g)"
/bin/stty raw -echo
IFS='' read -r -n 1 -d '' "$@"
/bin/stty "${save_state}"
}
ring_bell() {
# Use the shell's audible bell.
if [[ -t 1 ]]
then
printf "\a"
fi
}
wait_for_user() {
local c
echo -n "Press ${tty_bold}RETURN${tty_reset} to continue install or any other key to abort:"
getc c
# we test for \r and \n because some stuff does \r instead
if ! [[ "${c}" == $'\r' || "${c}" == $'\n' ]]
then
echo
echo
echo "${tty_bold}You did not press RETURN so we will stop!${tty_reset}"
exit 1
fi
echo
}
# ----------------------------------------------------------------------------
# Spinner https://github.com/tlatsas/bash-spinner
# ----------------------------------------------------------------------------
function _spinner() {
# $1 start/stop
#
# on start: $2 display message
# on stop : $2 process exit status
# $3 spinner function pid (supplied from stop_spinner)
local on_success="DONE"
local on_fail="FAIL"
local white="\033[1;37m"
local green="\033[1;32m"
local red="\033[1;31m"
local nc="\033[0m"
case $1 in
start)
# start spinner
i=1
sp='*+x'
delay=${SPINNER_DELAY:-0.15}
while :
do
printf "\b${sp:i++%${#sp}:1}"
sleep $delay
done
;;
stop)
if [[ -z ${2} ]]; then
echo ""
exit 1
fi
kill $2 > /dev/null 2>&1
# backspace spinner
echo -en "\b"
;;
*)
echo "invalid argument, try {start/stop}"
exit 1
;;
esac
}
function start_spinner {
# $1 : msg to display
_spinner "start" &
# set global spinner pid
_sp_pid=$!
disown
trap 'stop_spinner; wait' SIGINT
}
function stop_spinner {
# $1 : command exit status
_spinner "stop" $_sp_pid
unset _sp_pid
}
echo
echo "${tty_bold}Python install check${tty_reset}"
waitforit "Checking Python installation..."
python_path=`which python`
python_present=$?
if [[ $python_present -eq 0 ]]
then
python_dirname=`dirname ${python_path}`
fi
python3_path=`which python3`
python3_present=$?
if [[ $python3_present -eq 0 ]]
then
python3_dirname=`dirname ${python3_path}`
fi
pip3_path=`which pip3`
pip3_present=$?
if [[ $pip3_present -eq 0 ]]
then
pip3_dirname=`dirname ${pip3_path}`
fi
pip_path=`which pip`
pip_present=$?
if [[ $pip_present -eq 0 ]]
then
pip_dirname=`dirname $pip_path`
fi
clear_wait
echo
if [[ ($python_present -eq 0) && ($python3_present -eq 0) ]]
then
if [[ $python_dirname != $python3_dirname ]]
then
cross "The commands python and python3 are both present but not from the same distribution"
echo " This must be fixed before you continue installing"
echo " - $python3_path"
echo " - $python_path"
exit 1
fi
tick "The commands python and python3 are both present and from the same distribution"
python_command=$python3_path
python_dir=$python3_dirname
else
if [[ ($python3_present -eq 0) ]]
then
tick "The command python3 is present"
python_command=$python3_path
python_dir=$python3_dirname
else
if [[ ($python_present -eq 0) ]]
then
tick "The command python is present"
python_command=$python_path
python_dir=$python_dirname
else
cross "No python commands are available on the system"
echo "Solution: install Python"
exit 1
fi
fi
fi
if [[ -n $python_command ]]
then
python3_arch=$(file -L $python_command)
if [[ ${python3_arch//-/_} = *$(uname -m)* ]]
then
tick "Python is native for this machine"
else
cross "Python is not native for this machine"
fi
fi
if [[ ($pip_present -eq 0) && ($pip3_present -eq 0) ]]
then
if [[ $pip_dirname != $pip3_dirname ]]
then
cross "The commands pip and pip3 are both present but not from the same distribution"
echo " - $pip3_path"
echo " - $pip_path"
exit 1
fi
tick "The commands pip and pip3 are both present and from the same distribution"
pip_command=$pip3_path
pip_dir=$pip3_dirname
else
if [[ ($pip3_present -eq 0) ]]
then
tick "The command pip3 is present"
pip_command=$pip3_path
pip_dir=$pip3_dirname
else
if [[ (${pip_present} -eq 0) ]]
then
tick "The command pip is present"
pip_command=$pip_path
pip_dir=$pip_dirname
else
cross "No pip commands are available on the system"
exit 1
fi
fi
fi
echo
echo "python: $python_command"
echo " pip: $pip_command"
echo
conda=`echo "$python_command" | grep miniconda`
if [[ $? -eq 0 ]]
then
echo "You have installed miniconda Python. We would like to uninstall it now."
echo "miniconda is installed in `dirname $python_dir`"
wait_for_user
conda init --all --reverse
rm -r `dirname $python_dir`
fi
conda=`echo "$python_command" | grep anaconda`
if [[ $? -eq 0 ]]
then
echo "You have installed anaconda Python. We would like to uninstall it now."
echo "anaconda is installed in `dirname $python_dir`"
wait_for_user
conda init --all --reverse
rm -rf `dirname $python_dir`
rm /Applications/Anaconda*
fi
cd ~
if [ -d ~/anaconda* ]
then
cross "Anaconda folder is present in ~"
echo " We are now going to delete that folder"
wait_for_user
rm -rf ~/anaconda*
fi
if [ -d ~/miniconda3 ]
then
cross "miniconda folder is present in ~/miniconda3"
echo " We are now going to delete that folder"
wait_for_user
rm -rf ~/miniconda*
fi
if [ -d /opt/miniconda3 ]
then
cross "miniconda folder is present at /opt"
echo " We are now going to delete that folder"
wait_for_user
sudo rm -rf /opt/miniconda3
fi
conda_configured=`grep '>>> conda init >>>' .bash_profile .zshrc .tcshrc .xonshrc`
if [[ $? -eq 0 ]]
then
cross "Anaconda is still present in shell config files"
echo " We are now going to remove this shell integration"
wait_for_user
sed -I .anaconda_uninstalled '/^# >>> conda init[a-z]* >>>$/,/^# <<< conda init[a-z]* <<<$/d;/^# added by [A-Za-z]*conda/d' .bash_profile .zshrc .tcshrc .xonshrc
fi
if [ -h /Applications/Anaconda-Navigator.app ]
then
cross "Anaconda Navigator is installed in the Applications folder"
echo " We are now going to remove it"
wait_for_user
rm /Applications/Anaconda-Navigator.app
fi
# echo $python_command
pyorg=$(echo $python_command | grep "/Library/Frameworks/Python.framework")
if (( $? == 0 ))
then
bin_dir=`dirname $python_command`
version_dir=`dirname $bin_dir`
versions_dir=`dirname $version_dir`
cross "Official Python is present in $version_dir"
echo " We are now going to delete that folder"
wait_for_user
# delete 3.10 folder
sudo rm -rf $version_dir
# remove symlink
sudo rm $versions_dir/Current
fi
pyorg_configured=`grep '# Setting PATH for Python' .bash_profile .zshrc .tcshrc .xonshrc .zprofile`
if [[ $? -eq 0 ]]
then
cross "Official Python install is still present in shell config files"
echo " We are now going to remove this shell integration"
wait_for_user
sed -I .pyorg_uninstalled '/^# Setting PATH for Python/,/^export PATH$/d' .bash_profile .zshrc .tcshrc .xonshrc .zprofile
fi
if [ -d /Applications/Python* ]
then
cross "Official Python apps are installed in the Applications folder"
echo " We are now going to remove it"
wait_for_user
sudo rm -rf /Applications/Python*
fi
echo "Note: it is recommended to run this script multiple times until everything checks out!"
echo