-
Notifications
You must be signed in to change notification settings - Fork 0
/
basket.sh
executable file
·154 lines (120 loc) · 2.99 KB
/
basket.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
#!/bin/bash
# Debug setting
# 0 = false; 1 = true
DEBUG=0
# Booleans for readability
FALSE=0
TRUE=1
# Python version to use
PYMAJ=2
PYMIN=7
# Basket download URL
URL="https://github.com/dbaty/Basket/tarball/master"
function basket_install {
echo ""
echo "Installing..."
echo ""
$PIP install -U --user $URL
}
function basket_findpath {
echo ""
echo "Scanning..."
echo ""
# Grep site-packages location from pip
local sitepkgs=$($PIP show basket | egrep '^Location:' | awk '{print $2}')
debug "Result of pip query: ${sitepkgs}"
if [[ -z "${sitepkgs}" ]]; then
basket_quit "Could not parse '${PIP} show basket'; Basket may not be installed."
fi
PYDIR=""
IFS='/' read -ra PYDIR <<< "$sitepkgs"
for i in "${PYDIR[@]}"; do
dir="$i"
if [[ ! -z "$dir" ]]; then
if [[ "$dir" == "lib" ]]; then
debug "Python lib directory reached"
break
else
PYDIR="${PYDIR}/$dir"
debug "${PYDIR}"
fi
fi
done
}
function basket_complete {
echo ""
echo "Complete..."
echo ""
echo "You can now use Basket for the duration of this session. To make this"
echo "permenant, add this line to your '.bashrc' file in your home directory:"
echo ""
echo "PATH=\"${PYPATH}:\${PATH}\""
echo ""
}
function basket_quit {
local reason=$@
echo ""
echo "The program was unable to complete: ${reason}"
echo ""
exit 1
}
function init {
# Set executables
CURL=$(which curl)
if [[ -z "${CURL// }" ]]; then basket_quit "'curl' not found."; fi
RM=$(which rm)
if [[ -z "${RM// }" ]]; then basket_quit "'rm' not found."; fi
UNZIP=$(which unzip)
if [[ -z "${UNZIP// }" ]]; then basket_quit "'unzip' not found."; fi
# Find pip and verify Python version
local is_version=$FALSE
PIP=$(which pip)
debug "'which pip' result: ${PIP}"
if [[ ! -z "${PIP// }" ]]; then
getpyver
is_version=$?
fi
if [[ "$is_version" -eq "$FALSE" ]] || [[ -z "${PIP// }" ]]; then
PIP=$(which pip2)
debug "'which pip2' result: ${PIP}"
if [[ -z "${PIP// }" ]]; then basket_quit "'pip' not found; is Python installed?"; fi
getpyver
is_version=$?
fi
if [[ "$is_version" -eq "$FALSE" ]]; then
basket_quit "Not able to find Python version ${PYMAJ}.${PYMIN}."
fi
}
function getpyver {
local pyregex="python\s${PYMAJ}\.${PYMIN}(\.[0-9])?"
local pipver=$($PIP --version)
debug "pip version: ${pipver}"
if echo "$pipver" | egrep -q "$pyregex"; then
debug "Python ${PYMAJ}.${PYMIN} found"
return $TRUE
else
debug "Python ${PYMAJ}.${PYMIN} not found"
return $FALSE
fi
}
function debug {
if [ "$DEBUG" = true ]; then
local time=$(date +'%Y%m%d.%H%M%S')
echo "[$time]" "$@"
fi
}
#
# MAIN
#
init
basket_install
basket_findpath
PYPATH="${PYDIR}/bin"
if [[ -e "$PYPATH" ]]; then
debug "Found Python bin at ${PYPATH}"
export PATH="${PYPATH}:${PATH}"
debug "PATH exported as ${PATH}"
basket_complete
else
basket_quit "The Python bin directory does not exist at the expected location."
fi