Skip to content

Rewrite activate without using virtualenv/venv/conda's script #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 51 additions & 34 deletions bin/pyenv-sh-activate
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -124,29 +105,65 @@ if [ -z "$no_shell" ]; then
esac
fi

if [ -n "${conda_env}" ]; then
# anaconda/miniconda
case "${shell}" in
fish )
cat <<EOS
set -e PYENV_DEACTIVATE;
setenv PYENV_ACTIVATE "${prefix}";
EOS
;;
* )
cat <<EOS
unset PYENV_DEACTIVATE;
export PYENV_ACTIVATE="${prefix}";
. "${prefix%/envs/*}/bin/activate" "${conda_env}";
EOS
else
# virtualenv/pyvenv
case "$shell" in
;;
esac

# virtualenv/pyvenv
case "${shell}" in
fish )
echo "setenv VIRTUAL_ENV \"${prefix}\";"
;;
* )
echo "export VIRTUAL_ENV=\"${prefix}\";"
;;
esac

# anaconda/miniconda
if [ -x "${prefix}/bin/conda" ]; then
if [[ "${versions}" != "${versions%/envs/*}" ]]; then
CONDA_DEFAULT_ENV="${versions##*/envs/}"
else
CONDA_DEFAULT_ENV="root"
fi
case "${shell}" in
fish )
echo "setenv CONDA_DEFAULT_ENV \"${CONDA_DEFAULT_ENV}\";"
;;
* )
echo "export CONDA_DEFAULT_ENV=\"${CONDA_DEFAULT_ENV}\";"
;;
esac
fi

if [ -n "${PYTHONHOME}" ]; then
case "${shell}" in
fish )
cat <<EOS
set -e PYENV_DEACTIVATE;
setenv PYENV_ACTIVATE "${prefix}";
. "${prefix}/bin/activate.fish";
setenv _OLD_VIRTUAL_PYTHONHOME "${PYTHONHOME}";
set -e PYTHONHOME;
EOS
;;
* )
;;
* )
cat <<EOS
unset PYENV_DEACTIVATE;
export PYENV_ACTIVATE="${prefix}";
. "${prefix}/bin/activate";
export _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME}";
unset PYTHONHOME;
EOS
;;
esac
fi

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT}" ]; then
: # TODO: change prompt?
fi
112 changes: 53 additions & 59 deletions bin/pyenv-sh-deactivate
Original file line number Diff line number Diff line change
Expand Up @@ -30,91 +30,85 @@ done
shell="$(basename "${PYENV_SHELL:-$SHELL}")"
prefix="${PYENV_ACTIVATE:-${VIRTUAL_ENV}}"

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

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 <<EOS
export PYENV_DEACTIVATE="$prefix";
unset PYENV_ACTIVATE;
. "${prefix%/envs/*}/bin/deactivate";
else
setenv PYENV_DEACTIVATE "$prefix";
set -e PYENV_ACTIVATE;
EOS
else
case "$shell" in
;;
* )
cat <<EOS
export PYENV_DEACTIVATE="$prefix";
unset PYENV_ACTIVATE;
EOS
;;
esac

# virtualenv/pyvenv
case "${shell}" in
fish )
echo "set -e VIRTUAL_ENV;"
;;
* )
echo "unset VIRTUAL_ENV;"
;;
esac


# anaconda/miniconda
if [ -n "${CONDA_DEFAULT_ENV}" ]; then
case "${shell}" in
fish )
echo "set -e CONDA_DEFAULT_ENV;"
;;
* )
echo "unset CONDA_DEFAULT_ENV;"
;;
esac
fi

if [ -n "${_OLD_VIRTUAL_PYTHONHOME}" ]; then
case "${shell}" in
fish )
cat <<EOS
setenv PYENV_DEACTIVATE "$prefix";
set -e PYENV_ACTIVATE;
deactivate;
else;
setenv PYTHONHOME "${_OLD_VIRTUAL_PYTHONHOME}";
set -e _OLD_VIRTUAL_PYTHONHOME;
EOS
;;
* )
* )
cat <<EOS
export PYENV_DEACTIVATE="$prefix";
unset PYENV_ACTIVATE;
deactivate;
else
export PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME}";
unset _OLD_VIRTUAL_PYTHONHOME;
EOS
;;
esac
fi

if [ -z "$NOERROR" ]; then
echo " echo \"pyenv-virtualenv: no virtualenv has been activated.\" 1>&2;"
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT}" ]; then
: # TODO: change prompt?
fi

echo " false;"

case "$shell" in
fish )
echo "end;"
;;
* )
echo "fi;"
;;
esac
26 changes: 5 additions & 21 deletions bin/pyenv-virtualenv-prefix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 24 additions & 27 deletions bin/pyenv-virtualenvs
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,47 @@ if [ "$1" = "--bare" ]; then
hit_prefix=""
miss_prefix=""
current_versions=()
print_origin=""
unset print_origin
else
hit_prefix="* "
miss_prefix=" "
current_versions=($(IFS=:; for version in $(pyenv-version-name); do echo "$version"; done))
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
Loading