activate "root" environment of anaconda/miniconda if environment name is not given

This commit is contained in:
Yamashita, Yuu
2015-07-19 04:19:05 +09:00
parent 25395f0331
commit cdbf7c9972
6 changed files with 34 additions and 37 deletions

View File

@@ -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 <<EOS
unset PYENV_DEACTIVATE;
export PYENV_ACTIVATE="${prefix}";
. "${prefix%/envs/*}/bin/activate" "${versions##*/}";
. "${prefix%/envs/*}/bin/activate" "${conda_env}";
EOS
else
# virtualenv/pyvenv

View File

@@ -33,23 +33,16 @@ prefix="${PYENV_ACTIVATE:-${VIRTUAL_ENV}}"
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
if [ -n "${conda_env}" ]; then

View File

@@ -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)"

View File

@@ -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})"

View File

@@ -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

View File

@@ -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 <<OUT
@@ -57,11 +60,14 @@ 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 --bare
unstub pyenv-versions
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix
assert_success
assert_output <<OUT