diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 4f60a63..5917e17 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -14,6 +14,7 @@ set -e [ -n "$PYENV_DEBUG" ] && set -x +unset DEACTIVATE unset QUIET unset VERBOSE @@ -24,6 +25,9 @@ while [ $# -gt 0 ]; do echo --unset exec pyenv-virtualenvs --bare ;; + "--deactivate" ) + DEACTIVATE=1 + ;; "--no-error" | "--quiet" ) QUIET=1 ;; @@ -56,30 +60,40 @@ if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then no_shell= fi -if [ "${#versions[@]}" -gt 1 ]; then - [ -n "$QUIET" ] || echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2 +venv="${versions}" + +if ! pyenv-virtualenv-prefix "${venv}" 1>/dev/null 2>&1; then + if [ -z "$QUIET" ]; then + echo "pyenv-virtualenv: version \`${venv}' is not a virtualenv" 1>&2 + fi echo "false" exit 1 fi -if pyenv-prefix "${versions}" 1>/dev/null 2>&1; then - if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null 2>&1; then - [ -n "$QUIET" ] || echo "pyenv-virtualenv: version \`${versions}' is not a virtualenv" 1>&2 - echo "false" - exit 1 +# exit as error if there are multiple virtualenvs +# https://github.com/yyuu/pyenv-virtualenv/issues/105 +for version in "${versions[@]}"; do + if [[ "${version}" != "${venv}" ]]; then + if pyenv-virtualenv-prefix "${version}" 1>/dev/null 2>&1; then + if [ -z "$QUIET" ]; then + echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2 + fi + echo "false" + exit 1 + fi fi -else - version="$(pyenv-version-name)" - versions="${version%/envs/*}/envs/${versions}" - if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null 2>&1; then - [ -n "$QUIET" ] || echo "pyenv-virtualenv: version \`${versions}' is not an environment of anaconda/miniconda" 1>&2 - echo "false" - exit 1 - fi -fi +done shell="${PYENV_SHELL:-${SHELL##*/}}" -prefix="$(pyenv-prefix "${versions}")" +prefix="$(pyenv-prefix "${venv}")" + +if [[ "${VIRTUAL_ENV}" == "${prefix}" ]]; then + if [ -z "${QUIET}" ]; then + echo "pyenv-virtualenv: version \`${venv}' is already activated" 1>&2 + fi + echo "true" + exit 0 +fi # Display setup instruction, if pyenv-virtualenv has not been initialized. # if 'pyenv virtualenv-init -' is not found in "$profile" @@ -87,36 +101,33 @@ if [ -z "$PYENV_VIRTUALENV_INIT" ]; then pyenv-virtualenv-init >&2 || true fi -echo "pyenv-virtualenv: activate ${versions}" 1>&2 - -if [ -z "$no_shell" ]; then - echo "pyenv shell \"${versions}\";" - # shell version set in pyenv-sh-activate should be unset - # https://github.com/yyuu/pyenv-virtualenv/issues/61 - case "$shell" in - fish ) - echo "setenv PYENV_ACTIVATE_SHELL 1;" - ;; - * ) - echo "export PYENV_ACTIVATE_SHELL=1;" - ;; - esac +if [ -n "${DEACTIVATE}" ]; then + pyenv-sh-deactivate ${QUIET+--quiet} ${VERBOSE+--verbose} fi -case "${shell}" in -fish ) - cat <&2 + +if [ -z "$no_shell" ]; then + # shell version set in pyenv-sh-activate should be unset + # https://github.com/yyuu/pyenv-virtualenv/issues/61 + OLDIFS="$IFS" + IFS=: + case "$shell" in + fish ) + cat <&2 +if [ -z "${VIRTUAL_ENV}" ]; then + if [ -z "$QUIET" ]; then + echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2 + fi echo "false" exit 1 fi +shell="$(basename "${PYENV_SHELL:-$SHELL}")" +prefix="${VIRTUAL_ENV}" + if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then - version="$(basename "${prefix%/envs/*}")/envs/${prefix##*/}" + venv="$(basename "${prefix%/envs/*}")/envs/${prefix##*/}" else - version="${prefix##*/}" + venv="${prefix##*/}" fi -echo "pyenv-virtualenv: deactivate ${version}" 1>&2 + +echo "pyenv-virtualenv: deactivate ${venv}" 1>&2 if [ -n "${PYENV_ACTIVATE_SHELL}" ]; then # shell version set in pyenv-sh-activate should be unset # https://github.com/yyuu/pyenv-virtualenv/issues/61 - echo "pyenv shell --unset;" case "$shell" in fish ) - echo "set -e PYENV_ACTIVATE_SHELL;" + cat </dev/null; or true) ] - pyenv deactivate --quiet; or true - pyenv activate --quiet; or true - end + pyenv activate --deactivate --quiet; or pyenv deactivate --quiet; or true else - if [ "\$PYENV_DEACTIVATE" != (pyenv prefix 2>/dev/null; or true) ] - pyenv activate --quiet; or true - end + pyenv activate --quiet; or true end return \$ret end @@ -133,14 +128,9 @@ if [[ "$shell" != "fish" ]]; then cat <