forked from HariSekhon/DevOps-Bash-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_pip_install.sh
More file actions
executable file
·161 lines (143 loc) · 4.6 KB
/
python_pip_install.sh
File metadata and controls
executable file
·161 lines (143 loc) · 4.6 KB
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
#!/usr/bin/env bash
# shellcheck disable=SC2230
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2019-02-15 13:56:24 +0000 (Fri, 15 Feb 2019)
#
# https://github.com/HariSekhon/DevOps-Bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
# Installs to --user on Mac to avoid System Integrity Protection built in to OS X El Capitan and later
#
# Also detects and sets up OpenSSL and Kerberos library paths on Mac when using HomeBrew
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC1090,SC1091
. "$srcdir/lib/ci.sh"
# shellcheck disable=SC1090,SC1091
. "$srcdir/lib/python.sh"
# want arg splitting
# shellcheck disable=SC2206
opts=(${PIP_OPTS:-})
usage(){
echo "Installs Python PyPI modules using Pip, taking in to account library paths, virtual envs etc"
echo
echo "Takes a list of python module names as arguments or .txt files containing lists of modules (one per line)"
echo
echo "usage: ${0##*} <list_of_modules>"
echo
exit 3
}
for arg; do
case "$arg" in
-*) usage
;;
esac
done
pip_modules=()
process_args(){
for arg; do
if [ -f "$arg" ]; then
echo "adding pip modules from file: $arg"
# want splitting
# shellcheck disable=SC2207
pip_modules+=($(sed 's/#.*//;/^[[:space:]]*$$/d' "$arg"))
echo
else
pip_modules+=("$arg")
fi
done
}
if [ $# -gt 0 ]; then
process_args "$@"
else
# shellcheck disable=SC2046
process_args $(cat)
fi
if [ -z "${pip_modules[*]}" ]; then
usage
fi
# want splitting
# shellcheck disable=SC2207
pip_modules=($(tr '[:space:]' ' \n' <<< "${pip_modules[@]}" | sort -u | tr '\n' ' '))
echo "Installing Python PyPI Modules"
echo
if is_CI; then
#echo "running in quiet mode for CI to minimize log noise"
opts+=(-q)
fi
sudo=""
if inside_virtualenv; then
echo "inside virtualenv, not using sudo"
sudo=""
elif [ $EUID != 0 ]; then
sudo=sudo
fi
user_opt(){
if inside_virtualenv; then
echo "inside virtualenv, ignoring --user switch which wouldn't work"
sudo=""
else
opts+=(--user)
sudo=""
fi
}
envopts=()
export LDFLAGS=""
if [ "$(uname -s)" = "Darwin" ]; then
# setting these caused compile errors failing to find stdio.h when pip installing requests-kerberos
# if type -P brew &>/dev/null; then
# # usually /usr/local
# brew_prefix="$(brew --prefix)"
#
# export OPENSSL_INCLUDE="$brew_prefix/opt/openssl/include"
# export OPENSSL_LIB="$brew_prefix/opt/openssl/lib"
#
# export LDFLAGS="${LDFLAGS:-} -L$brew_prefix/lib"
# export CFLAGS="${CFLAGS:-} -I$brew_prefix/include"
# export CPPFLAGS="${CPPFLAGS:-} -I$brew_prefix/include"
#
# # for OpenSSL
# export LDFLAGS="${LDFLAGS:-} -L$OPENSSL_LIB"
# export CFLAGS="${CFLAGS:-} -I$OPENSSL_INCLUDE"
# export CPPFLAGS="${CPPFLAGS:-} -I$OPENSSL_INCLUDE"
#
# # for Kerberos
# export LDFLAGS="${LDFLAGS:-} -L$brew_prefix/opt/krb5/lib"
# export CFLAGS="${CFLAGS:-} -I$brew_prefix/opt/krb5/include -I $brew_prefix/opt/krb5/include/krb5"
# export CPPFLAGS="${CPPFLAGS:-} -I$brew_prefix/opt/krb5/include -I $brew_prefix/opt/krb5/include/krb5"
#
# #export CPATH="${CPATH:-}:$brew_prefix/lib"
# #export LIBRARY_PATH="${LIBRARY_PATH:-}:$brew_prefix/lib"
#
# # need to send OPENSSL_INCLUDE and OPENSSL_LIB through sudo explicitly using prefix
# envopts=(OPENSSL_INCLUDE="$OPENSSL_INCLUDE" OPENSSL_LIB="$OPENSSL_LIB") # LDFLAGS="$LDFLAGS" CFLAGS="$CFLAGS" CPPFLAGS="$CPPFLAGS")
# fi
# avoids Mac's System Integrity Protection built in to OS X El Capitan and later
user_opt
elif [ -n "${PYTHON_USER_INSTALL:-}" ] ||
[ -n "${GOOGLE_CLOUD_SHELL:-}" ]; then
user_opt
fi
if [ -n "${NO_FAIL:-}" ]; then
for pip_module in "${pip_modules[@]}"; do
# pip defined in lib/python.sh
# shellcheck disable=SC2154
echo "$sudo $pip install ${opts[*]:-} $pip_module"
# want splitting of opts
# shellcheck disable=SC2068
$sudo ${envopts[@]:-} "$pip" install ${opts[@]:-} "$pip_module"
done
else
echo "$sudo $pip install ${opts[*]:-} ${pip_modules[*]}"
# want splitting of opts and modules
# shellcheck disable=SC2068
$sudo ${envopts[@]:-} "$pip" install ${opts[@]:-} "${pip_modules[@]}"
fi