2019-04-23 10:23:33 -04:00
|
|
|
#!/usr/bin/env bash
|
2013-01-18 03:41:41 -05:00
|
|
|
# Summary: List all Python versions available to pyenv
|
2023-02-02 09:41:53 -05:00
|
|
|
# Usage: pyenv versions [--bare] [--skip-aliases] [--skip-envs]
|
2013-01-18 03:41:41 -05:00
|
|
|
#
|
|
|
|
# Lists all Python versions found in `$PYENV_ROOT/versions/*'.
|
|
|
|
|
2012-08-31 02:23:41 -04:00
|
|
|
set -e
|
|
|
|
[ -n "$PYENV_DEBUG" ] && set -x
|
|
|
|
|
2023-02-02 09:41:53 -05:00
|
|
|
unset bare skip_aliases skip_envs
|
2015-11-20 23:13:52 -05:00
|
|
|
# Provide pyenv completions
|
2015-10-27 05:49:29 -04:00
|
|
|
for arg; do
|
|
|
|
case "$arg" in
|
2015-11-16 11:54:22 -05:00
|
|
|
--complete )
|
|
|
|
echo --bare
|
|
|
|
echo --skip-aliases
|
2023-02-02 09:41:53 -05:00
|
|
|
echo --skip-envs
|
2015-11-16 11:54:22 -05:00
|
|
|
exit ;;
|
2015-10-27 05:49:29 -04:00
|
|
|
--bare ) bare=1 ;;
|
|
|
|
--skip-aliases ) skip_aliases=1 ;;
|
2023-02-02 09:41:53 -05:00
|
|
|
--skip-envs ) skip_envs=1 ;;
|
2015-10-27 05:49:29 -04:00
|
|
|
* )
|
2015-11-20 23:13:52 -05:00
|
|
|
pyenv-help --usage versions >&2
|
2015-10-27 05:49:29 -04:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2015-11-20 23:13:52 -05:00
|
|
|
versions_dir="${PYENV_ROOT}/versions"
|
2015-10-27 05:49:29 -04:00
|
|
|
|
2016-03-01 20:39:52 -05:00
|
|
|
if ! enable -f "${BASH_SOURCE%/*}"/pyenv-realpath.dylib realpath 2>/dev/null; then
|
2015-11-20 23:13:52 -05:00
|
|
|
if [ -n "$PYENV_NATIVE_EXT" ]; then
|
|
|
|
echo "pyenv: failed to load \`realpath' builtin" >&2
|
2015-10-27 05:49:29 -04:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-03-28 04:25:27 -04:00
|
|
|
READLINK=$(type -P readlink)
|
2015-10-27 05:49:29 -04:00
|
|
|
if [ -z "$READLINK" ]; then
|
2015-11-20 23:13:52 -05:00
|
|
|
echo "pyenv: cannot find readlink - are you missing GNU coreutils?" >&2
|
2015-10-27 05:49:29 -04:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
resolve_link() {
|
|
|
|
$READLINK "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
realpath() {
|
|
|
|
local path="$1"
|
|
|
|
local name
|
|
|
|
|
2017-11-13 16:12:13 -05:00
|
|
|
# Use a subshell to avoid changing the current path
|
|
|
|
(
|
2015-10-27 05:49:29 -04:00
|
|
|
while [ -n "$path" ]; do
|
|
|
|
name="${path##*/}"
|
|
|
|
[ "$name" = "$path" ] || cd "${path%/*}"
|
|
|
|
path="$(resolve_link "$name" || true)"
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "${PWD}/$name"
|
2017-11-13 16:12:13 -05:00
|
|
|
)
|
2015-10-27 05:49:29 -04:00
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d "$versions_dir" ]; then
|
|
|
|
versions_dir="$(realpath "$versions_dir")"
|
|
|
|
fi
|
|
|
|
|
2020-12-05 08:09:46 -05:00
|
|
|
if ((${BASH_VERSINFO[0]} > 3)); then
|
|
|
|
declare -A current_versions
|
|
|
|
else
|
|
|
|
current_versions=()
|
|
|
|
fi
|
2015-10-27 05:49:29 -04:00
|
|
|
if [ -n "$bare" ]; then
|
2013-01-18 04:57:08 -05:00
|
|
|
include_system=""
|
|
|
|
else
|
|
|
|
hit_prefix="* "
|
|
|
|
miss_prefix=" "
|
2013-08-15 09:29:14 -04:00
|
|
|
OLDIFS="$IFS"
|
2020-12-05 08:09:46 -05:00
|
|
|
IFS=:
|
|
|
|
if ((${BASH_VERSINFO[0]} > 3)); then
|
|
|
|
for i in $(pyenv-version-name || true); do
|
|
|
|
current_versions["$i"]="1"
|
|
|
|
done
|
|
|
|
else
|
|
|
|
current_versions=($(pyenv-version-name || true))
|
|
|
|
fi
|
2013-08-15 09:29:14 -04:00
|
|
|
IFS="$OLDIFS"
|
2013-01-18 04:57:08 -05:00
|
|
|
include_system="1"
|
|
|
|
fi
|
|
|
|
|
2014-10-14 11:46:11 -04:00
|
|
|
num_versions=0
|
|
|
|
|
|
|
|
exists() {
|
|
|
|
local car="$1"
|
|
|
|
local cdar
|
2012-09-07 06:56:55 -04:00
|
|
|
shift
|
2014-10-14 11:46:11 -04:00
|
|
|
for cdar in "$@"; do
|
|
|
|
if [ "${car}" == "${cdar}" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
2012-09-07 06:56:55 -04:00
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2013-01-18 04:57:08 -05:00
|
|
|
print_version() {
|
2023-02-02 09:24:19 -05:00
|
|
|
local version="${1:?}"
|
2023-02-02 09:58:10 -05:00
|
|
|
if [[ -n $bare ]]; then
|
|
|
|
echo "$version"
|
|
|
|
return
|
|
|
|
fi
|
2023-02-02 09:24:19 -05:00
|
|
|
local path="${2:?}"
|
2023-02-02 09:58:10 -05:00
|
|
|
if [[ -L "$path" ]]; then
|
2023-02-02 09:24:19 -05:00
|
|
|
# Only resolve the link itself for printing, do not resolve further.
|
|
|
|
# Doing otherwise would misinform the user of what the link contains.
|
2023-02-06 01:09:26 -05:00
|
|
|
version_repr="$version --> $(readlink "$path")"
|
2023-02-02 09:24:19 -05:00
|
|
|
else
|
|
|
|
version_repr="$version"
|
|
|
|
fi
|
2020-12-05 08:09:46 -05:00
|
|
|
if [[ ${BASH_VERSINFO[0]} -ge 4 && ${current_versions["$1"]} ]]; then
|
2023-02-02 09:24:19 -05:00
|
|
|
echo "${hit_prefix}${version_repr} (set by $(pyenv-version-origin))"
|
2021-02-14 07:50:36 -05:00
|
|
|
elif (( ${BASH_VERSINFO[0]} <= 3 )) && exists "$1" "${current_versions[@]}"; then
|
2023-02-02 09:24:19 -05:00
|
|
|
echo "${hit_prefix}${version_repr} (set by $(pyenv-version-origin))"
|
2013-01-18 04:57:08 -05:00
|
|
|
else
|
2023-04-07 18:12:09 -04:00
|
|
|
echo "${miss_prefix}${version_repr}"
|
2013-01-18 04:57:08 -05:00
|
|
|
fi
|
2014-10-14 11:46:11 -04:00
|
|
|
num_versions=$((num_versions + 1))
|
2013-01-18 04:57:08 -05:00
|
|
|
}
|
2012-08-31 02:23:41 -04:00
|
|
|
|
2013-01-18 04:57:08 -05:00
|
|
|
# Include "system" in the non-bare output, if it exists
|
2020-09-21 11:53:09 -04:00
|
|
|
if [ -n "$include_system" ] && \
|
|
|
|
(PYENV_VERSION=system pyenv-which python >/dev/null 2>&1 || \
|
|
|
|
PYENV_VERSION=system pyenv-which python3 >/dev/null 2>&1 || \
|
|
|
|
PYENV_VERSION=system pyenv-which python2 >/dev/null 2>&1) ; then
|
2023-02-02 09:24:19 -05:00
|
|
|
print_version system "/"
|
2012-08-31 02:23:41 -04:00
|
|
|
fi
|
|
|
|
|
2019-06-02 16:00:36 -04:00
|
|
|
shopt -s dotglob nullglob
|
2022-07-10 16:00:51 -04:00
|
|
|
versions_dir_entries=("$versions_dir"/*)
|
|
|
|
if sort --version-sort </dev/null >/dev/null 2>&1; then
|
|
|
|
# system sort supports version sorting
|
|
|
|
OLDIFS="$IFS"
|
|
|
|
IFS=$'\n'
|
|
|
|
versions_dir_entries=($(
|
|
|
|
printf "%s\n" "${versions_dir_entries[@]}" |
|
|
|
|
sort --version-sort
|
|
|
|
))
|
|
|
|
IFS="$OLDIFS"
|
|
|
|
fi
|
|
|
|
|
|
|
|
for path in "${versions_dir_entries[@]}"; do
|
2012-08-31 02:23:41 -04:00
|
|
|
if [ -d "$path" ]; then
|
2015-10-27 05:49:29 -04:00
|
|
|
if [ -n "$skip_aliases" ] && [ -L "$path" ]; then
|
|
|
|
target="$(realpath "$path")"
|
2023-02-02 09:24:19 -05:00
|
|
|
[ "${target%/*}" == "$versions_dir" ] && continue
|
|
|
|
[ "${target%/*/envs/*}" == "$versions_dir" ] && continue
|
2015-10-27 05:49:29 -04:00
|
|
|
fi
|
2023-02-02 09:24:19 -05:00
|
|
|
print_version "${path##*/}" "$path"
|
2023-02-02 09:41:53 -05:00
|
|
|
# virtual environments created by anaconda/miniconda/pyenv-virtualenv
|
|
|
|
if [[ -z $skip_envs ]]; then
|
2023-02-15 16:10:36 -05:00
|
|
|
for env_path in "${path}/envs/"*; do
|
|
|
|
if [ -d "${env_path}" ]; then
|
|
|
|
print_version "${env_path#${PYENV_ROOT}/versions/}" "${env_path}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2012-08-31 02:23:41 -04:00
|
|
|
fi
|
|
|
|
done
|
2019-06-02 16:00:36 -04:00
|
|
|
shopt -u dotglob nullglob
|
2014-10-14 11:46:11 -04:00
|
|
|
|
|
|
|
if [ "$num_versions" -eq 0 ] && [ -n "$include_system" ]; then
|
|
|
|
echo "Warning: no Python detected on the system" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|