Use PYENV_DEACTIVATE to store which virtualenv has been deactivated (#32)

This commit is contained in:
Yamashita Yuu
2014-06-30 22:40:59 +09:00
parent 7079cbe979
commit 08b4c94f85
6 changed files with 169 additions and 72 deletions

View File

@@ -75,6 +75,16 @@ if [ -z "$no_shell" ]; then
fi fi
case "$shell" in case "$shell" in
fish ) echo ". \"$(pyenv-prefix "${versions}")/bin/activate.fish\"" ;; fish )
* ) echo "source \"$(pyenv-prefix "${versions}")/bin/activate\"" ;; cat <<EOS
set -e PYENV_DEACTIVATE;
. "$(pyenv-prefix "${versions}")/bin/activate.fish";
EOS
;;
* )
cat <<EOS
unset PYENV_DEACTIVATE;
source "$(pyenv-prefix "${versions}")/bin/activate";
EOS
;;
esac esac

View File

@@ -11,12 +11,26 @@ set -e
shell="$(basename "${PYENV_SHELL:-$SHELL}")" shell="$(basename "${PYENV_SHELL:-$SHELL}")"
case "$shell" in case "$shell" in
fish ) echo "functions -q deactivate; and deactivate;";; fish )
* ) echo "declare -f deactivate 1>/dev/null 2>&1 && deactivate;";; cat <<EOS
if functions -q deactivate
setenv PYENV_DEACTIVATE "\$VIRTUAL_ENV";
deactivate;
end;
EOS
;;
* )
cat <<EOS
if declare -f deactivate 1>/dev/null 2>&1; then
export PYENV_DEACTIVATE="\$VIRTUAL_ENV";
deactivate;
fi;
EOS
;;
esac esac
if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then
# Backward compatibility issue # Backward compatibility issue
# https://github.com/yyuu/pyenv-virtualenv/issues/26 # https://github.com/yyuu/pyenv-virtualenv/issues/26
echo "pyenv shell --unset" echo "pyenv shell --unset;"
fi fi

View File

