Last active
August 29, 2015 14:22
-
-
Save amelio-vazquez-reina/0befbd311e96f8787754 to your computer and use it in GitHub Desktop.
activate (conda)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Ensure that this script is sourced, not executed | |
# Also note that errors are ignored as `activate foo` doesn't generate a bad | |
# value for $0 which would cause errors. | |
if [[ -n $BASH_VERSION ]] && [[ "$(basename "$0" 2> /dev/null)" == "activate" ]]; then | |
>&2 echo "Error: activate must be sourced. Run 'source activate envname' | |
instead of 'activate envname'. | |
" | |
"$(dirname "$0")/conda" ..activate -h | |
exit 1 | |
fi | |
# Determine the directory containing this script | |
if [[ -n $BASH_VERSION ]]; then | |
_SCRIPT_LOCATION=${BASH_SOURCE[0]} | |
elif [[ -n $ZSH_VERSION ]]; then | |
_SCRIPT_LOCATION=${funcstack[1]} | |
else | |
echo "Only bash and zsh are supported" | |
return 1 | |
fi | |
_THIS_DIR=$(dirname "$_SCRIPT_LOCATION") | |
# Load common functions | |
get_dirname() { | |
echo "$(cd "$(dirname "$1")" && pwd)" | |
} | |
run_scripts() { | |
_PREFIX="$(echo $(echo $PATH | awk -F ':' '{print $1}')/..)" | |
_CONDA_D="${_PREFIX}/etc/conda/$1.d" | |
if [[ -d $_CONDA_D ]]; then | |
for f in $(find $_CONDA_D -name "*.sh"); do source $f; done | |
fi | |
} | |
# http://stackoverflow.com/a/21188136/161801 | |
get_abs_filename() { | |
echo "$(get_dirname "$1")/$(basename "$1")" | |
} | |
if "$_THIS_DIR/conda" ..checkenv "$@"; then | |
# Ensure we deactivate any scripts from the old env | |
run_scripts "deactivate" | |
_NEW_PATH=$("$_THIS_DIR/conda" ..deactivate) | |
export PATH=$_NEW_PATH | |
if (( $("$_THIS_DIR/conda" ..changeps1) )); then | |
if [[ -n $CONDA_OLD_PS1 ]]; then | |
PS1=$CONDA_OLD_PS1 | |
unset CONDA_OLD_PS1 | |
fi | |
fi | |
else | |
return 1 | |
fi | |
_NEW_PATH=$("$_THIS_DIR/conda" ..activate "$@") | |
if (( $? == 0 )); then | |
export PATH=$_NEW_PATH | |
# If the string contains / it's a path | |
if [[ "$@" == */* ]]; then | |
export CONDA_DEFAULT_ENV=$(get_abs_filename "$@") | |
else | |
export CONDA_DEFAULT_ENV="$@" | |
fi | |
export CONDA_ENV_PATH=$(get_dirname $_THIS_DIR) | |
if (( $("$_THIS_DIR/conda" ..changeps1) )); then | |
CONDA_OLD_PS1="$PS1" | |
PS1="($CONDA_DEFAULT_ENV)$PS1" | |
fi | |
else | |
return $? | |
fi | |
# Load any of the scripts found $PREFIX/etc/conda/activate.d | |
run_scripts "activate" | |
if [[ -n $BASH_VERSION ]]; then | |
hash -r | |
elif [[ -n $ZSH_VERSION ]]; then | |
rehash | |
else | |
echo "Only bash and zsh are supported" | |
return 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment