From cdbf7c9972cd96f829e032ab7139305aee4b44a7 Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Sun, 19 Jul 2015 04:19:05 +0900 Subject: [PATCH] activate "root" environment of anaconda/miniconda if environment name is not given --- bin/pyenv-sh-activate | 30 ++++++++++++++---------------- bin/pyenv-sh-deactivate | 21 +++++++-------------- bin/pyenv-virtualenv-prefix | 3 +-- bin/pyenv-virtualenvs | 5 ++--- test/deactivate.bats | 2 ++ test/virtualenvs.bats | 10 ++++++++-- 6 files changed, 34 insertions(+), 37 deletions(-) diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 046d030..08c07c9 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -69,7 +69,12 @@ if pyenv-prefix "${versions}" 1>/dev/null 2>&1; then exit 1 fi else - versions="$(pyenv-version-name)/envs/${versions}" + version="$(pyenv-version-name)" + if [[ "${versions}" == "root" ]]; then + versions="${version%/envs/*}" + else + versions="${version%/envs/*}/envs/${versions}" + fi 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" @@ -83,23 +88,16 @@ prefix="$(pyenv-prefix "${versions}")" unset conda_env if [ -f "${prefix}/bin/conda" ]; then - if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then - case "$shell" in - bash ) - conda_env="${prefix##*/envs/}" - ;; - zsh ) - conda_env="${prefix##*/envs/}" - ;; - * ) - echo "false" - exit 1 - ;; - esac - else + 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. @@ -131,7 +129,7 @@ if [ -n "${conda_env}" ]; then 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 diff --git a/bin/pyenv-virtualenv-prefix b/bin/pyenv-virtualenv-prefix index b1ae326..dad9688 100755 --- a/bin/pyenv-virtualenv-prefix +++ b/bin/pyenv-virtualenv-prefix @@ -50,8 +50,7 @@ for version_string in "${versions[@]}"; do exit 1 fi else - echo "pyenv-virtualenv: version \`${version_string}' is not a virtualenv" 1>&2 - exit 1 + VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "${PREFIX}") fi else VIRTUALENV_PREFIX_PATH="$(real_prefix "${version}" || base_prefix "${version}" || true)" diff --git a/bin/pyenv-virtualenvs b/bin/pyenv-virtualenvs index c550b1c..6489565 100755 --- a/bin/pyenv-virtualenvs +++ b/bin/pyenv-virtualenvs @@ -51,10 +51,9 @@ for version in $(pyenv-versions --bare); do virtualenv_prefix="$(pyenv-virtualenv-prefix "${version}" 2>/dev/null || true)" if [ -d "${virtualenv_prefix}" ]; then print_version "${version}" " (created from ${virtualenv_prefix})" - else - # envs of anaconda/miniconda prefix="$(pyenv-prefix "${version}")" - if [ -f "${prefix}/bin/activate" ] && [ -f "${prefix}/bin/conda" ]; then + 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})" diff --git a/test/deactivate.bats b/test/deactivate.bats index e3366cc..0329031 100644 --- a/test/deactivate.bats +++ b/test/deactivate.bats @@ -106,6 +106,7 @@ EOS @test "deactivate virtualenv which has been activated manually" { export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" + export PYENV_ACTIVATE= export PYENV_ACTIVATE_SHELL= PYENV_SHELL="bash" run pyenv-sh-deactivate @@ -203,6 +204,7 @@ EOS @test "deactivate virtualenv which has been activated manually (fish)" { export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" + export PYENV_ACTIVATE= export PYENV_ACTIVATE_SHELL= PYENV_SHELL="fish" run pyenv-sh-deactivate diff --git a/test/virtualenvs.bats b/test/virtualenvs.bats index e27fc66..62cd813 100644 --- a/test/virtualenvs.bats +++ b/test/virtualenvs.bats @@ -17,8 +17,8 @@ setup() { 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 "2.7.6 : echo \"${PYENV_ROOT}/versions/2.7.6\"" - stub pyenv-prefix "3.3.3 : 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 @@ -39,12 +39,15 @@ OUT stub pyenv-versions "--bare : echo \"system\";echo \"venv27\";echo \"venv33\"" stub pyenv-virtualenv-prefix "venv27 : echo \"/usr\"" stub pyenv-virtualenv-prefix "venv33 : echo \"/usr\"" + stub pyenv-prefix "venv27 : echo \"/usr\"" + stub pyenv-prefix "venv33 : echo \"/usr\"" run pyenv-virtualenvs unstub pyenv-version-name unstub pyenv-versions unstub pyenv-virtualenv-prefix + unstub pyenv-prefix assert_success assert_output <