Allow activating first version of virtualenv even if there are multiple versions in pyenv (#105)

This commit is contained in:
Yamashita, Yuu
2015-11-08 05:18:25 +00:00
parent ce6ec4f173
commit f7d1aa7d94
3 changed files with 79 additions and 85 deletions

View File

@@ -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 <<EOS
set -e PYENV_DEACTIVATE;
setenv PYENV_ACTIVATE "${prefix}";
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 <<EOS
setenv PYENV_VERSION "${versions[*]}";
setenv PYENV_ACTIVATE_SHELL 1;
EOS
;;
* )
cat <<EOS
unset PYENV_DEACTIVATE;
export PYENV_ACTIVATE="${prefix}";
;;
* )
cat <<EOS
export PYENV_VERSION="${versions[*]}";
export PYENV_ACTIVATE_SHELL=1;
EOS
;;
esac
;;
esac
IFS="$OLDIFS"
fi
# virtualenv/pyvenv
case "${shell}" in
@@ -130,8 +141,8 @@ esac
# anaconda/miniconda
if [ -x "${prefix}/bin/conda" ]; then
if [[ "${versions}" != "${versions%/envs/*}" ]]; then
CONDA_DEFAULT_ENV="${versions##*/envs/}"
if [[ "${venv}" != "${venv%/envs/*}" ]]; then
CONDA_DEFAULT_ENV="${venv##*/envs/}"
else
CONDA_DEFAULT_ENV="root"
fi

View File

@@ -27,51 +27,44 @@ while [ $# -gt 0 ]; do
shift 1
done
shell="$(basename "${PYENV_SHELL:-$SHELL}")"
prefix="${VIRTUAL_ENV:-${PYENV_ACTIVATE}}"
if [ -z "${prefix}" ]; then
[ -n "$QUIET" ] || echo "pyenv-virtualenv: no virtualenv has been activated." 1>&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 <<EOS
set -e PYENV_VERSION;
set -e PYENV_ACTIVATE_SHELL;
EOS
;;
* )
echo "unset PYENV_ACTIVATE_SHELL;"
cat <<EOS
unset PYENV_VERSION;
unset PYENV_ACTIVATE_SHELL;
EOS
;;
esac
fi
case "${shell}" in
fish )
cat <<EOS
setenv PYENV_DEACTIVATE "$prefix";
set -e PYENV_ACTIVATE;
EOS
;;
* )
cat <<EOS
export PYENV_DEACTIVATE="$prefix";
unset PYENV_ACTIVATE;
EOS
;;
esac
# virtualenv/pyvenv
case "${shell}" in
fish )

View File

@@ -104,14 +104,9 @@ fish )
function _pyenv_virtualenv_hook --on-event fish_prompt;
set -l ret \$status
if [ -n "\$VIRTUAL_ENV" ]
if [ "\$VIRTUAL_ENV" != (pyenv prefix 2>/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 <<EOS
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
};