@@ -63,53 +63,84 @@ if [ -z "$print" ]; then
fi fi
case "$shell" in case "$shell" in
bash ) fish )
echo "setenv PYENV_VIRTUALENV_INIT 1;"
;;
* )
echo "export PYENV_VIRTUALENV_INIT=1;"
;;
esac
case "$shell" in
fish )
cat <<EOS
function _pyenv_virtualenv_hook --on-event fish_prompt;
if [ -n "\$VIRTUAL_ENV" ]
if [ (pyenv version-name) = "system" ]
eval (pyenv sh-deactivate); or true
return 0
end
if [ "\$VIRTUAL_ENV" != (pyenv prefix) ]
eval (pyenv sh-deactivate); or true
eval (pyenv sh-activate 2>/dev/null); or true
end
else
if [ "\$PYENV_DEACTIVATE" != (pyenv prefix) ]
eval (pyenv sh-activate 2>/dev/null); or true
end
end
end
EOS
;;
ksh )
cat <<EOS
function _pyenv_virtualenv_hook() {
EOS
;;
* )
cat <<EOS cat <<EOS
export PYENV_VIRTUALENV_INIT=1
_pyenv_virtualenv_hook() { _pyenv_virtualenv_hook() {
if [[ "\$(pyenv version-name)" == "system" ]]; then EOS
pyenv deactivate || true; ;;
elif [[ "\$VIRTUAL_ENV" != "\$(pyenv prefix)" ]]; then esac
pyenv deactivate || true;
pyenv activate 2>/dev/null || true if [[ "$shell" != "fish" ]]; then
cat <<EOS
if [ -n "\$VIRTUAL_ENV" ]; then
if [ "x\`pyenv version-name\`" = "xsystem" ]; then
pyenv deactivate || true
return 0
fi
if [ "x\$VIRTUAL_ENV" != "x\`pyenv prefix\`" ]; then
pyenv deactivate || true
pyenv activate 2>/dev/null || true
fi
else
if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then
pyenv activate 2>/dev/null || true
fi
fi fi
}; };
EOS
case "$shell" in
bash )
cat <<EOS
if ! [[ "\$PROMPT_COMMAND" =~ _pyenv_virtualenv_hook ]]; then if ! [[ "\$PROMPT_COMMAND" =~ _pyenv_virtualenv_hook ]]; then
PROMPT_COMMAND="_pyenv_virtualenv_hook;\$PROMPT_COMMAND"; PROMPT_COMMAND="_pyenv_virtualenv_hook;\$PROMPT_COMMAND";
fi fi
EOS EOS
;; ;;
fish ) zsh )
cat <<EOS cat <<EOS
setenv PYENV_VIRTUALENV_INIT 1;
function _pyenv_virtualenv_hook --on-event fish_prompt;
if [ (pyenv version-name) = "system" ]
eval (pyenv sh-deactivate); or true
else if [ "\$VIRTUAL_ENV" != (pyenv prefix) ]
eval (pyenv sh-deactivate); or true
eval (pyenv sh-activate 2>/dev/null); or true
end
end
EOS
;;
zsh )
cat <<EOS
export PYENV_VIRTUALENV_INIT=1
_pyenv_virtualenv_hook() {
if [[ "\$(pyenv version-name)" == "system" ]]; then
pyenv deactivate || true
elif [[ "\$VIRTUAL_ENV" != "\$(pyenv prefix)" ]]; then
pyenv deactivate || true
pyenv activate 2>/dev/null || true
fi
}
typeset -a precmd_functions typeset -a precmd_functions
if [[ -z \$precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then if [[ -z \$precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then
precmd_functions+=_pyenv_virtualenv_hook; precmd_functions+=_pyenv_virtualenv_hook;
fi fi
EOS EOS
;; ;;
* ) * )
# FIXME: what should i do here?? # FIXME: what should i do here??
;; ;;
esac esac
fi

View File

@@ -22,7 +22,8 @@ setup() {
assert_success assert_success
assert_output <<EOS assert_output <<EOS
source "${PYENV_ROOT}/versions/venv/bin/activate" unset PYENV_DEACTIVATE;
source "${PYENV_ROOT}/versions/venv/bin/activate";
EOS EOS
} }
@@ -42,7 +43,8 @@ EOS
assert_success assert_success
assert_output <<EOS assert_output <<EOS
pyenv shell "venv"; pyenv shell "venv";
source "${PYENV_ROOT}/versions/venv/bin/activate" unset PYENV_DEACTIVATE;
source "${PYENV_ROOT}/versions/venv/bin/activate";
EOS EOS
} }
@@ -61,7 +63,8 @@ EOS
assert_success assert_success
assert_output <<EOS assert_output <<EOS
. "${PYENV_ROOT}/versions/venv/bin/activate.fish" set -e PYENV_DEACTIVATE;
. "${PYENV_ROOT}/versions/venv/bin/activate.fish";
EOS EOS
} }
@@ -81,7 +84,8 @@ EOS
assert_success assert_success
assert_output <<EOS assert_output <<EOS
pyenv shell "venv"; pyenv shell "venv";
. "${PYENV_ROOT}/versions/venv/bin/activate.fish" set -e PYENV_DEACTIVATE;
. "${PYENV_ROOT}/versions/venv/bin/activate.fish";
EOS EOS
} }
@@ -99,7 +103,8 @@ EOS
assert_success assert_success
assert_output <<EOS assert_output <<EOS
pyenv shell "venv27"; pyenv shell "venv27";
source "${PYENV_ROOT}/versions/venv27/bin/activate" unset PYENV_DEACTIVATE;
source "${PYENV_ROOT}/versions/venv27/bin/activate";
EOS EOS
} }
@@ -117,7 +122,8 @@ EOS
assert_success assert_success
assert_output <<EOS assert_output <<EOS
pyenv shell "venv27"; pyenv shell "venv27";
source "${PYENV_ROOT}/versions/venv27/bin/activate" unset PYENV_DEACTIVATE;
source "${PYENV_ROOT}/versions/venv27/bin/activate";
EOS EOS
} }

View File

@@ -13,7 +13,10 @@ setup() {
assert_success assert_success
assert_output <<EOS assert_output <<EOS
declare -f deactivate 1>/dev/null 2>&1 && deactivate; if declare -f deactivate 1>/dev/null 2>&1; then
export PYENV_DEACTIVATE="\$VIRTUAL_ENV";
deactivate;
fi;
EOS EOS
} }
@@ -24,8 +27,11 @@ EOS
assert_success assert_success
assert_output <<EOS assert_output <<EOS
declare -f deactivate 1>/dev/null 2>&1 && deactivate; if declare -f deactivate 1>/dev/null 2>&1; then
pyenv shell --unset export PYENV_DEACTIVATE="\$VIRTUAL_ENV";
deactivate;
fi;
pyenv shell --unset;
EOS EOS
} }
@@ -36,7 +42,10 @@ EOS
assert_success assert_success
assert_output <<EOS assert_output <<EOS
functions -q deactivate; and deactivate; if functions -q deactivate
setenv PYENV_DEACTIVATE "\$VIRTUAL_ENV";
deactivate;
end;
EOS EOS
} }
@@ -47,8 +56,11 @@ EOS
assert_success assert_success
assert_output <<EOS assert_output <<EOS
functions -q deactivate; and deactivate; if functions -q deactivate
pyenv shell --unset setenv PYENV_DEACTIVATE "\$VIRTUAL_ENV";
deactivate;
end;
pyenv shell --unset;
EOS EOS
} }

