-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup_env.sh
executable file
·70 lines (59 loc) · 1.93 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
#!/bin/bash
#
# Create conda environment with pinned or unpinned requirements
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
echo "Please simply call the script instead of sourcing it!"
return
fi
# Default env names
DEFAULT_ENV_NAME="probtest"
# Default options
ENV_NAME="${DEFAULT_ENV_NAME}"
PYVERSION=3.10.8
PINNED=true
EXPORT=false
CONDA=conda
HELP=false
help_msg="Usage: $(basename "${0}") [-n NAME] [-p VER] [-u] [-e] [-m] [-h]
Options:
-n NAME Env name [default: ${DEFAULT_ENV_NAME}
-p VER Python version [default: ${PYVERSION}]
-u Use unpinned requirements (minimal version restrictions)
-e Export environment files (requires -u)
-m Use mamba instead of conda
-h Print this help message and exit
"
# Eval command line options
while getopts n:p:defhimu flag; do
case ${flag} in
n) ENV_NAME=${OPTARG};;
p) PYVERSION=${OPTARG};;
e) EXPORT=true;;
h) HELP=true;;
m) CONDA=mamba;;
u) PINNED=false;;
?) echo -e "\n${help_msg}" >&2; exit 1;;
esac
done
if ${HELP}; then
echo "${help_msg}"
exit 0
fi
echo "Setting up environment for installation"
eval "$(conda shell.bash hook)" || exit # NOT ${CONDA} (doesn't work with mamba)
conda activate || exit # NOT ${CONDA} (doesn't work with mamba)
# Create new env; pass -f to overwriting any existing one
echo "Creating ${CONDA} environment"
${CONDA} create -n ${ENV_NAME} python=${PYVERSION} --yes || exit
# Install requirements in new env
if ${PINNED}; then
echo "Pinned installation"
${CONDA} env update --name ${ENV_NAME} --file requirements/environment.yml || exit
else
echo "Unpinned installation"
${CONDA} env update --name ${ENV_NAME} --file requirements/requirements.yml || exit
if ${EXPORT}; then
echo "Export pinned prod environment"
${CONDA} env export --name ${ENV_NAME} --no-builds | \grep -v '^prefix:' > requirements/environment.yml || exit
fi
fi