-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingen_conda_helper.sh
executable file
·237 lines (161 loc) · 5.98 KB
/
ingen_conda_helper.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
#!/bin/bash
# this file is meant to be sourced and used in a script
# which wants to setup/test a new conda env at a specific location!
if [[ "$0" = "$BASH_SOURCE" ]]; then
echo "This is meant to be sourced from another script. Do not execute."
exit 1
fi
# bash variables
TRUE_VAL="TRUE"
FALSE_VAL="FALSE"
MINICONDA_INSTALLER="Miniconda3-py37_4.10.3-Linux-x86_64.sh"
MINICONDA_BASE_URL="https://repo.anaconda.com/miniconda"
# setup_local_conda_install_paths </path/to/conda/install/dir>
function setup_local_conda_install_paths() {
if [ ! $# == 1 ] ; then
echo
echo "usage: setup_local_conda_install_paths </path/to/conda/install/dir>"
return 1
fi
# pass in the directory where the conda installation is available
CONDA_INSTALL_DIR=$1
# source conda setup script so 'conda' commands are available
# TODO we can check if this has already been done in current shell
# using output of `which conda` and checking the path to be $CONDA_INSTALL_DIR/condabin/conda
source $CONDA_INSTALL_DIR/etc/profile.d/conda.sh
# indicate ok
return 0
}
# setup_local_conda_install </path/to/conda/install/dir>
function setup_local_conda_install() {
if [ ! $# == 1 ] ; then
echo
echo "usage: setup_local_conda_install </path/to/conda/install/dir>"
return 1
fi
# pass in the directory where the conda should be installed
CONDA_INSTALL_DIR=$1
# get the conda installer
if [ ! -f "$MINICONDA_INSTALLER" ] ; then
echo
echo "downloading miniconda installer ..."
wget -q "$MINICONDA_BASE_URL/$MINICONDA_INSTALLER" -O "$MINICONDA_INSTALLER"
echo "done"
echo
else
echo
echo "using existing installer: $MINICONDA_INSTALLER"
echo
fi
# install the conda environment into the local_dir/conda
# -b: batch mode/no PATH modifications, -p: use prefix for conda install
# https://docs.anaconda.com/anaconda/install/silent-mode/
chmod +x "$MINICONDA_INSTALLER"
bash "$MINICONDA_INSTALLER" -q -b -p "$CONDA_INSTALL_DIR"
# setup the conda install paths
setup_local_conda_install_paths "$CONDA_INSTALL_DIR"
# setup custom configuration to prevent pip installing stuff into global python installation
# https://stackoverflow.com/questions/51525072/global-pip-referenced-within-a-conda-environment
echo 'include-system-site-packages=false' >> $CONDA_INSTALL_DIR/pyvenv.cfg
# update conda itself
conda update -y --override-channels -c defaults -q conda
# disable auto activate of base environment
# (no need as local only conda install)
#conda config --set auto_activate_base false
# remove the conda installer binary - leave it in place so we don't have to redownload every time.
#rm -f "$MINICONDA_INSTALLER"
# indicate ok
return 0
}
# create_local_conda_env </path/to/conda/install/dir> </path/to/environment/> </path/to/environment/yaml/>
function create_local_conda_env() {
if [ ! $# == 3 ] ; then
echo
echo "usage: create_local_conda_env </path/to/conda/install/dir> </path/to/environment/> </path/to/environment/yaml/>"
return 1
fi
CONDA_INSTALL_DIR=$1
CONDA_ENVIRONMENT_PREFIX=$2
CONDA_ENVIRONMENT_YAML_FILE=$3
setup_local_conda_install_paths $CONDA_INSTALL_DIR
conda env create -f "$CONDA_ENVIRONMENT_YAML_FILE" -p "$CONDA_ENVIRONMENT_PREFIX"
STATUS=$?
return $STATUS
}
# delete_local_conda_env </path/to/conda/install/dir> </path/to/environment/>
function delete_local_conda_env() {
if [ ! $# == 2 ] ; then
echo
echo "usage: delete_local_conda_env </path/to/conda/install/dir> </path/to/environment/>"
return 1
fi
CONDA_INSTALL_DIR=$1
CONDA_ENVIRONMENT_PREFIX=$2
setup_local_conda_install_paths $CONDA_INSTALL_DIR
conda env remove -p "$CONDA_ENVIRONMENT_PREFIX"
STATUS=$?
return $STATUS
}
# test_local_conda_env </path/to/conda/install/dir> </path/to/environment/>
function test_local_conda_env() {
if [ ! $# == 2 ] ; then
echo
echo "usage: test_local_conda_env </path/to/conda/install/dir> </path/to/environment/>"
return 1
fi
# pass in the directory where the conda should be installed
CONDA_INSTALL_DIR=$1
CONDA_ENVIRONMENT_PREFIX=$2
CONDA_SETUP_INSTALL_OK="$FALSE_VAL"
# activate the conda environment
conda activate "$CONDA_ENVIRONMENT_PREFIX"
# verify the conda environment
CONDA_PATH=$(which conda)
CONDA_VERSION=$(conda --version)
PYTHON_PATH=$(which python)
PYTHON_VERSION=$(python --version)
PIP_PATH=$(which pip)
PIP_VERSION=$(pip --version)
EXPECTED_CONDA_PATH="${CONDA_INSTALL_DIR}/condabin/conda"
EXPECTED_PYTHON_PATH="${CONDA_ENVIRONMENT_PREFIX}/bin/python"
EXPECTED_PIP_PATH="${CONDA_ENVIRONMENT_PREFIX}/bin/pip"
if [ "$CONDA_PATH" == "$EXPECTED_CONDA_PATH" ] &&
[ "$PYTHON_PATH" == "$EXPECTED_PYTHON_PATH" ] &&
[ "$PIP_PATH" == "$EXPECTED_PIP_PATH" ] ; then
echo
echo "conda setup looks ok:"
echo
echo "$CONDA_VERSION"
echo "$PYTHON_VERSION"
echo "$PIP_VERSION"
echo
CONDA_SETUP_INSTALL_OK="$TRUE_VAL"
echo
else
echo
echo "conda setup is incorrect, check the script flow!"
echo
echo "conda"
echo "got: " "$CONDA_PATH"
echo "expected:" "$EXPECTED_CONDA_PATH"
echo
echo "python"
echo "got: " "$PYTHON_PATH"
echo "expected:" "$EXPECTED_PYTHON_PATH"
echo
echo "pip"
echo "got: " "$PIP_PATH"
echo "expected:" "$EXPECTED_PIP_PATH"
echo
fi
# deactivate the conda environment
conda deactivate
if [ ! "$CONDA_SETUP_INSTALL_OK" == "$TRUE_VAL" ] ; then
# indicate error
return 1
fi
# indicate ok.
return 0
}
# indicate everything is ok when sourced!
return 0