diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 08c07c9c..4fff6763 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -70,11 +70,7 @@ if pyenv-prefix "${versions}" 1>/dev/null 2>&1; then fi else version="$(pyenv-version-name)" - if [[ "${versions}" == "root" ]]; then - versions="${version%/envs/*}" - else - versions="${version%/envs/*}/envs/${versions}" - fi + versions="${version%/envs/*}/envs/${versions}" if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null 2>&1; then [ -n "$NOERROR" ] || echo "pyenv-virtualenv: version \`${versions}' is not an environment of anaconda/miniconda" 1>&2 echo "false" @@ -85,21 +81,6 @@ fi shell="${PYENV_SHELL:-${SHELL##*/}}" prefix="$(pyenv-prefix "${versions}")" -unset conda_env - -if [ -f "${prefix}/bin/conda" ]; then - if [[ "$shell" != "bash" ]] && [[ "$shell" != "zsh" ]]; then - [ -n "$NOERROR" ] || echo "pyenv-virtualenv: Only bash and zsh are supported by Anaconda/Miniconda" 1>&2 - echo "false" - exit 1 - fi - if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then - conda_env="${prefix##*/envs/}" - else - conda_env="root" - 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 @@ -124,29 +105,65 @@ if [ -z "$no_shell" ]; then esac fi -if [ -n "${conda_env}" ]; then - # anaconda/miniconda +case "${shell}" in +fish ) + cat <&2 - echo "false" - exit 1 - fi - if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then - conda_env="${prefix##*/envs/}" - else - conda_env="root" - fi -fi - -if [ -n "${conda_env}" ]; then - echo "if [ -f \"${prefix%/envs/*}/bin/deactivate\" ]; then" -else - case "$shell" in - fish ) - echo "if functions -q deactivate;" - ;; - * ) - echo "if declare -f deactivate 1>/dev/null 2>&1; then" - ;; - esac +if [ -z "${VIRTUAL_ENV}" ]; then + [ -n "$NOERROR" ] || echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2 + echo "false" + exit 1 fi if [ -n "$VERBOSE" ]; then - echo " echo \"pyenv-virtualenv: deactivate ${prefix##*/}\" 1>&2;" + echo "pyenv-virtualenv: deactivate ${prefix##*/}" 1>&2 fi 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;" + echo "pyenv shell --unset;" case "$shell" in fish ) - echo " set -e PYENV_ACTIVATE_SHELL;" + echo "set -e PYENV_ACTIVATE_SHELL;" ;; * ) - echo " unset PYENV_ACTIVATE_SHELL;" + echo "unset PYENV_ACTIVATE_SHELL;" ;; esac fi -if [ -n "${conda_env}" ]; then +case "${shell}" in +fish ) cat <&2;" +if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT}" ]; then + : # TODO: change prompt? fi - -echo " false;" - -case "$shell" in -fish ) - echo "end;" - ;; -* ) - echo "fi;" - ;; -esac diff --git a/bin/pyenv-virtualenv-prefix b/bin/pyenv-virtualenv-prefix index 858ff723..d0976703 100755 --- a/bin/pyenv-virtualenv-prefix +++ b/bin/pyenv-virtualenv-prefix @@ -31,31 +31,15 @@ base_prefix() { # pyvenv } VIRTUALENV_PREFIX_PATHS=() -for version_string in "${versions[@]}"; do - version="${version_string%%/*}" +for version in "${versions[@]}"; do if [ "$version" = "system" ]; then echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2 exit 1 fi - PREFIX="$(pyenv-prefix "${version}")" - if [ -f "${PREFIX}/bin/activate" ]; then - # Anaconda has `activate` script nevertheless it is not a virtual environment (#65) - if [ -f "${PREFIX}/bin/conda" ]; then - if [[ "${version_string}" != "${version_string%/envs/*}" ]]; then - conda_env="${version_string##*/envs/}" - if [ -f "${PREFIX}/envs/${conda_env}/bin/conda" ]; then - VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "${PREFIX}") - else - echo "pyenv-virtualenv: version \`${version_string}' is not an environment of anaconda/miniconda" 1>&2 - exit 1 - fi - else - VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "${PREFIX}") - fi - else - VIRTUALENV_PREFIX_PATH="$(real_prefix "${version}" || base_prefix "${version}" || true)" - VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "$VIRTUALENV_PREFIX_PATH") - fi + PYENV_PREFIX_PATH="$(pyenv-prefix "${version}")" + if [ -f "${PYENV_PREFIX_PATH}/bin/activate" ]; then + VIRTUALENV_PREFIX_PATH="$(real_prefix "${version}" || base_prefix "${version}" || true)" + VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "${VIRTUALENV_PREFIX_PATH:-${PYENV_PREFIX_PATH}}") else echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2 exit 1 diff --git a/bin/pyenv-virtualenvs b/bin/pyenv-virtualenvs index 64895653..23d715d0 100755 --- a/bin/pyenv-virtualenvs +++ b/bin/pyenv-virtualenvs @@ -16,7 +16,7 @@ if [ "$1" = "--bare" ]; then hit_prefix="" miss_prefix="" current_versions=() - print_origin="" + unset print_origin else hit_prefix="* " miss_prefix=" " @@ -24,42 +24,39 @@ else print_origin="1" fi -array_exists() { - local x car="$1" +exists() { + local car="$1" + local cdar shift - for x in "$@"; do - [ "${x}" = "${car}" ] && return 0 + for cdar in "$@"; do + if [ "${car}" == "${cdar}" ]; then + return 0 + fi done return 1 } print_version() { - if [ -n "${print_origin}" ]; then - local version_origin="$2" - else - local version_origin="" - fi - if array_exists "$1" "${current_versions[@]}"; then - echo "${hit_prefix}${1}${version_origin}" + if exists "$1" "${current_versions[@]}"; then + echo "${hit_prefix}${1}${print_origin+$2}" else - echo "${miss_prefix}${1}${version_origin}" + echo "${miss_prefix}${1}${print_origin+$2}" fi } -for version in $(pyenv-versions --bare); do - if [[ "${version}" != "system" ]]; then - virtualenv_prefix="$(pyenv-virtualenv-prefix "${version}" 2>/dev/null || true)" +shopt -s nullglob +for path in "${PYENV_ROOT}/versions/"*; do + version="${path##*/}" + virtualenv_prefix="$(pyenv-virtualenv-prefix "${version}" 2>/dev/null || true)" + if [ -d "${virtualenv_prefix}" ]; then + print_version "${version}" " (created from ${virtualenv_prefix})" + fi + for venv_path in "${path}/envs/"*; do + venv_version="${version}/envs/${venv_path##*/}" + virtualenv_prefix="$(pyenv-virtualenv-prefix "${venv_version}" 2>/dev/null || true)" if [ -d "${virtualenv_prefix}" ]; then - print_version "${version}" " (created from ${virtualenv_prefix})" - prefix="$(pyenv-prefix "${version}")" - if [ -f "${prefix}/bin/conda" ]; then - # envs of anaconda/miniconda - shopt -s nullglob - for conda_env in "${prefix}/envs/"*; do - print_version "${version##*/}${conda_env#${prefix}}" " (created from ${prefix})" - done - shopt -u nullglob - fi + print_version "${venv_version}" " (created from ${virtualenv_prefix})" fi - fi + done done +shopt -u nullglob diff --git a/test/activate.bats b/test/activate.bats index 88237498..e812c07b 100644 --- a/test/activate.bats +++ b/test/activate.bats @@ -25,7 +25,7 @@ setup() { assert_output <&2; - false; -fi; +export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; +unset PYENV_ACTIVATE; +unset VIRTUAL_ENV; +unset CONDA_DEFAULT_ENV; EOS } @test "deactivate conda root (fish)" { + export VIRTUAL_ENV="${PYENV_ROOT}/versions/anaconda-2.3.0" export PYENV_ACTIVATE="${PYENV_ROOT}/versions/anaconda-2.3.0" export PYENV_ACTIVATE_SHELL= + export CONDA_DEFAULT_ENV="root" + create_conda "anaconda-2.3.0" PYENV_SHELL="fish" run pyenv-sh-deactivate - assert_failure + assert_success assert_output <&2; - false; -fi; +export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; +unset PYENV_ACTIVATE; +unset VIRTUAL_ENV; +unset CONDA_DEFAULT_ENV; EOS } diff --git a/test/conda-virtualenv-prefix.bats b/test/conda-virtualenv-prefix.bats index d7a928b0..489087c7 100644 --- a/test/conda-virtualenv-prefix.bats +++ b/test/conda-virtualenv-prefix.bats @@ -14,28 +14,28 @@ setup() { PYENV_VERSION="anaconda-2.3.0" run pyenv-virtualenv-prefix - unstub pyenv-version-name - unstub pyenv-prefix - assert_success assert_output </dev/null 2>&1; then - export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; - unset PYENV_ACTIVATE; - deactivate; -else - echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2; - false; -fi; +export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; +unset PYENV_ACTIVATE; +unset VIRTUAL_ENV; EOS } @test "deactivate virtualenv (verbose)" { + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE_SHELL= @@ -33,19 +30,15 @@ EOS assert_success assert_output </dev/null 2>&1; then - echo "pyenv-virtualenv: deactivate venv" 1>&2; - export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; - unset PYENV_ACTIVATE; - deactivate; -else - echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2; - false; -fi; +pyenv-virtualenv: deactivate venv +export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; +unset PYENV_ACTIVATE; +unset VIRTUAL_ENV; EOS } @test "deactivate virtualenv (no-error)" { + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE_SHELL= @@ -53,17 +46,14 @@ EOS assert_success assert_output </dev/null 2>&1; then - export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; - unset PYENV_ACTIVATE; - deactivate; -else - false; -fi; +export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; +unset PYENV_ACTIVATE; +unset VIRTUAL_ENV; EOS } @test "deactivate virtualenv (with shell activation)" { + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE_SHELL=1 @@ -71,20 +61,16 @@ EOS assert_success assert_output </dev/null 2>&1; then - pyenv shell --unset; - unset PYENV_ACTIVATE_SHELL; - export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; - unset PYENV_ACTIVATE; - deactivate; -else - echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2; - false; -fi; +pyenv shell --unset; +unset PYENV_ACTIVATE_SHELL; +export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; +unset PYENV_ACTIVATE; +unset VIRTUAL_ENV; EOS } @test "deactivate virtualenv (with shell activation) (no-error)" { + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE_SHELL=1 @@ -92,15 +78,11 @@ EOS assert_success assert_output </dev/null 2>&1; then - pyenv shell --unset; - unset PYENV_ACTIVATE_SHELL; - export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; - unset PYENV_ACTIVATE; - deactivate; -else - false; -fi; +pyenv shell --unset; +unset PYENV_ACTIVATE_SHELL; +export PYENV_DEACTIVATE="$PYENV_ACTIVATE"; +unset PYENV_ACTIVATE; +unset VIRTUAL_ENV; EOS } @@ -113,18 +95,14 @@ EOS assert_success assert_output </dev/null 2>&1; then - export PYENV_DEACTIVATE="$VIRTUAL_ENV"; - unset PYENV_ACTIVATE; - deactivate; -else - echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2; - false; -fi; +export PYENV_DEACTIVATE="$VIRTUAL_ENV"; +unset PYENV_ACTIVATE; +unset VIRTUAL_ENV; EOS } @test "deactivate virtualenv (fish)" { + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE_SHELL= @@ -132,18 +110,14 @@ EOS assert_success assert_output <&2; - false; -end; +setenv PYENV_DEACTIVATE "$PYENV_ACTIVATE"; +set -e PYENV_ACTIVATE; +set -e VIRTUAL_ENV; EOS } @test "deactivate virtualenv (fish) (no-error)" { + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE_SHELL= @@ -151,17 +125,14 @@ EOS assert_success assert_output <&2; - false; -end; +pyenv shell --unset; +set -e PYENV_ACTIVATE_SHELL; +setenv PYENV_DEACTIVATE "$PYENV_ACTIVATE"; +set -e PYENV_ACTIVATE; +set -e VIRTUAL_ENV; EOS } @test "deactivate virtualenv (fish) (with shell activation) (no-error)" { + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv" export PYENV_ACTIVATE_SHELL=1 @@ -190,15 +157,11 @@ EOS assert_success assert_output <&2; - false; -end; +setenv PYENV_DEACTIVATE "$VIRTUAL_ENV"; +set -e PYENV_ACTIVATE; +set -e VIRTUAL_ENV; EOS } diff --git a/test/virtualenvs.bats b/test/virtualenvs.bats index 62cd8136..9079efb1 100644 --- a/test/virtualenvs.bats +++ b/test/virtualenvs.bats @@ -12,66 +12,55 @@ setup() { @test "list virtual environments only" { stub pyenv-version-name ": echo system" - stub pyenv-versions "--bare : echo \"system\";echo \"2.7.6\";echo \"3.3.3\";echo \"venv27\";echo \"venv33\"" stub pyenv-virtualenv-prefix "2.7.6 : false" stub pyenv-virtualenv-prefix "3.3.3 : false" stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/2.7.6\"" stub pyenv-virtualenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/3.3.3\"" - stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/2.7.6\"" - stub pyenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/3.3.3\"" run pyenv-virtualenvs - unstub pyenv-version-name - unstub pyenv-versions - unstub pyenv-virtualenv-prefix - unstub pyenv-prefix - assert_success assert_output <