forked from saezlab/pypath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac-install-brew.sh
executable file
·439 lines (380 loc) · 12.5 KB
/
mac-install-brew.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/bin/bash
#
# This file is part of the `pypath` python module
#
# Copyright
# 2014-2018
# EMBL, EMBL-EBI, Uniklinik RWTH Aachen, Heidelberg University
#
# File author(s): Dénes Türei ([email protected])
# Nicolas Palacio
#
# Distributed under the GPLv3 License.
# See accompanying file LICENSE.txt or copy at
# http://www.gnu.org/licenses/gpl-3.0.html
#
# Website: http://pypath.omnipathdb.org/
#
#
# Tested on OS X 10.11.3
#
# installing HomeBrew first:
USAGE="Usage:\n\t$0\n\t\t[-h (show help and exit)]\n\t\t[-p <2|3> (Python version)]\n\t\t"\
"[-c (do not install cairo)]\n\t\t[-g (do not install graphviz)]\n\t\t"\
"[-t (run tests only)]\n\t\t[-u (uninstall everything)]\n\t\t[-f (do not ask confirmation at uninstall)]\n\t\t"\
"[-m (uninstall Python modules)]\n\t\t[-b (uninstall HomeBrew and formulas)]\n\t\t[-e (remove environment changes)]\n"
PYMAINVER="3"
INSTALL=true
TESTS=true
ICAIRO=true
IGRAPHVIZ=true
UNINSTM=false
UNINSTB=false
UNINSTE=false
UCONFIRM=true
while getopts ":htumbefp:" opt;
do
case $opt in
h)
echo -en "$USAGE";
exit 0
;;
p)
PYMAINVER="${OPTARG}"
;;
t)
INSTALL=false
;;
u)
INSTALL=false
UNINSTM=true
UNINSTF=true
UNINSTB=true
UNINSTE=true
TESTS=false
;;
m)
INSTALL=false
UNINSTM=true
TESTS=false
;;
b)
INSTALL=false
UNINSTF=true
UNINSTB=true
TESTS=false
;;
e)
INSTALL=false
UNINSTE=true
TESTS=false
;;
f)
UCONFIRM=false
;;
c)
ICAIRO=false
;;
g)
IGRAPHVIZ=false
;;
?)
echo -en "$USAGE";
exit 2
;;
esac
done
if [[ $PYMAINVER == "3" ]];
then
PYVER="3.7";
PYCAIRONAME="py3cairo";
PYTHONNAME="python3";
PYFABRIC="fabric3";
else
PYVER="2.7";
PYMAINVER="2";
PYCAIRONAME="py2cairo";
PYTHONNAME="python";
PYFABRIC="fabric";
fi
USER=`whoami`
LOCAL="/usr/local/Cellar"
LOCALBIN="$LOCAL/bin"
LOCALPIP="$LOCALBIN/pip$PYVER"
PYPATHURL="http://pypath.omnipathdb.org/releases/latest/pypath-latest.tar.gz"
if [ ! -d $LOCAL ];
then mkdir $LOCAL;
fi
export PATH="$LOCALBIN:$PATH"
function update_python_path {
PYVER=$(
[[ `$PYTHONNAME --version` =~ (3\.[0-9])\.[0-9] ]] &&
echo ${BASH_REMATCH[1]}
)
LOCALBIN=`brew info python3 | tr -d '\n\r' | pcregrep -o1 -i 'has been installed as[ ]+(.*?)python[23]'`
LOCALPIP="$LOCALBIN/pip$PYVER"
}
function rc_contents {
PYTHONRCCONTENT=$(printf %s ''\
'import readline# pypath added\n'\
'import rlcompleter# pypath added\n'\
'if "libedit" in readline.__doc__:# pypath added\n'\
' readline.parse_and_bind("bind ^I rl_complete")# pypath added\n'\
'else:# pypath added\n'\
' readline.parse_and_bind("tab: complete")# pypath added\n')
BASHPROFPYRC='export PYTHONSTARTUP=~/.pythonrc # pypath added\n'
BASHPROFLOCP='export PATH="'$LOCALBIN':$PATH" # pypath added\n'
}
function write_rc {
rc_contents
if [[ ! -f ~/.pythonrc || "$(grep 'tab:[[:space:]]\?complete' ~/.pythonrc)" == "" ]];
then
# autocompletion is highly useful
# we add it to the .pyhthonrc
# feel free to remove later if u don't like
echo -en "$PYTHONRCCONTENT" >> ~/.pythonrc
fi
if [[ ! -f ~/.bash_profile || "$(grep '.pythonrc' ~/.bash_profile)" == "" ]];
then
# adding this to .bash_profile to use .pythonrc
echo -en "$BASHPROFPYRC" >> ~/.bash_profile
fi
if [[ ! -f ~/.bash_profile || "$(grep $LOCALBIN ~/.bash_profile)" == "" ]];
then
# adding this to .bash_profile to have local in path
echo -en "$BASHPROFLOCP" >> ~/.bash_profile
fi
}
# beginning part `INSTALL`
if [[ "$INSTALL" = "true" ]];
then
echo -en "\n\n===[ Attempting to install pypath and all its dependencies with the help of HomeBrew. ]===\n\n"
echo -en "\t Note: this method works on most of the Mac computers.\n"\
"\t Watch out for errors, and the test results post installation.\n"\
"\t This will last at least 10 mins. Now relax, and hope the best.\n\n"
# downloading and extracting homebrew:
type brew >/dev/null 2>&1
if [[ $? != 0 ]];
then
cd $LOCAL
curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1
cd ~
fi
# update repositories
brew update
# installing curl and openssl
brew install curl
brew install openssl
brew install --force libxml2
brew install --force libxslt
brew link libxml2 --force
brew link libxslt --force
# obtaining a recent python distribution from brew:
brew install $PYTHONNAME
# optionally install (py)cairo:
if [[ "$ICAIRO" == "true" ]];
then brew install $PYCAIRONAME;
fi
# optionally install graphviz:
if [[ "$IGRAPHVIZ" == "true" ]];
then brew install igraph graphviz;
fi
# maybe we just installed new python, update the python/pip version
update_python_path
write_rc
# installing another python modules by pip
$LOCALPIP install --upgrade pip
if [[ `$LOCALPIP show pycurl` ]];
then $LOCALPIP uninstall -y pycurl
fi
if [[ `$LOCALPIP show lxml` ]];
then $LOCALPIP uninstall -y lxml
fi
export LD_LIBRARY_PATH=/usr/local/opt/curl/lib
export CPATH=/usr/local/opt/openssl/include
export LIBRARY_PATH=/usr/local/opt/openssl/lib
PYCURL_SSL_LIBRARY=openssl LDFLAGS="-L/usr/local/opt/openssl/lib" CPPFLAGS="-I/usr/local/opt/openssl/include" $LOCALPIP --no-cache-dir install pycurl
STATIC_DEPS=true $LOCALPIP --no-cache-dir install lxml
$LOCALPIP install python-igraph
$LOCALPIP install pysftp
$LOCALPIP install $PYFABRIC
$LOCALPIP install future
$LOCALPIP install pandas
$LOCALPIP install scipy
$LOCALPIP install suds-jurko
$LOCALPIP install bioservices
$LOCALPIP install pymysql
$LOCALPIP install tqdm
# optionally install pygraphviz
if [[ "$IGRAPHVIZ" == "true" ]];
then $LOCALPIP install pygraphviz;
fi
$LOCALPIP install $PYPATHURL
# adding local paths and python paths permantently
cat << EOF >> .bash_profile
export PATH="$LOCALBIN:\$PATH"
EOF
# with Py3 this will be necessary:
#if [[ $PYMAINVER == "3" ]];
# then $LOCALPIP install git+https://github.com/brentp/fishers_exact_test.git;
#fi
# end of part `INSTALL`
fi
# beginning part `TESTS`
if [[ "$TESTS" == "true" ]];
then
# check and report:
echo -en "\n\n\t===[ Testing installation ]===\n\n"
type brew >/dev/null 2>&1
if [[ $? -eq 1 ]];
then
echo -en "\t [ !! ] Failed to install HomeBrew. This prevents the installation of essential dependencies.\n"
else
echo -en "\t [ OK ] HomeBrew is available.\n"
fi
declare -a formulas=(curl openssl $PYTHONNAME $PYCAIRONAME igraph graphviz)
for frm in "${formulas[@]}"
do
hasfrm="$(brew ls --versions $(echo $frm | awk 'BEGIN{FS="/"}{print $NF}'))"
if [[ $hasfrm"o" == "o" ]];
then
echo -en "\t [ !! ] Brew formula \`$frm\` is missing.\n"
else
echo -en "\t [ OK ] Brew formula \`$frm\` is available.\n"
fi
done
declare -a modules=(cairo igraph future numpy scipy pandas suds bioservices pymysql pygraphviz pycurl pysftp fabric tqdm pypath)
for mod in "${modules[@]}"
do
$PYTHONNAME -c "import $mod" >/dev/null 2>&1
if [[ $? == 0 ]];
then
echo -en "\t [ OK ] Module \`$mod\` for Python $PYVER is available.\n"
else
echo -en "\t [ !! ] Module \`$mod\` for Python $PYVER failed to install.\n"
fi
done
$PYTHONNAME -c "import pypath; pa = pypath.PyPath()" >/dev/null 2>&1
if [[ $? == 0 ]];
then
echo -en "\t [ OK ] Congratulations! You have pypath installed! :)\n"\
"\t The authors wish you interesting findings in your analysis.\n"\
"\t If you experience any issue, don't hesitate to contact [email protected].\n\n"
else
echo -en "\t [ !! ] You have no pypath installed or some issue avoids the module to load.\n"\
"\t Please check the list above for failed items. Contact [email protected] if you need help.\n\n"
fi
# end of part `TESTS`
fi
# beginning part `UNINSTALL`
declare -a modules=(pypath $PYFABRIC pysftp fisher pygraphviz pymysql bioservices suds pandas scipy numpy future igraph cairo tqdm)
mcols="$(for i in $(seq 0 $((${#modules[@]} - 1))); do echo "($i) ${modules[$i]}"; done; echo "(a) all"; echo "() none")"
mcols="$(echo $mcols | column)"
mmsg="$(echo -en "\n===>[ Remove the following Python $PYVER modules? ]<===\n\n$mcols\n[Answer e.g. \"1,2,3\" or \"a\"] ")"
declare -a formulas=(graphviz igraph $PYCAIRONAME $PYTHONNAME)
fcols="$(for i in $(seq 0 $((${#formulas[@]} - 1))); do echo "($i) ${formulas[$i]}"; done; echo "(a) all"; echo "() none")"
fcols="$(echo $fcols | column)"
fmsg="$(echo -en "\n===>[ Remove the following HomeBrew formulas? ]<===\n\n$fcols\n[Answer e.g. \"1,2,3\" or \"a\"] ")"
bmsg="$(echo -en "\n===>[ Do you want to remove HomeBrew? ]<===\n\n [ y/N ] ")"
# update this now to be able to remove later RC file rc_contents
update_python_path
rc_contents
# uninstalling python modules:
if [[ "$UNINSTM" == "true" ]];
then
# make sure python/pip version is correct
if [[ "$UCONFIRM" == "true" ]];
then
ifs_default=$IFS
IFS=","
read -p "$mmsg" -r -a msel
IFS=$ifs_default
else
declare -a msel=(a)
fi
if [[ ${#msel[@]} -gt 0 ]];
then
echo -en "\n\n\t===[ Uninstalling Python modules ]===\n\n"
if [[ ${msel[0]} == "a" ]];
then
msel=$(seq 0 $((${#modules[@] - 1})))
fi
for i in ${msel[@]};
do
if [[ $i -lt ${#modules[@]} ]];
then
$LOCALPIP uninstall -y ${modules[$i]}
fi
done
fi
fi
# uninstalling homebrew formulas:
if [[ "$UNINSTF" == "true" ]];
then
if [[ "$UCONFIRM" == "true" ]];
then
ifs_default=$IFS
IFS=","
read -p "$fmsg" -r -a fsel
IFS=$ifs_default
else
declare -a fsel=(a)
fi
if [[ ${#fsel[@]} -gt 0 ]];
then
if [[ ${fsel[0]} == "a" ]];
then
fsel=$(seq 0 $((${#formulas[@] - 1})))
fi
echo -en "\n\n===[ Uninstalling HomeBrew formulas ]===\n\n"
for i in ${fsel[@]};
do
if [[ $i -lt ${#formulas[@]} ]];
then
brew remove --force $(echo ${formulas[$i]} | awk 'BEGIN{FS="/"}{print $NF}')
brew rm $(join <(brew leaves) <(brew deps ${formulas[$i]}))
fi
done
fi
if [[ "$UCONFIRM" == "true" ]];
then
read -p "$bmsg" -n 1 UNINSTB
if [[ "$UNINSTB" =~ ^(y|Y)$ ]];
then
UNINSTB=true
else
UNINSTB=false
fi
fi
if [[ "$UNINSTB" == "true" ]];
then
echo -en "\n===[ Uninstalling HomeBrew ]===\n\n"
curl -L 'https://raw.githubusercontent.com/Homebrew/install/master/uninstall' > brew-uninstall.rb
chmod +x brew-uninstall.rb
./brew-uninstall.rb -qf
fi
fi
if [[ "$UNINSTE" == "true" ]];
then
rc_contents
echo -en "\n\n===[ Restoring environment (~/.pythonrc, ~/.bash_profile) ]===\n\n"
if [ -f .pythonrc ];
then
echo "$(tr '\n' '\a' < .pythonrc)" | sed 's/'"$(echo -en "$PYTHONRCCONTENT" | tr '\n' '\a')"'//g' | tr '\a' '\n' > .pythonrc_tmp
mv .pythonrc_tmp .pythonrc
fi
if [ -f .bash_profile ];
then
BASHPROFPYRCE="$(echo $BASHPROFPYRC | sed -e 's/[]\/$*.^|[]/\\&/g')"
echo "$(tr '\n' '\a' < .bash_profile)" | sed 's/'"$(echo -en "$BASHPROFPYRCE" | tr '\n' '\a')"'//g' | tr '\a' '\n' > .bash_profile_tmp
mv .bash_profile_tmp .bash_profile
BASHPROFLOCPE="$(echo $BASHPROFLOCP | sed -e 's/[]\/$*.^|[]/\\&/g')"
echo "$(tr '\n' '\a' < .bash_profile)" | sed 's/'"$(echo -en "$BASHPROFLOCPE" | tr '\n' '\a')"'//g' | tr '\a' '\n' > .bash_profile_tmp
mv .bash_profile_tmp .bash_profile
fi
fi
# end of part `UNINSTALL`
unset LOCALBIN
unset LOCALPIP
unset PYPATHURL
echo -en "\n===[ Tasks complete. Please report any issue to [email protected]. Bye. ]===\n\n"