-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-env.sh
executable file
·383 lines (324 loc) · 9.28 KB
/
update-env.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
#!/bin/bash
set -e
set -u
VERBOSE="n"
DEBUG="n"
QUIET='n'
VERSION="2.4"
# console colors:
RED=""
YELLOW=""
GREEN=""
# BLUE=""
CYAN=""
NORMAL=""
BASENAME=$( basename "${0}" )
BASE_DIR=$( dirname "$0" )
cd "${BASE_DIR}"
BASE_DIR=$( readlink -f . )
declare -a VALID_PY_VERSIONS=("3.11" "3.10" "3.9" "3.8" "3.7" "3.6")
PIP_OPTIONS=
export VIRTUAL_ENV_DISABLE_PROMPT=y
#-------------------------------------------------------------------
detect_color() {
local safe_term="${TERM//[^[:alnum:]]/?}"
local match_lhs=""
local use_color="false"
local term=
if [[ -f ~/.dir_colors ]] ; then
match_lhs="${match_lhs}$( grep '^TERM ' ~/.dir_colors | sed -e 's/^TERM *//' -e 's/ .*//')"
fi
if [[ -f /etc/DIR_COLORS ]] ; then
match_lhs="${match_lhs}$( grep '^TERM ' /etc/DIR_COLORS | sed -e 's/^TERM *//' -e 's/ .*//')"
fi
if [[ -z ${match_lhs} ]] ; then
type -P dircolors >/dev/null && \
match_lhs=$(dircolors --print-database | grep '^TERM ' | sed -e 's/^TERM *//' -e 's/ .*//')
fi
for term in ${match_lhs} ; do
# shellcheck disable=SC2053
if [[ "${safe_term}" == ${term} || "${TERM}" == ${term} ]] ; then
use_color="true"
break
fi
done
# console colors:
if [ "${use_color}" = "true" ] ; then
RED="\033[38;5;196m"
YELLOW="\033[38;5;226m"
GREEN="\033[38;5;46m"
# BLUE="\033[38;5;27m"
CYAN="\033[38;5;36m"
NORMAL="\033[39m"
else
RED=""
YELLOW=""
GREEN=""
# BLUE=""
CYAN=""
NORMAL=""
fi
local my_tty
my_tty=$( tty )
if [[ "${my_tty}" =~ 'not a tty' ]] ; then
my_tty='-'
fi
}
detect_color
#------------------------------------------------------------------------------
my_date() {
date +'%F %T.%N %:::z'
}
#------------------------------------------------------------------------------
debug() {
if [[ "${VERBOSE}" != "y" ]] ; then
return 0
fi
echo -e " * [$(my_date)] [${BASENAME}:${CYAN}DEBUG${NORMAL}]: $*" >&2
}
#------------------------------------------------------------------------------
info() {
if [[ "${QUIET}" == "y" ]] ; then
return 0
fi
if [[ "${VERBOSE}" == "y" ]] ; then
echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $*"
else
echo -e " ${GREEN}*${NORMAL} $*"
fi
}
#------------------------------------------------------------------------------
warn() {
if [[ "${VERBOSE}" == "y" ]] ; then
echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $*" >&2
else
echo -e " ${YELLOW}*${NORMAL} [${BASENAME}:${YELLOW}WARN${NORMAL}] : $*" >&2
fi
}
#------------------------------------------------------------------------------
error() {
if [[ "${VERBOSE}" == "y" ]] ; then
echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $*" >&2
else
echo -e " ${RED}*${NORMAL} [${BASENAME}:${RED}ERROR${NORMAL}]: $*" >&2
fi
}
#------------------------------------------------------------------------------
description() {
cat <<-EOF
Updates the Python virtual environment.
EOF
}
#------------------------------------------------------------------------------
line() {
if [[ "${QUIET}" == "y" ]] ; then
return 0
fi
echo "---------------------------------------------------"
}
#------------------------------------------------------------------------------
empty_line() {
if [[ "${QUIET}" == "y" ]] ; then
return 0
fi
echo
}
#------------------------------------------------------------------------------
usage() {
cat <<-EOF
Usage: ${BASENAME} [-d|--debug] [[-v|--verbose] | [-q|--quiet]] [--nocolor]
${BASENAME} [-h|--help]
${BASENAME} [-V|--version]
Options:
-d|--debug Debug output (bash -x).
-v|--verbose Set verbosity on.
-q|--quiet Quiet execution. Mutually exclusive to --verbose.
--nocolor Don't use colors on display.
-h|--help Show this output and exit.
-V|--version Prints out version number of the script and exit.
EOF
}
#------------------------------------------------------------------------------
get_options() {
local tmp=
local short_options="dvqhV"
local long_options="debug,verbose,quiet,help,version"
set +e
tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASENAME}" -- "$@" )
ret="$?"
if [[ "${ret}" != 0 ]] ; then
echo "" >&2
usage >&2
exit 1
fi
set -e
# Note the quotes around `$TEMP': they are essential!
eval set -- "${tmp}"
while true ; do
case "$1" in
-d|--debug)
DEBUG="y"
shift
;;
-v|--verbose)
VERBOSE="y"
shift
;;
-q|--quiet)
QUIET="y"
RED=""
YELLOW=""
GREEN=""
# BLUE=""
CYAN=""
NORMAL=""
shift
;;
--nocolor)
RED=""
YELLOW=""
GREEN=""
# BLUE=""
CYAN=""
NORMAL=""
shift
;;
-h|--help)
description
echo
usage
exit 0
;;
-V|--version)
echo "${BASENAME} version: ${VERSION}"
exit 0
;;
--) shift
break
;;
*) echo "Internal error!"
exit 1
;;
esac
done
if [[ "${DEBUG}" = "y" ]] ; then
set -x
fi
if [[ "${VERBOSE}" == "y" && "${QUIET}" == "y" ]] ; then
error "Options '${RED}--verbose${NORMAL}' and '${RED}--quiet${NORMAL}' are mutually exclusive."
echo >&2
usage >&2
exit 1
fi
if type -t msgfmt >/dev/null ; then
:
else
echo "Command '${RED}msgfmt${NORMAL}' not found, please install package '${YELLOW}gettext${NORMAL}' or appropriate." >&2
exit 6
fi
if [[ "${VERBOSE}" == "y" ]] ; then
PIP_OPTIONS="--verbose"
elif [[ "${QUIET}" == "y" ]] ; then
PIP_OPTIONS="--quiet"
fi
}
#------------------------------------------------------------------------------
init_venv() {
local py_version=
local python=
local found="n"
empty_line
line
info "Preparing virtual environment …"
empty_line
if [[ ! -f venv/bin/activate ]] ; then
found="n"
for py_version in "${VALID_PY_VERSIONS[@]}" ; do
python="python${py_version}"
debug "Testing Python binary '${CYAN}${python}${NORMAL}' …"
if type -t "${python}" >/dev/null ; then
found="y"
empty_line
info "Found '${GREEN}${python}${NORMAL}'."
empty_line
"${python}" -m venv venv
break
fi
done
if [[ "${found}" == "n" ]] ; then
empty_line >&2
error "Did not found a usable Python version." >&2
error "Usable Python versions are: ${YELLOW}${VALID_PY_VERSIONS[*]}${NORMAL}." >&2
empty_line >&2
exit 5
fi
fi
# shellcheck disable=SC1091
. venv/bin/activate || exit 5
}
#------------------------------------------------------------------------------
upgrade_pip() {
line
info "Upgrading PIP …"
empty_line
pip install ${PIP_OPTIONS} --upgrade --upgrade-strategy eager pip
empty_line
}
#------------------------------------------------------------------------------
upgrade_setuptools() {
line
info "Upgrading setuptools + wheel + six …"
empty_line
pip install ${PIP_OPTIONS} --upgrade --upgrade-strategy eager setuptools wheel six
empty_line
}
#------------------------------------------------------------------------------
upgrade_modules() {
line
info "Installing and/or upgrading necessary modules …"
empty_line
pip install ${PIP_OPTIONS} --upgrade --upgrade-strategy eager --requirement requirements.txt
empty_line
}
#------------------------------------------------------------------------------
list_modules() {
if [[ "${QUIET}" == "y" ]] ; then
return 0
fi
line
info "Installed modules:"
empty_line
pip list --format columns
empty_line
}
#------------------------------------------------------------------------------
compile_i18n() {
if [[ -x compile-xlate-msgs.sh ]]; then
line
info "Updating i18n files in ${BASE_DIR} …"
empty_line
./compile-xlate-msgs.sh
empty_line
fi
}
################################################################################
##
## Main
##
################################################################################
#------------------------------------------------------------------------------
main() {
get_options "$@"
init_venv
upgrade_pip
upgrade_setuptools
upgrade_modules
list_modules
compile_i18n
line
info "Fertig."
empty_line
}
main "$@"
exit 0
# vim: ts=4 list