-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-env.sh
executable file
·236 lines (201 loc) · 6.85 KB
/
setup-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
#!/usr/bin/env bash
set -eo pipefail
########################################################################################################################
# Settings
########################################################################################################################
# Variables - do not edit
_SOURCE="${BASH_SOURCE[0]}"
while [ -h "$_SOURCE" ]; do # resolve $_SOURCE until the file is no longer a symlink
_DIR="$(cd -P "$(dirname "$_SOURCE")" >/dev/null 2>&1 && pwd)"
_SOURCE="$(readlink "$_SOURCE")"
# if $SOURCE was a relative symlink, we need to resolve it relative to the path
# where the symlink file was located
[[ $_SOURCE != /* ]] && _SOURCE="$_DIR/$_SOURCE"
done
# shellcheck disable=SC2155
readonly SOURCE_DIR="$(cd -P "$(dirname "$_SOURCE")" >/dev/null 2>&1 && pwd)"
# shellcheck source=src/libs/logging_lib.sh
source "$SOURCE_DIR"/src/libs/logging_lib.sh
readonly PYTHON_MIN_VERSION=3.8
readonly PYTHON_MAX_VERSION=3.12
# shellcheck disable=SC2155
readonly SCRIPT_NAME=$(basename "$0")
########################################################################################################################
# Functions
#-----------------------------------------------------------------------------------------------------------------------
help() {
echo "
$SCRIPT_NAME [-l <loglevel>] [-n] [-D] [-P] [-R] [-h] [<venv-name>]
Setup dev environment. Will create a python venv and install requirements from
requirements.txt into it.
Options:
-l <level> Set loglevel. Valid values are 'trace', 'debug', 'info', 'warn',
'error' (case insensitive). Default: 'info'
-n Dry run
-P do not install pre-commit
-R register git hooks only
-h this help.
<venv-name> Name for the python virtual env. Default: '.venv'
Requirements:
* Python: $PYTHON_MIN_VERSION
"
}
#-----------------------------------------------------------------------------------------------------------------------
execute_cmd() {
local cmd="$*"
if [[ $DRY_RUN -eq 1 ]]; then
logging_info "$cmd" " [DRY_RUN]"
else
logging_info "$cmd"
$cmd
fi
}
#-----------------------------------------------------------------------------------------------------------------------
check_venv() {
if [[ "$VIRTUAL_ENV" != "" ]]; then
logging_error "It looks like your are already working inside a virtual env: $VIRTUAL_ENV"
logging_error "Please run 'deactivate' first before executing this script"
return 1
fi
}
#-----------------------------------------------------------------------------------------------------------------------
# version_compare <v1> <v2>
# VERSION_COMPARE_RESULT=0 if v1 == v2
# VERSION_COMPARE_RESULT=1 if v1 > v2
# VERSION_COMPARE_RESULT=2 if v1 < v2
version_compare() {
VERSION_COMPARE_RESULT=0
if [[ $1 == "$2" ]]; then
return
fi
local i ver1 ver2
IFS=. read -r -a ver1 <<<"$1"
IFS=. read -r -a ver2 <<<"$2"
for ((i = 0; i < ${#ver1[@]}; i++)); do
if ((10#${ver1[i]} > 10#${ver2[i]})); then
VERSION_COMPARE_RESULT=1
return
fi
if ((10#${ver1[i]} < 10#${ver2[i]})); then
VERSION_COMPARE_RESULT=2
return
fi
done
}
#-----------------------------------------------------------------------------------------------------------------------
check_python_version() {
local PYTHON_VERSION=
PYTHON_VERSION=$(python3 --version | head -n 1 | cut -d' ' -f2)
version_compare $PYTHON_MIN_VERSION "$PYTHON_VERSION"
if [[ $VERSION_COMPARE_RESULT -eq 1 ]]; then
logging_error "Minimal python version required: $PYTHON_MIN_VERSION, found $PYTHON_VERSION"
return 1
fi
version_compare $PYTHON_MAX_VERSION "$PYTHON_VERSION"
if [[ $VERSION_COMPARE_RESULT -eq 2 ]]; then
logging_error "Maximum python version allowed: $PYTHON_MAX_VERSION, found $PYTHON_VERSION"
logging_error "Python $PYTHON_MAX_VERSION required, found $PYTHON_VERSION"
return 1
fi
logging_info "Found Python $PYTHON_VERSION ... OK"
}
#-----------------------------------------------------------------------------------------------------------------------
create_venv() {
if [[ ! -d $VENV ]]; then
logging_info "Creating VENV $VENV ..."
execute_cmd python3 -m venv "$VENV"
else
logging_info "VENV $VENV found"
fi
}
#-----------------------------------------------------------------------------------------------------------------------
install_requirements() {
logging_info "Install requirements ..."
if [[ $LOGGING_LEVEL -le $LOGGING_LEVEL_DEBUG || $DRY_RUN -eq 1 ]]; then
echo "--- requirements.txt:"
cat "$SOURCE_DIR"/requirements.txt
echo
fi
execute_cmd python -m pip install --timeout 30 -r "$SOURCE_DIR"/requirements.txt
}
#-----------------------------------------------------------------------------------------------------------------------
setup_precommit() {
logging_info "Setting up pre-commit [may take a couple of minutes] ..."
execute_cmd pre-commit install --install-hooks --overwrite
}
#-----------------------------------------------------------------------------------------------------------------------
register_git_hooks() {
if [[ -d ".git-hooks" ]]; then
logging_info "Register git hooks ..."
while IFS= read -r -d '' hook; do
ln -sf "../../$hook" ".git/hooks/"
logging_info "Registered ${hook}"
done < <(find .git-hooks -maxdepth 1 -type f -perm /111 -print0)
else
logging_info "directory .git-hooks not found"
fi
}
#-----------------------------------------------------------------------------------------------------------------------
update_git_submodules() {
logging_info "Updating git submodules"
execute_cmd git submodule init
execute_cmd git submodule update
}
########################################################################################################################
# main
while getopts ":l:nPRh" opt; do
case $opt in
l) _LOG_LEVEL=$OPTARG ;;
n) DRY_RUN=1 ;;
P) PRECOMMIT_SETUP=0 ;;
R) HOOKS_ONLY=1 ;;
h)
help
exit 0
;;
\?)
echo "ERROR: Invalid option -$OPTARG"
exit 1
;;
:)
echo "ERROR: Option -$OPTARG requires an argument"
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [[ -n $1 ]]; then
VENV_NAME=$1
shift
fi
# set defaults
: "${_LOG_LEVEL:=info}"
: "${DRYRUN:=0}"
: "${VENV_NAME:=.venv}"
: "${PRECOMMIT_SETUP:=1}"
: "${HOOKS_ONLY:=0}"
VENV="$VENV_NAME"
logging_setLevel "$_LOG_LEVEL"
register_git_hooks
[[ $HOOKS_ONLY -eq 1 ]] && exit
# check requirements
check_venv
check_python_version
create_venv
logging_info "Sourceing VENV $VENV ..."
execute_cmd source "${VENV}"/bin/activate
logging_info "Install basic packages first"
execute_cmd "python -m pip install --timeout 30 pip>=21.3.1 setuptools wheel"
logging_info "Update pip to latest version"
execute_cmd "python -m pip install --upgrade pip"
install_requirements
if [[ $PRECOMMIT_SETUP -eq 1 ]]; then
setup_precommit
else
logging_info "Setup precommit disabled via flag"
fi
update_git_submodules
echo "
Creating environment competed. Type 'source $VENV/bin/activate'
to activate it.
"