diff --git a/bin/pyenv-virtualenvs b/bin/pyenv-virtualenvs index 23d715d..0b988d1 100755 --- a/bin/pyenv-virtualenvs +++ b/bin/pyenv-virtualenvs @@ -1,9 +1,9 @@ #!/usr/bin/env bash # # Summary: List all Python virtualenvs found in `$PYENV_ROOT/versions/*'. +# Usage: pyenv virtualenvs [--bare] [--skip-aliases] # -# Usage: pyenv virtualenvs [--bare] -# +# List all virtualenvs found in `$PYENV_ROOT/versions/*' and its `$PYENV_ROOT/versions/envs/*'. set -e [ -n "$PYENV_DEBUG" ] && set -x @@ -12,18 +12,80 @@ if [ -z "$PYENV_ROOT" ]; then PYENV_ROOT="${HOME}/.pyenv" fi -if [ "$1" = "--bare" ]; then +unset bare +unset skip_aliases +# Provide pyenv completions +for arg; do + case "$arg" in + --complete ) + echo --bare + echo --skip-aliases + exit ;; + --bare ) bare=1 ;; + --skip-aliases ) skip_aliases=1 ;; + * ) + pyenv-help --usage virtualenvs >&2 + exit 1 + ;; + esac +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 + +if [ -n "$bare" ]; then hit_prefix="" miss_prefix="" current_versions=() unset print_origin + include_system="" else hit_prefix="* " miss_prefix=" " - current_versions=($(IFS=:; for version in $(pyenv-version-name); do echo "$version"; done)) + OLDIFS="$IFS" + IFS=: current_versions=($(pyenv-version-name || true)) + IFS="$OLDIFS" print_origin="1" + include_system="" fi +num_versions=0 + exists() { local car="$1" local cdar @@ -42,21 +104,32 @@ print_version() { else echo "${miss_prefix}${1}${print_origin+$2}" fi + num_versions=$((num_versions + 1)) } 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 "${venv_version}" " (created from ${virtualenv_prefix})" +for path in "$versions_dir"/*; do + if [ -d "$path" ]; then + if [ -n "$skip_aliases" ] && [ -L "$path" ]; then + target="$(realpath "$path")" + [ "${taget%/*/envs/*}" == "$versions_dir" ] || continue fi - done + virtualenv_prefix="$(pyenv-virtualenv-prefix "${path##*/}" 2>/dev/null || true)" + if [ -d "${virtualenv_prefix}" ]; then + print_version "${path##*/}" " (created from ${virtualenv_prefix})" + fi + for venv_path in "${path}/envs/"*; do + venv="${path##*/}/envs/${venv_path##*/}" + virtualenv_prefix="$(pyenv-virtualenv-prefix "${venv}" 2>/dev/null || true)" + if [ -d "${virtualenv_prefix}" ]; then + print_version "${venv}" " (created from ${virtualenv_prefix})" + fi + done + fi done shopt -u nullglob + +if [ "$num_versions" -eq 0 ] && [ -n "$include_system" ]; then + echo "Warning: no Python virtualenv detected on the system" >&2 + exit 1 +fi