-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreset_venv
executable file
·180 lines (154 loc) · 4.69 KB
/
reset_venv
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
#! /bin/bash
# Hermes : Change Data Capture (CDC) tool from any source(s) to any target
# Copyright (C) 2023, 2024 INSA Strasbourg
#
# This file is part of Hermes.
#
# Hermes is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Hermes is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Hermes. If not, see <https://www.gnu.org/licenses/>.
###########################
# CONFIG
# Validated versions of python, space separated
# Order from favorite to least favorite version
validpythonversions="python3.13 python3.12 python3.11 python3.10"
# Virtualenv directory name
venvdir=".venv"
# /CONFIG
###########################
function usage {
echo "Usage: $0 [-d] [-e] [-p] [-h]" >&2
echo "" >&2
echo "options:" >&2
echo " -d, --install-devel-requirements" >&2
echo " Install devel requirements from requirements-devel.txt" >&2
echo " Won't do anything if '-p' is set" >&2
echo " -e, --dont-recreate-venv" >&2
echo " Don't delete and recreate current virtualenv." >&2
echo " WARNING: If you specify this option and no virtualenv exists," >&2
echo " python requirements will be installed in your user environment" >&2
echo " -p, --dont-install-python-requirements" >&2
echo " Don't install python requirements from found requirements.txt" >&2
echo "" >&2
echo " -h, --help" >&2
echo " Show this help message and exit" >&2
}
options=$(getopt -o deph --long help,install-devel-requirements,dont-recreate-venv,dont-install-python-requirements -- "$@")
[ $? -eq 0 ] || {
echo -e "Incorrect options provided\n" >&2
usage
exit 1
}
eval set -- "$options"
while true; do
case "$1" in
-d | --install-devel-requirements)
INSTALLDEVELREQUIREMENTS=1
;;
-e | --dont-recreate-venv)
DONTRECREATEVENV=1
;;
-p | --dont-install-python-requirements)
DONTINSTALLPYTHONREQUIREMENTS=1
;;
-h | --help)
usage
exit 0
;;
--)
shift
break
;;
*)
esac
shift
done
# Checking for a valid version of python
PYTHONCMD=UNDEF
for pyver in ${validpythonversions}
do
if command -v "${pyver}" &> /dev/null
then
PYTHONCMD="${pyver}"
break
fi
done
if [ "${PYTHONCMD}" == "UNDEF" ]
then
echo "No validated version of python found - abort" >&2
exit 1
fi
if ! command -v "direnv" &> /dev/null
then
echo "direnv was not found in \$PATH - abort" >&2
exit 1
fi
# Adding the folder to the allowed folders for direnv
direnv allow
if [ -z ${DIRENV_DIR+x} ]
then
# Direnv does not seem active, we are trying to reload its helpers
. "${HOME}/.bashrc"
fi
if [ -z ${DIRENV_DIR+x} ]
then
# Direnv is still not active
echo -e "direnv is not active, please add this to your '${HOME}/.bashrc' and reload your bash session before running this script again:\n"
cat << 'EOF'
#########
# direnv
eval "$(direnv hook bash)"
show_virtual_env() {
if [[ -n "$VIRTUAL_ENV" && -n "$DIRENV_DIR" ]]; then
echo "($(basename $VIRTUAL_ENV)) "
fi
}
export -f show_virtual_env
PS1='$(show_virtual_env)'$PS1
# /direnv
#########
EOF
echo -e "\ndirenv is not active - abort" >&2
exit 1
fi
# Checking that the current directory is indeed the root of the repository
if [ "${DIRENV_DIR:1}" != "${PWD}" ]
then
echo "[Error] This script must be executed from the folder where it is located - abort" >&2
exit 1
fi
# Creating virtualenv
if [ -z ${DONTRECREATEVENV+x} ]
then
[ -d "${venvdir}" ] && rm -Rf "${venvdir}"
if [ -e "${venvdir}" ]
then
echo "[Error] Virtualenv folder target '${venvdir}' exists, but is not a folder - abort" >&2
exit 1
fi
${PYTHONCMD} -m venv "${venvdir}"
# Force venv reload
eval "$(direnv export ${SHELL})"
fi
if [ -z ${DONTINSTALLPYTHONREQUIREMENTS+x} ]
then
reqfile=$(mktemp --suffix=-requirements.txt)
cat "requirements.txt" "plugins/"*/*"/requirements.txt" >"${reqfile}" 2>/dev/null
if [ ! -z "${INSTALLDEVELREQUIREMENTS}" ]
then
cat "requirements-devel.txt" >> "${reqfile}"
fi
# Installing Python dependencies
echo -e "\n\n*** Installing Python dependencies ***\n"
pip3 install -r "${reqfile}"
rm -f "${reqfile}"
fi