Skip to content

Fix: support newer conda #290

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 2 commits into from
Nov 23, 2018
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
6 changes: 4 additions & 2 deletions bin/pyenv-sh-activate
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand Down
3 changes: 2 additions & 1 deletion bin/pyenv-sh-deactivate
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
3 changes: 2 additions & 1 deletion bin/pyenv-virtualenv
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,11 @@ for hook in "${before_hooks[@]}"; do eval "$hook"; done

# Plan cleanup on unsuccessful installation.
cleanup() {
[[ -L "${COMPAT_VIRTUALENV_PATH}" ]] && rm "${COMPAT_VIRTUALENV_PATH}"
[ -z "${PREFIX_EXISTS}" ] && rm -rf "$VIRTUALENV_PATH"
}

trap cleanup SIGINT
trap cleanup SIGINT ERR

# Invoke virtualenv and record exit status in $STATUS.
STATUS=0
Expand Down
21 changes: 15 additions & 6 deletions bin/pyenv-virtualenv-prefix
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 1 addition & 32 deletions bin/pyenv-virtualenvs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions etc/pyenv.d/which/conda.bash
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions libexec/pyenv-virtualenv-realpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
# Summary: Substitute realpath if unavailable as a builtin or file
# Usage: . pyenv-virtualenv-realpath

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to have "set -e" in all scripts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that.
However, are you aware this is an inline shell file and not a proper script?
It's never run on it's own.
It's always run inline/sourced inside another script already doing set -e.
It's really like a reusable, incomplete section of larger script, like the files under pyenv.d.
Should I rename it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I was overlooking that this script was placed umder libexec dir. It's fine without -e.

if ! {
enable -f "${BASH_SOURCE%/*}"/../libexec/pyenv-realpath.dylib realpath ||
type realpath
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to have this condition? At least this is something new and I'd like to know your intention of this new condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose is to skip defining unneeded substitutes.
realpath is commonly available: coreutils & busybox have it, so it's basically standard in Linux systems.
BSD systems commonly have it, too.
Is there something wrong with the native commands?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to use GNU coreutils feature as long as we have fallback code for where the platform without it. My point was that there was no such condition before you split the code into library condition and would like to know your intention for the change.

} >/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