diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 4f60a63..9c46f0c 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -14,6 +14,8 @@ set -e [ -n "$PYENV_DEBUG" ] && set -x +unset DEACTIVATE +unset FORCE unset QUIET unset VERBOSE @@ -24,14 +26,20 @@ while [ $# -gt 0 ]; do echo --unset exec pyenv-virtualenvs --bare ;; - "--no-error" | "--quiet" ) + "-d" | "--deactivate" ) + DEACTIVATE=1 + ;; + "-f" | "--force" ) + FORCE=1 + ;; + "-q" | "--quiet" ) QUIET=1 ;; "--unset" ) echo "pyenv deactivate" exit ;; - "--verbose" ) + "-v" | "--verbose" ) VERBOSE=1 ;; * ) @@ -56,82 +64,105 @@ 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 +done + +shell="${PYENV_SHELL:-${SHELL##*/}}" +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 + +if [[ "${PYENV_DEACTIVATE}" == "${prefix}" ]]; then + if [ -z "${FORCE}" ]; then + if [ -z "${QUIET}" ]; then + echo "pyenv-virtualenv: \`${venv}' is marked deactivated. use \`pyenv activate --force ${venv}' to activate forcibly." 1>&2 + fi echo "false" exit 1 fi fi -shell="${PYENV_SHELL:-${SHELL##*/}}" -prefix="$(pyenv-prefix "${versions}")" - # Display setup instruction, if pyenv-virtualenv has not been initialized. # if 'pyenv virtualenv-init -' is not found in "$profile" 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 +echo "pyenv-virtualenv: activate ${venv}" 1>&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 </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 @@ -106,14 +96,9 @@ export PYENV_VIRTUALENV_INIT=1; _pyenv_virtualenv_hook() { local ret=\$? if [ -n "\$VIRTUAL_ENV" ]; then - if [ "\$VIRTUAL_ENV" != "\$(pyenv prefix 2>/dev/null || true)" ]; then - eval "\$(pyenv sh-deactivate --quiet || true)" || true - eval "\$(pyenv sh-activate --quiet || true)" || true - fi + eval "\$(pyenv sh-activate --deactivate --quiet || pyenv sh-deactivate --quiet || true)" || true else - if [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then - eval "\$(pyenv sh-activate --quiet || true)" || true - fi + eval "\$(pyenv sh-activate --quiet || true)" || true fi return \$ret };