Skip to content
Permalink
Browse files

support newer conda

pyenv-virtualenvs could not list conda environments & pyenv shell would only activate the base conda environment
the conda detection criteria of testing the presence of `conda` or `activate` files under `$(pyenv root)/versions/$version/bin` appears to be the culprit, since newer environments no longer include these files: those files reside in the base conda environment
- add detection criteria of `$(pyenv root)/versions/$version/conda-meta`
- compute the real prefix to the base environment from `realpath $(realpath $(pyenv root)/versions/$version)/../..`
- to allow that, enhance substitute `realpath` in `pyenv-virtualenvs` to reduce relative paths `.` & `..`, and factor that code out to a file under `libexec` for reuse
- hook `which` to locate conda from the real prefix
  • Loading branch information
lmmarsano committed Nov 11, 2018
1 parent 8c9f7fa commit e0a1e9b35c2be386dbd91e686e57864690323a22
@@ -177,7 +177,8 @@ EOS
esac

# anaconda/miniconda
if [ -x "${prefix}/bin/conda" ]; then
if [ -d "${prefix}/conda-meta" ] ||
[ -x "${prefix}/bin/conda" ]; then
if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then
CONDA_DEFAULT_ENV="${venv##*/envs/}"
else
@@ -233,7 +234,8 @@ EOS
fi

# conda package anaconda/miniconda scripts (#173)
if [ -x "${prefix}/bin/conda" ]; then
if [ -d "${prefix}/conda-meta" ] ||
[ -x "${prefix}/bin/conda" ]; then
shopt -s nullglob
case "${shell}" in
fish )
@@ -59,7 +59,8 @@ if [ -n "$PYENV_VIRTUALENV_VERBOSE_ACTIVATE" ]; then
fi

# conda package anaconda/miniconda scripts (#173)
if [ -x "${prefix}/bin/conda" ]; then
if [ -d "${prefix}/conda-meta" ] ||
[ -x "${prefix}/bin/conda" ]; then
shopt -s nullglob
case "${shell}" in
fish )
@@ -6,6 +6,7 @@

set -e
[ -n "$PYENV_DEBUG" ] && set -x
. "${BASH_SOURCE%/*}"/../libexec/pyenv-virtualenv-realpath

if [ -z "$PYENV_ROOT" ]; then
PYENV_ROOT="${HOME}/.pyenv"
@@ -19,6 +20,15 @@ else
IFS=: versions=($(pyenv-version-name))
fi

append_virtualenv_prefix() {
if [ -d "${VIRTUALENV_PREFIX_PATH}" ]; then
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
fi
}

VIRTUALENV_PREFIX_PATHS=()
for version in "${versions[@]}"; do
if [ "$version" = "system" ]; then
@@ -55,12 +65,11 @@ for version in "${versions[@]}"; do
fi
fi
fi
if [ -d "${VIRTUALENV_PREFIX_PATH}" ]; then
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
fi
append_virtualenv_prefix
elif [ -d "${PYENV_PREFIX_PATH}/conda-meta" ]; then
# conda
VIRTUALENV_PREFIX_PATH="$(realpath "${PYENV_PREFIX_PATH}"/../..)"
append_virtualenv_prefix
else
echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2
exit 1
@@ -7,6 +7,7 @@

set -e
[ -n "$PYENV_DEBUG" ] && set -x
. "${BASH_SOURCE%/*}"/../libexec/pyenv-virtualenv-realpath

if [ -z "$PYENV_ROOT" ]; then
PYENV_ROOT="${HOME}/.pyenv"
@@ -32,38 +33,6 @@ done

versions_dir="${PYENV_ROOT}/versions"

if ! enable -f "${BASH_SOURCE%/*}"/../libexec/pyenv-realpath.dylib realpath 2>/dev/null; then
if [ -n "$PYENV_NATIVE_EXT" ]; then
echo "pyenv: failed to load \`realpath' builtin" >&2
exit 1
fi

READLINK=$(type -p greadlink readlink | head -1)
if [ -z "$READLINK" ]; then
echo "pyenv: cannot find readlink - are you missing GNU coreutils?" >&2
exit 1
fi

resolve_link() {
$READLINK "$1"
}

realpath() {
local cwd="$PWD"
local path="$1"
local name

while [ -n "$path" ]; do
name="${path##*/}"
[ "$name" = "$path" ] || cd "${path%/*}"
path="$(resolve_link "$name" || true)"
done

echo "${PWD}/$name"
cd "$cwd"
}
fi

if [ -d "$versions_dir" ]; then
versions_dir="$(realpath "$versions_dir")"
fi
@@ -0,0 +1,11 @@
# newer versions of conda share programs from the real prefix
# this hook tries to find the executable there

if [ ! -x "${PYENV_COMMAND_PATH}" ] && [[ "${PYENV_COMMAND_PATH##*/}" == "conda" ]]; then
if [ -d "${PYENV_ROOT}/versions/${version}/conda-meta" ]; then
conda_command_path="$(pyenv-virtualenv-prefix "$version")"/bin/"${PYENV_COMMAND_PATH##*/}"
if [ -x "${conda_command_path}" ]; then
PYENV_COMMAND_PATH="${conda_command_path}"
fi
fi
fi
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
# Summary: Substitute realpath if unavailable as a builtin or file
# Usage: . pyenv-virtualenv-realpath

if ! {
enable -f "${BASH_SOURCE%/*}"/../libexec/pyenv-realpath.dylib realpath ||
type realpath
} >/dev/null 2>&1; then
if [ -n "$PYENV_NATIVE_EXT" ]; then
echo "pyenv: failed to load \`realpath' builtin" >&2
exit 1
fi

READLINK=$(type -p greadlink readlink | head -1)
if [ -z "$READLINK" ]; then
echo "pyenv: cannot find readlink - are you missing GNU coreutils?" >&2
exit 1
fi

resolve_link() {
$READLINK "$1"
}

realpath() {
local f="$*" \
name dir
[[ $f ]] || {
>&2 echo ${FUNCNAME[0]}: missing operand
return
}
while [[ -L $f ]]; do
f="$(resolve_link "$f")"
done
if [[ ! -d $f ]]; then
name="/${f##*/}"
# parent?
dir="${f%/*}"
if [[ $dir == $f ]]; then
#lacks /: parent is current directory
f="$PWD"
else
f="$dir"
fi
fi
#absolute directory
dir="$(cd "$f"
pwd)"
echo "$dir$name"
}
fi

0 comments on commit e0a1e9b

Please sign in to comment.
You can’t perform that action at this time.