-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
344 lines (301 loc) · 11.7 KB
/
install.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
#!/bin/sh
set -e
# TODO: Convert to a python script. This would
# a) make it much to maintain
# b) make it windows-compatible
# These commands create a new conda environment from scratch, grabbing
# the most current version for each package.
# To *reproduce* an existing environment, export
# conda list --export > env-freeze.yml
# and follow the instructions at the top of the file
## Hard-coded values. Use these to override values inferred from the directory structure
# (Leaving blank lets the script infer values from the directory structure
ENVNAME=
PROJECTROOT=
ENVDIR=
ENVYML=
# https://stackoverflow.com/a/21188136
get_abs_filename() {
# $1 : relative filename
echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
}
#https://stackoverflow.com/a/2924755
bold=$(tput bold)
normal=$(tput sgr0)
# Change to the script's directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "Switching to directory $DIR"
cd "$DIR"
INSTALL_LOCATION="local"
# # To set INSTALL_LOCATION based on the computer hostname, uncomment below
# # and adjust the `if` conditions.
# # Determine if we are on a cluster
# # These are hard-coded tests based on the hostname
# # They determine two things: whether to do a CPU or GPU install, and whether
# # to install ipykernel so the environment can be executed from Jupyter
# if [[ `hostname` == "login" ]]; then
# echo "You shouldn't be installing from the cluster's login node."
# echo "Aborting."
# exit
# elif [[ `hostname` == "cluster-name" ]]; then
# INSTALL_LOCATION="cluster"
# else
# # Anything else we presume is a local install
# INSTALL_LOCATION="local"
# fi
# Make `conda activate` work in shell script (https://github.com/conda/conda/issues/7980#issuecomment-492784093)
eval "$(conda shell.bash hook)"
# Set the name of the conda environment – this is what will be typed
# to activate conda: `conda activate $ENVNAME`
# Here we set it to the name of the directory containing this file,
# in lowercase for easier typing.
# If the parent's name is 'code', the name of its parent is used instead.
# TODO: Drop certain chars, like '-'. Would be much easier in Python…
namedir=$DIR
if [ ! $ENVNAME ]; then # Skip if ENVNAME is hard-coded in script
ENVNAME=`basename "$namedir"`
while [ "$ENVNAME" == "code" ]; do
namedir=`dirname "$namedir"`
ENVNAME=`basename "$namedir"`
done
ORIGENVNAME=$ENVNAME
ENVNAME="${ENVNAME,,}" # Use shell parameter expansion to make lowercase
fi
# Make `conda activate` work in shell script (https://github.com/conda/conda/issues/7980#issuecomment-492784093)
eval "$(conda shell.bash hook)"
## Ensure we don't clobber an existing environment
# Get the list of current conda environments
envlist="$(conda env list | tr "*" " ")"
for env in $envlist; do
if [ "$env" == "$ENVNAME" ]; then
# if [[ "$envlist" == *"$ENVNAME "* ]]; then #
echo "Environment '$ENVNAME' is already installed."
doenvinstall=n
break
else
doenvinstall=y
fi
done
# Determine the root directory (PROJECTROOT) for the project
# PROJECTROOT should not be in the repo: it's a place to put project-related
# stuff (like environments) which are not tracked
# Here we set it to the parent folder of the repository
# Move up until we find the root directory for the repo
if [ ! $PROJECTROOT ]; then # Skip if PROJECTROOT is hard-coded in script
cd "$namedir"
while [ -d .git ]; do
if [ "$(pwd)" == "/" ]; then
echo "Cannot find a parent which isn't a git repository."
echo "Rather than relying on this script, you specify the PROJECTROOT by editing the file install.sh."
exit
fi
cd ..
done
PROJECTROOT="$(readlink -f .)"
# (readlink -f turns relative path into absolute)
# Change cur dir back to DIR
cd $DIR
fi
# Set the environments directory
if [ ! $ENVDIR ]; then # Skip if ENVDIR is hard-coded in script
ENVDIR="$PROJECTROOT/envs"
fi
# Determine the name of the environment YAML file
# We take the first file which matches, in order
# - $ENVNAME.yml
# - environment.yml
# - env.yml
if [ $ENVYML ]; then
1 # pass
elif [ -f "$ENVNAME.yaml" ]; then
ENVYML="$ENVNAME.yaml"
elif [ -f "$ENVNAME.yml" ]; then
ENVYML="$ENVNAME.yml"
elif [ -f "environment.yaml" ]; then
ENVYML="environment.yaml"
elif [ -f "environment.yml" ]; then
ENVYML="environment.yml"
elif [ -f "env.yaml" ]; then
ENVYML="env.yaml"
elif [ -f "env.yml" ]; then
ENVYML="env.yml"
else
echo "Could not find an environment YAML file. Aborting."
exit
fi
# Determine which conda env files to merge
# The found ENVYML is used as a stem for specialized files
# TODO: Use the found ENVYML filename as a stem for suffixes
env_files=$ENVYML
stem="${ENVYML%.*}"
ext="${1##*.}"
if [ $INSTALL_LOCATION == "local" ] && [ -e $stem-local.$ext ]; then
env_files="$env_files $stem-local.$ext"
fi
# Verify with the user the auto-detected file names and paths
if [ $doenvinstall == "y" ]; then
echo "You are about to install a conda environment with the following properties:"
echo " - Environment name: $ENVNAME"
echo " - Environment location: $ENVDIR"
echo " - YAML description file: $ENVYML"
echo ""
read -p "Proceed ? (Ctrl-C to abort) " -n 1
## Create conda environment ##
# # Create and activate new environment
# conda env create --prefix "$ENVDIR/$ENVNAME" --file "$ENVYML"
# Merge the conda env files
# We need 'conda-merge' for this, which we install in a throaway environment
# Create a throaway environment in which to install conda-merge
echo ""
echo "${bold}Merging environment files...${normal}"
python3 -m venv /tmp/tmp-python
source /tmp/tmp-python/bin/activate
pip install --upgrade pip wheel
pip install conda-merge
conda-merge $env_files > _merged_env.yaml
deactivate
rm -r /tmp/tmp-python
# Create the new environment, using the merged environment file
echo ""
echo "${bold}Creating Conda environment...${normal}"
if [ -e "$ENVDIR/$ENVNAME" ]; then
echo ""
echo "${bold}ERROR:${normal} The directory '$ENVDIR/$ENVNAME' already exists. If this is a conda "
echo "environment, it is not known to your current conda installation. "
echo "(One way this can happen is if the directory is copied from elsewhere.) "
echo "The simplest resolution is probably to delete or move the directory "
echo "and let conda install a fresh environment."
fi
conda env create --prefix "$ENVDIR/$ENVNAME" --file _merged_env.yaml
# TODO: only append if not already in `conda env list`
conda config --append envs_dirs "$ENVDIR"
conda activate "$ENVDIR/$ENVNAME"
# Add a pyvenv.cfg file prohibiting the use of user site packages.
# (This is the default for normal Python venvs, but not Conda environments)
# The pyvenv.cfg file must be placed one directory above the Python executable
# (see https://docs.python.org/3/library/site.html)
THISENVDIR="$( cd "$( dirname "$( dirname "$(which python)" )" )" && pwd )"
echo "include-system-site-packages = false" >> $THISENVDIR/pyvenv.cfg
# Unlikely that the pyvenv.cfg file exists, but append (instead of write) in case it does
# NB: $THISENVDIR should be equivalent to $ENVDIR/$ENVNAME, but I prefer to
# explicitly go up one directory up rather than rely on Conda conventions
# Install pip requirements
# Since these may be sensitive to the installation order (especially if
# they depend on git repos), the requirements files may be named
# requirements-1.txt, requirements-2.txt, etc.
if [ -e requirements*.txt ]; then
for file in requirements*.txt; do
echo ""
echo "${bold}Installing additional dependencies from $file...${normal}"
pip install -r "$file"
done
fi
else
conda activate "$ENVDIR/$ENVNAME"
echo ""
echo "Detected YAML file: $ENVYML"
echo "Environment installation was skipped because it is already installed."
read -p "Would you like to update the environment based on the merged environment file (y/N) ? " -n 1 update_env
if [[ $update_env =~ ^[Yy]$ ]]; then
# TODO: Don’t repeat code from above
# Merge the conda env files
# We need 'conda-merge' for this, which we install in a throaway environment
# Create a throaway environment in which to install conda-merge
echo ""
echo "${bold}Merging environment files...${normal}"
python3 -m venv /tmp/tmp-python
source /tmp/tmp-python/bin/activate
pip install --upgrade pip
pip install conda-merge
conda-merge $env_files > _merged_env.yaml
deactivate
rm -r /tmp/tmp-python
conda env update --file _merged_env.yaml
if [ -e requirements*.txt ]; then
for file in requirements*.txt; do
echo ""
echo "${bold}Installing additional dependencies from $file...${normal}"
pip install -r $file
done
fi
fi
fi
echo ""
read -p "Proceed with registering the kernel and installing local packages ? (Ctrl-C to abort) " -n 1
conda activate "$ENVDIR/$ENVNAME"
# Add the conda channels as defaults for this environment
# TODO: Read from env.yaml
# TODO: Only add those not already present
conda config --env --prepend channels conda-forge
# If `ipykernel` is in the '.yml' file, make the new kernel discoverable from Jupyter
if grep -q ipykernel _merged_env.yaml; then
echo ""
echo "${bold}Registering new environment as the IPython kernel \"$ENVNAME\".${normal}"
python -m ipykernel install --user --name $ENVNAME --display-name "Python ($ORIGENVNAME)"
fi
cd "$DIR"
# Install any bundled library dependencies
if [ -d "lib" ] && [ -n "$(ls "lib")" ]; then
# TODO: Set a flag and print this at the end
echo ""
echo "You have bundled dependencies in the 'lib' directory."
echo "This is intended for development only; when deploying, specify all "
echo "dependencies environment or requirements files"
for package in $(ls lib); do
pip install -e "lib/$package"
if [ -d "src/$package" ]; then
# Remove conda's source install, which we've just replaced
yes | rm -r "src/$package"
fi
done
fi
# Delete conda's src directory if it's now empty
if [ -d src ]; then
if [ -z "$(ls -A src)" ]; then # See https://superuser.com/a/352290
rm -r src
fi
fi
## Install the code in this directory
if [ -e "setup.py" ]; then
echo ""
echo "${bold}Installing project code...${normal}"
pip install -e .
fi
### Preparatory configuration for R
# This section creates a .Rprofile file which will point Python to the
# correct conda environment.
# This .Rprofile expects a file ~/.conda/condapath, which is created if
# necessary.
# TODO: Make .Rprofile optional
# Ensure the ~/.conda/condapath file is present
# We do it this way because conda installs itself in such a way that only login
# shells can see it, and R loads a non-login shell. Otherwise conda only works
# with one of a handful of hard-coded paths which R knows.
if [ ! -e "$HOME/.conda/condapath" ]; then
condapath="$(which conda)"
echo ""
if [ $condapath ]; then
echo "$condapath" > "$HOME/.conda/condapath"
echo "Stored the path to the conda executable in \`~/.conda/condapath\`."
else
echo "Could not find the conda executable. The \`condapath\` file could not be created."
fi
fi
# Create the .Rprofile file
if [ ! -e .Rprofile ]; then
cat >.Rprofile <<EOF
# ~/.conda/condapath is a text file with the path to conda, created by install.sh
condapath <- utils::read.table("~/.conda/condapath")
condapath <- as.vector(condapath\$V1[1])
library(reticulate)
reticulate::use_condaenv("$ENVNAME", conda=condapath, required=TRUE)
EOF
echo "Created an .Rprofile file. If you create an RStudio project in this directory, it will use this conda environment."
fi
# Remind user to set up smttask
echo ""
echo "Remember to complete the project setup by intializing smttask:"
echo ""
echo "\$ conda activate $ENVNAME"
echo "\$ smttask init"
echo ""