From f7d1aa7d9481d79991b6395cf84b755068301fae Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Sun, 8 Nov 2015 05:18:25 +0000 Subject: [PATCH 1/4] Allow activating first version of virtualenv even if there are multiple versions in pyenv (#105) --- bin/pyenv-sh-activate | 101 +++++++++++++++++++++----------------- bin/pyenv-sh-deactivate | 45 +++++++---------- bin/pyenv-virtualenv-init | 18 ++----- 3 files changed, 79 insertions(+), 85 deletions(-) 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 < Date: Sun, 8 Nov 2015 06:32:20 +0000 Subject: [PATCH 2/4] Fix tests --- test/activate.bats | 92 ++++++++++++++++++++++---------------- test/conda-activate.bats | 46 +------------------ test/conda-deactivate.bats | 9 ---- test/deactivate.bats | 41 ++--------------- test/init.bats | 27 +++-------- 5 files changed, 66 insertions(+), 149 deletions(-) diff --git a/test/activate.bats b/test/activate.bats index d274e9b..c9caaf6 100644 --- a/test/activate.bats +++ b/test/activate.bats @@ -13,7 +13,6 @@ setup() { stub pyenv-version-name "echo venv" stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" - stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate @@ -24,8 +23,6 @@ setup() { assert_success assert_output </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 }; From bdd52a79cd6a61c55376fd76e5dbe49c4926cf81 Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Sun, 8 Nov 2015 08:24:07 +0000 Subject: [PATCH 3/4] Revert `PYENV_DEACTIVATE` for backward compatibility --- bin/pyenv-sh-activate | 24 ++++++++++++++++++++++-- bin/pyenv-sh-deactivate | 14 ++++++++++++-- test/activate.bats | 10 ++++++++++ test/conda-activate.bats | 5 +++++ test/conda-deactivate.bats | 3 +++ test/deactivate.bats | 11 +++++++++++ 6 files changed, 63 insertions(+), 4 deletions(-) diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 5917e17..75fc1c6 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -15,6 +15,7 @@ set -e [ -n "$PYENV_DEBUG" ] && set -x unset DEACTIVATE +unset FORCE unset QUIET unset VERBOSE @@ -28,6 +29,9 @@ while [ $# -gt 0 ]; do "--deactivate" ) DEACTIVATE=1 ;; + "--force" ) + FORCE=1 + ;; "--no-error" | "--quiet" ) QUIET=1 ;; @@ -95,6 +99,16 @@ if [[ "${VIRTUAL_ENV}" == "${prefix}" ]]; then 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 + # 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 @@ -132,10 +146,16 @@ fi # virtualenv/pyvenv case "${shell}" in fish ) - echo "setenv VIRTUAL_ENV \"${prefix}\";" + cat < Date: Sun, 8 Nov 2015 09:18:29 +0000 Subject: [PATCH 4/4] Add short options --- bin/pyenv-sh-activate | 8 ++++---- bin/pyenv-sh-deactivate | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 75fc1c6..9c46f0c 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -26,20 +26,20 @@ while [ $# -gt 0 ]; do echo --unset exec pyenv-virtualenvs --bare ;; - "--deactivate" ) + "-d" | "--deactivate" ) DEACTIVATE=1 ;; - "--force" ) + "-f" | "--force" ) FORCE=1 ;; - "--no-error" | "--quiet" ) + "-q" | "--quiet" ) QUIET=1 ;; "--unset" ) echo "pyenv deactivate" exit ;; - "--verbose" ) + "-v" | "--verbose" ) VERBOSE=1 ;; * ) diff --git a/bin/pyenv-sh-deactivate b/bin/pyenv-sh-deactivate index 56f51b6..0a251a3 100755 --- a/bin/pyenv-sh-deactivate +++ b/bin/pyenv-sh-deactivate @@ -15,13 +15,13 @@ unset VERBOSE while [ $# -gt 0 ]; do case "$1" in - "--force" ) + "-f" | "--force" ) FORCE=1 ;; - "--no-error" | "--quiet") + "-q" | "--quiet") QUIET=1 ;; - "--verbose" ) + "-v" | "--verbose" ) VERBOSE=1 ;; * )