-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathpy_env_create
executable file
·246 lines (231 loc) · 7.86 KB
/
py_env_create
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
#!/bin/bash
#
# py_env_create -- setup the python environment in order to use CTSM python tools
#
# Simple bash script to setup the python environment for the user so they can run the CTSM
# python tools using "conda" or "mamba".
#
# Exit on Ctrl-C
trap 'echo "Exiting on Ctrl+C"; exit 130' INT
# Exit on errors
set -e
# Get the directory where py_env_create is located
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd "${SCRIPT_DIR}"
condadir="${SCRIPT_DIR}/python"
default_new_env_name=ctsm_pylib
default_old_env_name=ctsm_pylib_3_7_9
default_condafile="${condadir}/conda_env_ctsm_py.yml"
default_condafile_old="${condadir}/conda_env_ctsm_py_old_3.7.9.yml"
condafile="${default_condafile}"
quiet=""
verbose=""
option=""
yes=""
new_env_name=${default_new_env_name}
new_name_for_existing=""
overwrite=0
dry_run=0
condamamba="conda"
help=0
while [ $# -gt 0 ]; do
case $1 in
-h|--help )
help=1
;;
-d|--dry-run )
dry_run=1
;;
-f|--file )
condafile=$2
shift
;;
-m|--mamba )
condamamba="mamba"
;;
-n|--name )
new_env_name=$2
shift
;;
-o|--overwrite )
overwrite=1
;;
--old )
condafile="${default_condafile_old}"
new_env_name=${default_old_env_name}
;;
-q|--quiet )
quiet="--quiet"
;;
--option )
option=$2
shift
;;
-r|--rename-existing )
new_name_for_existing=$2
shift
;;
-v|--verbose )
verbose="--verbose"
;;
-y|--yes )
yes="--yes"
;;
* )
echo "ERROR:: invalid argument: '$2'"
usage
exit 1
;;
esac
shift
done
#----------------------------------------------------------------------
# Usage subroutine
usage() {
echo ""
echo "***********************************************************************"
echo "usage:"
echo "./py_env_create"
echo ""
echo "valid arguments: "
echo "[-h|--help] "
echo " Displays this help message"
echo "[-d|--dry-run] "
echo " Dry run: Print what would happen but don't actually do it."
echo "[-f|--file <file>] "
echo " Conda environment requirements text file to use (text format) in addition to the others"
echo " Assumed to be under the directory: $condadir"
echo " Default is '$default_condafile' (unless --old is given)"
echo "[-m|--mamba] "
echo " Use mamba instead of conda. This shouldn't be necessary on most systems because modern"
echo " conda installations use mamba as their backend."
echo "[-n|--name <environment name>] "
echo " Name of the environment you wish to create. Default: ${default_new_env_name} (unless --old is given)"
echo "[-o|--overwrite] "
echo " Overwrite existing env with same name, if any. Incompatible with -r/--rename-existing."
echo "[--old] "
echo " Install the previous version of ctsm_pylib (Python 3.7.9). Changes these to old-version defaults:"
echo " env name: ${default_old_env_name}"
echo " file: ${default_condafile_old}"
echo "[-q|--quiet] "
echo " Run with quiet mode to minimize Conda messages. Incompatible with -v/--verbose. May fail to quiet things if you don't also specify -y/--yes."
echo "[-r|--rename-existing] <name> "
echo " If an existing environment has the same name as the environment you're installing (${new_env_name}), rename the existing env to <name>. Incompatible with -o/--overwrite."
echo "[-v|--verbose] "
echo " Run with verbose mode so you see the progress bar. Incompatible with -q/--quiet."
echo "[-y|--yes] "
echo " Do not ask for confirmation"
echo "[--option <option>] "
echo " Option(s) to pass to 'conda install' step"
echo "***********************************************************************"
}
#----------------------------------------------------------------------
if [[ ${help} -eq 1 ]]; then
usage
exit 0
fi
#
# Error checking options and setup
#
if [[ "${verbose}" == "--verbose" && "${quiet}" == "--quiet" ]]; then
echo "-v/--verbose and -q/--quiet are not compatible" >&2
exit 1
fi
if [ ! -f $condafile ]; then
echo "$condafile does NOT exist"
echo "Use the --file option with a valid filename"
exit -1
fi
#
# Handle an environment that already exists
#
function conda_env_exists {
${condamamba} env list | grep -oE "/$1$" | wc -l
}
function rename_existing_env {
if [[ "$CONDA_DEFAULT_ENV" == *"$1" ]]; then
echo "Not going to let you rename the currently active env" >&2
exit 1
elif [[ $(conda_env_exists $2) -eq 0 ]]; then
echo "Renaming $1 to $2 (this will take a few minutes)..."
yes_tmp=${yes/yes/force}
# Use conda instead of $condamamba because, at least as of mamba 1.5.9 / conda 24.7.1,
# rename is not supported through mamba.
if [[ "${quiet}" == "--quiet" ]]; then
# -q/--quiet option not available on Derecho
(set -x; conda rename ${yes_tmp} -n $1 $2 1>/dev/null)
else
(set -x; conda rename ${yes_tmp} ${verbose} -n $1 $2)
fi
else
echo "$2 also already exists" >&2
exit 1
fi
}
function overwrite_existing_env {
if [[ "$CONDA_DEFAULT_ENV" == *"$1" ]]; then
echo "Not going to let you overwrite the currently active env" >&2
exit 1
elif [[ "${quiet}" == "--quiet" ]]; then
# -q/--quiet option not available on Derecho
(set -x; ${condamamba} remove ${yes} -n $1 --all ${quiet} 1>/dev/null)
else
(set -x; ${condamamba} remove ${yes} -n $1 --all ${verbose})
fi
}
if [[ "${new_name_for_existing}" != "" && ${overwrite} -eq 1 ]]; then
echo "Only specify one of -o/--overwrite or -r/--rename-existing." >&2
exit 1
fi
# Print plan (and check for unhandled existing env)
echo "Using ${condamamba} to install the python environment needed to run the CTSM python tools in the ${condamamba} environment: $new_env_name"
echo "Using the file: $condafile"
conda_env_does_exist=$(conda_env_exists ${new_env_name})
if [[ ${conda_env_does_exist} -gt 0 ]]; then
if [[ "${new_name_for_existing}" != "" ]]; then
echo "Renaming existing ${new_env_name} to ${new_name_for_existing}"
elif [[ ${overwrite} -eq 1 ]]; then
echo "Overwriting existing ${new_env_name}"
else
echo "Environment ${new_env_name} already exists." >&2
echo "Try again using one of:" >&2
echo " -n/--name <name> to choose a different name for the new env" >&2
echo " -r/--rename-existing <name> to choose a different name for the existing env" >&2
echo " -o/--overwrite <name> to overwrite the existing env" >&2
if [[ ${dry_run} -eq 0 ]]; then
exit 1
fi
fi
fi
# Exit if doing dry run
if [[ ${dry_run} -ne 0 ]]; then
echo "Exiting before any actions taken, due to -d/--dry-run option."
exit 0
fi
# Rename or overwrite existing
if [[ ${conda_env_does_exist} -gt 0 ]]; then
if [[ "${new_name_for_existing}" != "" ]]; then
rename_existing_env ${new_env_name} ${new_name_for_existing}
elif [[ ${overwrite} -eq 1 ]]; then
overwrite_existing_env ${new_env_name}
else
echo "How did you get here?" >&2
exit 1
fi
fi
#
# Create and install the environment
#
echo "Installing $new_env_name; this can take a few minutes."
if [[ "${quiet}" == "--quiet" ]]; then
(set -x; ${condamamba} env create ${yes} --file "${condafile}" --name $new_env_name ${quiet} $option 1>/dev/null)
else
(set -x; ${condamamba} env create ${yes} --file "${condafile}" --name $new_env_name $option ${verbose})
fi
#
# Report on success
#
echo "Successfully installed the $new_env_name python environment"
echo
echo "activate the environment by doing the following..."
echo "${condamamba} activate $new_env_name"