-
Notifications
You must be signed in to change notification settings - Fork 6
/
build_conda_env.sh
executable file
·234 lines (207 loc) · 7.79 KB
/
build_conda_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
#!/usr/bin/env bash
# This file is part of Karabo.
#
# http://www.karabo.eu
#
# Copyright (C) European XFEL GmbH Schenefeld. All rights reserved.
#
# Karabo is free software: you can redistribute it and/or modify it under
# the terms of the MPL-2 Mozilla Public License.
#
# You should have received a copy of the MPL-2 Public License along with
# Karabo. If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
#
# Karabo 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.
# Script for activating the KaraboGUI conda environment, also offers some
# auxiliary functionality like building the conda package or cleaning the
# environment.
# Help function for checking successful execution of commands
safeRunCondaCommand() {
typeset cmnd="$*"
typeset ret_code
echo cmnd=${cmnd}
eval ${cmnd}
ret_code=$?
if [ ${ret_code} != 0 ]; then
printf "Error : [%d] when executing command: '${cmnd}'" ${ret_code}
echo
echo
return ${ret_code}
fi
}
karaboCondaGetScriptPath() {
# This function allows to follow links and identify the path of this script.
# since this script should run in OsX as well, we cannot simply use the `-f`
# option of readlink
ORIGIN_PWD=$PWD
# get the absolute path
# in linux one would not need this trick. This will work also on BSD based systems like OsX
SCRIPT_PATH=$(cd "$(dirname ${BASH_SOURCE[0]})" ; pwd -P)
cd $SCRIPT_PATH
TARGET_FILE=$(basename ${BASH_SOURCE[0]})
# follow the symlinks if any
while [ -L "$TARGET_FILE" ]
do
TARGET_FILE=`readlink $TARGET_FILE`
SCRIPT_PATH=$(cd "$(dirname ${TARGET_FILE})" ; pwd -P)
cd $SCRIPT_PATH
TARGET_FILE=`basename $TARGET_FILE`
done
cd $ORIGIN_PWD
unset ORIGIN_PWD
}
karaboCondaCleanEnvironment() {
typeset env_name="$1"
if [ "${CLEAN}" != true ]; then
# NOP
return 0
fi
# exit environment that will be wiped, if we are in it.
if [ "${CONDA_DEFAULT_ENV}" == "${env_name}" ]; then
safeRunCondaCommand conda deactivate || safeRunCondaCommand source deactivate || return 1
fi
# wipe the environment
if [[ `conda info --envs | grep ${env_name}` != "" ]]; then
echo "### removed '${env_name}' conda environment ###"
safeRunCondaCommand conda env remove -n ${env_name} -y || return 1
else
echo "### '${env_name}' conda environment inexistent, nothing to clean ###"
fi
if [ "$SETUP_FLAG" != false ]; then
_RECIPE_DIR=${SCRIPT_PATH}/conda-recipes/${KARABO_ENV}
echo
echo "### Creating '${env_name}' conda environment ###"
echo
# create the base environment programmatically
safeRunCondaCommand conda activate || safeRunCondaCommand source activate || return 1
safeRunCondaCommand conda devenv --file ${_RECIPE_DIR}/environment.devenv.yml || return 1
unset _RECIPE_DIR
fi
}
karaboCondaCheckBaseEnvironment() {
echo "> build_conda_env.sh::karaboCondaCheckBaseEnvironment: CONDA_DEFAULT_ENV = $CONDA_DEFAULT_ENV"
# make sure we are in the default environment
if [ -z "${CONDA_DEFAULT_ENV}" ]; then
safeRunCondaCommand conda activate || safeRunCondaCommand source activate || safeRunCondaCommand activate base || return 1
elif [ "${CONDA_DEFAULT_ENV}" != "base" ]; then
echo "> build_conda_env.sh::karaboCondaCheckBaseEnvironment: will activate base environment"
safeRunCondaCommand conda activate || return 1
fi
# check if the base environment is sufficient to setup the recipes
BUILD_PKGS=("conda-build" "conda-devenv" "cogapp" "setuptools_scm")
for pkg in "${BUILD_PKGS[@]}"; do
echo "> build_conda_env.sh::karaboCondaCheckBaseEnvironment: checking for pkg = $pkg"
if [ -z "$(conda list ${pkg} | grep -v '#')" ]; then
echo "Conda environment missing package from needed packages ${BUILD_PKGS}"
return 1
fi
done
}
karaboCondaInstallGUIEnvironment() {
KARABO_ENV=karabogui
# Clean environment if needed
karaboCondaCleanEnvironment ${KARABO_ENV} || return 1
if [ "$SETUP_FLAG" == false ]; then
# NOP
return 0
fi
safeRunCondaCommand conda activate ${KARABO_ENV} || return 1
# We are breaking the module integrity of pythonKarabo, from which we only need pythonKarabo/common and /native.
# This flag is used in the setup.py to filter which modules to select. Ideally native and common should
# be in their own package or at least their own conda recipe.
pushd ${SCRIPT_PATH}/src/pythonKarabo
export BUILD_KARABO_SUBMODULE=NATIVE
safeRunCondaCommand python3 setup.py ${SETUP_FLAG} || return 1
unset BUILD_KARABO_SUBMODULE
popd
pushd ${SCRIPT_PATH}/src/pythonGui
safeRunCondaCommand python3 setup.py ${SETUP_FLAG} || return 1
popd
}
displayHelp() {
echo "
Usage: build_conda_env.sh install|develop|clean [envs]
This script should be sourced so the environment is activated in the caller shell.
If you don't source it, just run 'conda activate karabogui' in the end.
The optional list of environments will allow one to develop/install multiple
conda development environments. No environment means the karabogui environment
for backward compatibility.
Usage example:
source build_conda_env.sh clean develop
Will:
- Clean the 'karabogui' environment
- Create it again
- Install 'karabogui' in development mode
The installation is usually not needed as all code is added in the PYTHONPATH,
but installing it you will have access to the entrypoints (karabo-gui, etc)
Note: "install" installs karabogui in release mode
"develop" installs karabogui in development mode
"clean" cleans the environment
Note: The environment variable XFEL_CONDA_CHANNEL can optionally be used to point
the build process to alternative Conda repos.
This is useful, e.g. if one needs to tunnel into a private network.
If one follows the instructions in doc/installation/gui.rst, this script
should be called as:
XFEL_CONDA_CHANNEL=localhost:8081 source build_conda_env.sh clean develop"
}
SETUP_FLAG=false
CLEAN=false
ENVS=()
# Parse command line
while [ -n "$1" ]; do
case "$1" in
install|develop)
if [ "${SETUP_FLAG}" != false ]; then
echo "Can't use 'install' and 'develop' flags at the same time"
return 1
fi
SETUP_FLAG=$1
shift
;;
clean)
CLEAN=true
shift
;;
karabogui|karabo-cpp|karabo-mdl)
echo "> build_conda_env.sh: arg[1] = $1"
if [ "${SETUP_FLAG}" == false ]; then
displayHelp
return 1
fi
ENVS+=($1)
shift
;;
-h|--help)
displayHelp
return 0
;;
*)
# Make a little noise
echo "Unrecognized commandline flag: $1"
return 1
esac
done
if [ "${#ENVS[@]}" == "0" ]; then
ENVS+=("karabogui")
fi
if [[ "${SETUP_FLAG}" == false && "${CLEAN}" == false ]]; then
displayHelp
return 1
fi
CONDA_ROOT=`conda info --json | grep root_prefix | awk -F ": " '{printf $2;}' | sed 's|[,"]||g'`
CONDA_LOCAL_CHANNEL="file://${CONDA_ROOT}/conda-bld"
CONDA_LOCAL_CHANNEL_ENV=" - ${CONDA_LOCAL_CHANNEL}"
# functions are not exported to subshells.
source ${CONDA_ROOT}/etc/profile.d/conda.sh
echo "> build_conda_env.sh: will check conda base environment"
karaboCondaCheckBaseEnvironment || return 1
karaboCondaGetScriptPath
for ENV_NAME in $ENVS; do
case "$ENV_NAME" in
karabogui)
karaboCondaInstallGUIEnvironment || return 1
;;
esac
done