Skip to content

Commit

Permalink
Make conda_env_setup more verbose
Browse files Browse the repository at this point in the history
Use verbose mode when sourcing conda_init and print other
messages. Use more robust method of finding repo directory.
  • Loading branch information
tsj5 committed Apr 24, 2020
1 parent 3cf9820 commit bea57b0
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/conda/conda_env_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ set -Eeo pipefail
# https://www.gnu.org/software/bash/manual/bashref.html#Pattern-Matching
shopt -s extglob

# get directory this script is located in
script_dir=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
# paranoid code to get directory this script is located in, resolving any
# symlinks/aliases (https://stackoverflow.com/a/246128)
_source="${BASH_SOURCE[0]}"
while [ -h "$_source" ]; do # resolve $_source until the file is no longer a symlink
script_dir="$( cd -P "$( dirname "$_source" )" >/dev/null 2>&1 && pwd )"
_source="$( readlink "$_source" )"
# if $_source was a relative symlink, we need to resolve it relative to the
# path where the symlink file was located
[[ $_source != /* ]] && _source="$script_dir/$_source"
done
script_dir="$( cd -P "$( dirname "$_source" )" >/dev/null 2>&1 && pwd )"

# relative paths resolved relative to repo directory, which is grandparent
repo_dir=$( cd "${script_dir}/../.." ; pwd -P )
repo_dir="$( cd -P "$script_dir" >/dev/null 2>&1 && cd ../../ && pwd )"

pushd "$PWD" > /dev/null
# parse aruments manually
Expand All @@ -27,7 +37,7 @@ while (( "$#" )); do
-e|--env)
# specify one env by name
env_glob="env_${2}.yml"
if [[ ! -f "${script_dir}/${env_glob}" ]]; then
if [ ! -f "${script_dir}/${env_glob}" ]; then
echo "ERROR: ${script_dir}/${env_glob} not found."
exit 1
fi
Expand All @@ -46,7 +56,7 @@ while (( "$#" )); do
-d|--env_dir)
# specify install destination; resolve path first
cd "$repo_dir"
if [[ ! -d "$2" ]]; then
if [ ! -d "$2" ]; then
echo "Creating directory $2"
mkdir -p "$2"
fi
Expand All @@ -57,7 +67,7 @@ while (( "$#" )); do
-c|--conda_root)
# manually specify path to conda installation; resolve path first
cd "$repo_dir"
if [[ ! -d "$2" ]]; then
if [ ! -d "$2" ]; then
echo "ERROR: can't find conda dir $2"
exit 1
fi
Expand All @@ -76,53 +86,54 @@ done
popd > /dev/null # restore CWD

# setup conda in non-interactive shell
if [[ -z "$_MDTF_CONDA_ROOT" ]]; then
if [ -z "$_MDTF_CONDA_ROOT" ]; then
set -- # clear cmd line
source "${script_dir}/conda_init.sh"
. "${script_dir}/conda_init.sh" -v
else
# pass conda installation dir to setup script
source "${script_dir}/conda_init.sh" "$_MDTF_CONDA_ROOT"
. "${script_dir}/conda_init.sh" -v "$_MDTF_CONDA_ROOT"
fi
if [[ -z "$_CONDA_ENV_ROOT" ]]; then
if [ -z "$_CONDA_ENV_ROOT" ]; then
# not set, create conda env without --prefix
echo "Installing envs into system Anaconda"
else
# set, create and change conda envs using --prefix
echo "Installing envs into $_CONDA_ENV_ROOT"
echo "To use envs interactively, run conda config --append envs_dirs $_CONDA_ENV_ROOT"
echo "To use envs interactively, run \"conda config --append envs_dirs $_CONDA_ENV_ROOT\""
fi

# create all envs in a loop
"$CONDA_EXE" clean -i
for env_file in "${script_dir}/"${env_glob}; do
[[ -e "$env_file" ]] || continue # catch the case where nothing matches
[ -e "$env_file" ] || continue # catch the case where nothing matches
# get env name from reading "name:" attribute of yaml file
env_name=$( sed -n "s/^[[:space:]]*name:[[:space:]]*\([[:alnum:]_\-]*\)[[:space:]]*/\1/p" "$env_file" )
if [[ -z "$_CONDA_ENV_ROOT" ]]; then
echo "Creating conda env ${env_name}"
if [ -z "$_CONDA_ENV_ROOT" ]; then
echo "Creating conda env ${env_name}..."
"$CONDA_EXE" env create --force -q -f="$env_file"
else
conda_prefix="${_CONDA_ENV_ROOT}/${env_name}"
echo "Creating conda env ${env_name} in ${conda_prefix}"
echo "Creating conda env ${env_name} in ${conda_prefix}..."
"$CONDA_EXE" env create --force -q -p="$conda_prefix" -f="$env_file"
fi
echo "... conda env ${env_name} created."
done
"$CONDA_EXE" clean -ay

# create script wrapper to activate base environment
_CONDA_WRAPPER="${repo_dir}/mdtf"
if [[ -f "$_CONDA_WRAPPER" ]]; then
if [ -e "$_CONDA_WRAPPER" ]; then
rm -f "$_CONDA_WRAPPER"
fi
echo '#!/usr/bin/env bash' > "$_CONDA_WRAPPER"
echo "# This wrapper script is generated by conda_env_setup.sh." >> "$_CONDA_WRAPPER"
echo "_mdtf_src=\"${repo_dir}/src\"" >> "$_CONDA_WRAPPER"
echo "source \${_mdtf_src}/conda/conda_init.sh -q \"${_CONDA_ROOT}\"" >> "$_CONDA_WRAPPER"
if [[ -z "$_CONDA_ENV_ROOT" ]]; then
if [ -z "$_CONDA_ENV_ROOT" ]; then
echo "conda activate _MDTF_base" >> "$_CONDA_WRAPPER"
else
echo "conda activate ${_CONDA_ENV_ROOT}/_MDTF_base" >> "$_CONDA_WRAPPER"
fi
echo "\${_mdtf_src}/mdtf.py \"\$@\"" >> "$_CONDA_WRAPPER"
chmod +x "$_CONDA_WRAPPER"
echo "Created wrapper script at env ${_CONDA_WRAPPER}"
echo "Created MDTF wrapper script at ${_CONDA_WRAPPER}"

0 comments on commit bea57b0

Please sign in to comment.