View File

@@ -29,13 +29,21 @@ load test_helper
run pyenv-virtualenv-init - bash run pyenv-virtualenv-init - bash
assert_success assert_success
assert_output <<EOS assert_output <<EOS
export PYENV_VIRTUALENV_INIT=1 export PYENV_VIRTUALENV_INIT=1;
_pyenv_virtualenv_hook() { _pyenv_virtualenv_hook() {
if [[ "\$(pyenv version-name)" == "system" ]]; then if [ -n "\$VIRTUAL_ENV" ]; then
pyenv deactivate || true; if [ "x\`pyenv version-name\`" = "xsystem" ]; then
elif [[ "\$VIRTUAL_ENV" != "\$(pyenv prefix)" ]]; then pyenv deactivate || true
pyenv deactivate || true; return 0
pyenv activate 2>/dev/null || true fi
if [ "x\$VIRTUAL_ENV" != "x\`pyenv prefix\`" ]; then
pyenv deactivate || true
pyenv activate 2>/dev/null || true
fi
else
if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then
pyenv activate 2>/dev/null || true
fi
fi fi
}; };
if ! [[ "\$PROMPT_COMMAND" =~ _pyenv_virtualenv_hook ]]; then if ! [[ "\$PROMPT_COMMAND" =~ _pyenv_virtualenv_hook ]]; then
@@ -50,11 +58,19 @@ EOS
assert_output <<EOS assert_output <<EOS
setenv PYENV_VIRTUALENV_INIT 1; setenv PYENV_VIRTUALENV_INIT 1;
function _pyenv_virtualenv_hook --on-event fish_prompt; function _pyenv_virtualenv_hook --on-event fish_prompt;
if [ (pyenv version-name) = "system" ] if [ -n "\$VIRTUAL_ENV" ]
eval (pyenv sh-deactivate); or true if [ (pyenv version-name) = "system" ]
else if [ "\$VIRTUAL_ENV" != (pyenv prefix) ] eval (pyenv sh-deactivate); or true
eval (pyenv sh-deactivate); or true return 0
eval (pyenv sh-activate 2>/dev/null); or true end
if [ "\$VIRTUAL_ENV" != (pyenv prefix) ]
eval (pyenv sh-deactivate); or true
eval (pyenv sh-activate 2>/dev/null); or true
end
else
if [ "\$PYENV_DEACTIVATE" != (pyenv prefix) ]
eval (pyenv sh-activate 2>/dev/null); or true
end
end end
end end
EOS EOS
@@ -64,15 +80,23 @@ EOS
run pyenv-virtualenv-init - zsh run pyenv-virtualenv-init - zsh
assert_success assert_success
assert_output <<EOS assert_output <<EOS
export PYENV_VIRTUALENV_INIT=1 export PYENV_VIRTUALENV_INIT=1;
_pyenv_virtualenv_hook() { _pyenv_virtualenv_hook() {
if [[ "\$(pyenv version-name)" == "system" ]]; then if [ -n "\$VIRTUAL_ENV" ]; then
pyenv deactivate || true if [ "x\`pyenv version-name\`" = "xsystem" ]; then
elif [[ "\$VIRTUAL_ENV" != "\$(pyenv prefix)" ]]; then pyenv deactivate || true
pyenv deactivate || true return 0
pyenv activate 2>/dev/null || true fi
if [ "x\$VIRTUAL_ENV" != "x\`pyenv prefix\`" ]; then
pyenv deactivate || true
pyenv activate 2>/dev/null || true
fi
else
if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then
pyenv activate 2>/dev/null || true
fi
fi fi
} };
typeset -a precmd_functions typeset -a precmd_functions
if [[ -z \$precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then if [[ -z \$precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then
precmd_functions+=_pyenv_virtualenv_hook; precmd_functions+=_pyenv_virtualenv_hook;