-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpython
executable file
·311 lines (252 loc) · 7.28 KB
/
python
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
#!/usr/bin/env bash
ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" || exit; pwd)
BASE_PYTHON_VERSION=3.9.13
VIRTUALENV_NAME=avplayer-$USER-$BASE_PYTHON_VERSION
VENV_HOME=$ROOT_DIR/.venv
VENV_PYTHON=$VENV_HOME/bin/python3
REQUIREMENTS=$ROOT_DIR/requirements.txt
function print_error
{
# shellcheck disable=SC2145
echo -e "\033[31m$@\033[0m" 1>&2
}
function print_warning
{
if [[ -z $NOWARNING ]]; then
# shellcheck disable=SC2145
echo -e "\033[33m$@\033[0m" 1>&2
fi
}
function print_message
{
# shellcheck disable=SC2145
echo -e "\033[32m$@\033[0m"
}
trap 'cancel_black' INT
function cancel_black
{
print_error "An interrupt signal was detected."
exit 1
}
function find_pyenv_exe
{
local base_python_version
base_python_version=$1
local pyenv_exe
local virtualenv_version
local available_install_version
local default_pyenv_exe="$HOME/.pyenv/bin/pyenv"
pyenv_exe=$(which pyenv 2> /dev/null)
if [[ -z "$pyenv_exe" ]]; then
if [[ -x "$default_pyenv_exe" ]]; then
pyenv_exe=$default_pyenv_exe
else
print_error "Could not find executable pyenv command"
return 1
fi
fi
virtualenv_version=$("$pyenv_exe" virtualenv --version 2> /dev/null)
if [[ -z "$virtualenv_version" ]]; then
print_error "Could not find pyenv-virtualenv"
return 1
fi
available_install_version=$(
"$pyenv_exe" install -l \
| sed -n -e 's/^ *//g' -e '2,$p' \
| grep --color=none -Fx "$base_python_version"
)
if [[ -z "$available_install_version" ]]; then
print_error "Not found available python version: $base_python_version"
print_error "Please update pyenv"
return 1
fi
echo "$pyenv_exe"
}
function find_pyenv_version
{
local pyenv_exe
local version
pyenv_exe=$1
version=$2
"$pyenv_exe" versions --bare | grep --color=never -Fx "$version"
}
function install_python_on_darwin
{
local pyenv_exe
local base_python_version
pyenv_exe=$1
base_python_version=$2
print_message "Use the homebrew's OpenSSL, Zlib, SQLite"
print_message " - ref: /~https://github.com/pyenv/pyenv/wiki/Common-build-problems"
print_message "Install python '$base_python_version' from pyenv (--enable-framework) ..."
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix zlib)/include -I$(brew --prefix sqlite)/include" \
LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix sqlite)/lib" \
PYTHON_CONFIGURE_OPTS="--enable-framework" \
"$pyenv_exe" install "$base_python_version"
}
function install_python_on_auto_detect
{
local pyenv_exe
local base_python_version
pyenv_exe=$1
base_python_version=$2
print_message "Install python '$base_python_version' from pyenv (--enable-shared) ..."
PYTHON_CONFIGURE_OPTS="--enable-shared" \
"$pyenv_exe" install "$base_python_version"
}
function install_python_if_not_exist
{
local pyenv_exe
local base_python_version
pyenv_exe=$1
base_python_version=$2
if [[ -n $(find_pyenv_version "$pyenv_exe" "$base_python_version") ]]; then
return 0
fi
print_message "Not found '$base_python_version' in pyenv"
if [[ $(uname -s) == Darwin ]]; then
install_python_on_darwin "$pyenv_exe" "$base_python_version"
else
install_python_on_auto_detect "$pyenv_exe" "$base_python_version"
fi
local code=$?
if [[ $code -ne 0 ]]; then
print_error "Python installation failed: $code"
exit $code
fi
}
function install_virtualenv_if_not_exist
{
local pyenv_exe
local base_python_version
local virtualenv_name
pyenv_exe=$1
base_python_version=$2
virtualenv_name=$3
if [[ -n $(find_pyenv_version "$pyenv_exe" "$virtualenv_name") ]]; then
return 0
fi
print_message "Not found '$virtualenv_name' in pyenv"
print_message "Install '$virtualenv_name' virtualenv based on '$base_python_version'"
"$pyenv_exe" virtualenv "$base_python_version" "$virtualenv_name"
}
function enable_pyenv
{
local pyenv_exe
pyenv_exe=$1
PYENV_ROOT="$("$pyenv_exe" root)"
PATH="$PYENV_ROOT/bin:$PATH"
export PYENV_ROOT
export PATH
local platform
platform=$(uname -s)
# [IMPORTANT]
# An error occurs when using `pyenv_exe`
unset pyenv_exe
case "$platform" in
Darwin)
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
return 0
;;
Linux)
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
return 0
;;
*)
print_error "Unsupported platform: $platform"
exit 1
esac
}
function active_virtualenv
{
local virtualenv_name
virtualenv_name=$1
local current
current=$(pyenv version | awk '{print($1);}')
if [[ "$current" != "$virtualenv_name" ]]; then
pyenv shell "$virtualenv_name"
else
pyenv activate "$virtualenv_name"
fi
}
function exit_on_error
{
local code=$?
if [[ $code -ne 0 ]]; then
exit $code
fi
}
function run_base_python
{
local pyenv_exe
local base_python_version=$BASE_PYTHON_VERSION
local virtualenv_name=$VIRTUALENV_NAME
pyenv_exe=$(find_pyenv_exe "$base_python_version")
exit_on_error
install_python_if_not_exist "$pyenv_exe" "$base_python_version"
exit_on_error
install_virtualenv_if_not_exist \
"$pyenv_exe" \
"$base_python_version" \
"$virtualenv_name"
exit_on_error
enable_pyenv "$pyenv_exe"
exit_on_error
active_virtualenv "$virtualenv_name"
exit_on_error
local base_python_exe
base_python_exe=$(pyenv which python)
if [[ -x "$base_python_exe" ]]; then
"$base_python_exe" "$@"
else
print_error "The python executable could not be found"
exit 1
fi
}
## ----
## MAIN
## ----
if [[ -n $PYTHON_EXE && -n $USE_SYSTEM_PYTHON ]]; then
print_error "{PYTHON_EXE} and {USE_SYSTEM_PYTHON} cannot coexist."
exit 1
fi
if [[ -n $PYTHON_EXE ]]; then
if [[ ! -x "$PYTHON_EXE" ]]; then
print_error "The specified python executable could not be run"
exit 1
fi
elif [[ -n $USE_SYSTEM_PYTHON ]]; then
if [[ $USE_SYSTEM_PYTHON -eq 3 ]]; then
PYTHON_EXE=$(which python3 2> /dev/null)
elif [[ $USE_SYSTEM_PYTHON -eq 2 ]]; then
print_warning "python2 is deprecated. do you really use it?"
PYTHON_EXE=$(which python2 2> /dev/null)
elif [[ $USE_SYSTEM_PYTHON -eq 1 ]]; then
print_warning "Please specify the system python version as '3'"
PYTHON_EXE=$(which python 2> /dev/null)
else
print_error "{USE_SYSTEM_PYTHON} must be 1, 2, or 3"
exit 1
fi
if [[ -z $PYTHON_EXE ]]; then
print_error "Could not find system python executable"
exit 1
fi
else
PYTHON_EXE=$VENV_PYTHON
fi
if [[ ! -x "$PYTHON_EXE" ]]; then
run_base_python -m venv "$VENV_HOME"
if [[ -x "$VENV_PYTHON" ]]; then
"$VENV_PYTHON" -m pip install -U pip
"$VENV_PYTHON" -m pip install -r "$REQUIREMENTS"
fi
PYTHON_EXE=$VENV_PYTHON
fi
if [[ ! -x "$PYTHON_EXE" ]]; then
print_error "The venv's python executable could not be found."
exit 1
fi
PYTHONPATH="$ROOT_DIR:$PYTHONPATH" "$PYTHON_EXE" "$@"