| 1 | #!/usr/bin/env bash |
| 2 | # Summary: List all Python versions available to pyenv |
| 3 | # Usage: pyenv versions [--bare] [--skip-aliases] [--skip-envs] [--executables] |
| 4 | # |
| 5 | # Lists all Python versions found in `$PYENV_ROOT/versions/*'. |
| 6 | # |
| 7 | # --bare List just the names, omit `system' |
| 8 | # --skip-aliases Skip symlinks to other versions and to virtual environments |
| 9 | # --skip-envs Skip virtual environments (under <version>/envs) |
| 10 | # --executables Internal. Overrides other options. |
| 11 | # Optimally get a deduplicated list of all executable names in Pyenv-managed |
| 12 | # versions and environments for `pyenv rehash' |
| 13 | # |
| 14 | |
| 15 | set -e |
| 16 | [ -n "$PYENV_DEBUG" ] && set -x |
| 17 | |
| 18 | unset bare skip_aliases skip_envs executables |
| 19 | # Provide pyenv completions |
| 20 | for arg; do |
| 21 | case "$arg" in |
| 22 | --complete ) |
| 23 | echo --bare |
| 24 | echo --skip-aliases |
| 25 | echo --skip-envs |
| 26 | exit ;; |
| 27 | --executables ) executables=1; break ;; |
| 28 | --bare ) bare=1 ;; |
| 29 | --skip-aliases ) skip_aliases=1 ;; |
| 30 | --skip-envs ) skip_envs=1 ;; |
| 31 | * ) |
| 32 | pyenv-help --usage versions >&2 |
| 33 | exit 1 |
| 34 | ;; |
| 35 | esac |
| 36 | done |
| 37 | |
| 38 | |
| 39 | versions_dir="${PYENV_ROOT}/versions" |
| 40 | |
| 41 | # Fast path for rehash: skip filtering and link resolution |
| 42 | if [[ -n "$executables" ]]; then |
| 43 | if [ -d "$versions_dir" ]; then |
| 44 | shopt -s dotglob nullglob |
| 45 | # MacOS 12+ and FreeBSD 15 support `xargs -r -0' and `basename -a' |
| 46 | # `sort -u` is simpler and a bit faster than `awk '!seen[$0]++'`, with the same result for rehash purposes |
| 47 | printf '%s\0' "$versions_dir"/*/bin/* "$versions_dir"/*/envs/*/bin/* | xargs -0 -r basename -a | sort -u |
| 48 | shopt -u dotglob nullglob |
| 49 | fi |
| 50 | exit 0 |
| 51 | fi |
| 52 | |
| 53 | if ! enable -f "${BASH_SOURCE%/*}"/pyenv-realpath.dylib realpath 2>/dev/null; then |
| 54 | if [ -n "$PYENV_NATIVE_EXT" ]; then |
| 55 | echo "pyenv: failed to load \`realpath' builtin" >&2 |
| 56 | exit 1 |
| 57 | fi |
| 58 | |
| 59 | READLINK=$(type -P readlink) |
| 60 | if [ -z "$READLINK" ]; then |
| 61 | echo "pyenv: cannot find readlink - are you missing GNU coreutils?" >&2 |
| 62 | exit 1 |
| 63 | fi |
| 64 | |
| 65 | resolve_link() { |
| 66 | $READLINK "$1" |
| 67 | } |
| 68 | |
| 69 | realpath() { |
| 70 | local path="$1" |
| 71 | local name |
| 72 | |
| 73 | # Use a subshell to avoid changing the current path |
| 74 | ( |
| 75 | while [ -n "$path" ]; do |
| 76 | name="${path##*/}" |
| 77 | [ "$name" = "$path" ] || cd "${path%/*}" |
| 78 | path="$(resolve_link "$name" || true)" |
| 79 | done |
| 80 | |
| 81 | echo "${PWD}/$name" |
| 82 | ) |
| 83 | } |
| 84 | fi |
| 85 | |
| 86 | if [ -d "$versions_dir" ]; then |
| 87 | versions_dir="$(realpath "$versions_dir")" |
| 88 | fi |
| 89 | |
| 90 | if ((${BASH_VERSINFO[0]} > 3)); then |
| 91 | declare -A current_versions |
| 92 | else |
| 93 | current_versions=() |
| 94 | fi |
| 95 | if [ -n "$bare" ]; then |
| 96 | include_system="" |
| 97 | else |
| 98 | hit_prefix="* " |
| 99 | miss_prefix=" " |
| 100 | OLDIFS="$IFS" |
| 101 | IFS=: |
| 102 | if ((${BASH_VERSINFO[0]} > 3)); then |
| 103 | for i in $(pyenv-version-name || true); do |
| 104 | current_versions["$i"]="1" |
| 105 | done |
| 106 | else |
| 107 | current_versions=($(pyenv-version-name || true)) |
| 108 | fi |
| 109 | IFS="$OLDIFS" |
| 110 | include_system="1" |
| 111 | fi |
| 112 | |
| 113 | num_versions=0 |
| 114 | |
| 115 | exists() { |
| 116 | local car="$1" |
| 117 | local cdar |
| 118 | shift |
| 119 | for cdar in "$@"; do |
| 120 | if [ "${car}" == "${cdar}" ]; then |
| 121 | return 0 |
| 122 | fi |
| 123 | done |
| 124 | return 1 |
| 125 | } |
| 126 | |
| 127 | print_version() { |
| 128 | local version="${1:?}" |
| 129 | if [[ -n $bare ]]; then |
| 130 | echo "$version" |
| 131 | return |
| 132 | fi |
| 133 | local path="${2:?}" |
| 134 | if [[ -L "$path" ]]; then |
| 135 | # Only resolve the link itself for printing, do not resolve further. |
| 136 | # Doing otherwise would misinform the user of what the link contains. |
| 137 | version_repr="$version --> $(readlink "$path")" |
| 138 | else |
| 139 | version_repr="$version" |
| 140 | fi |
| 141 | if [[ ${BASH_VERSINFO[0]} -ge 4 && ${current_versions["$1"]} ]]; then |
| 142 | echo "${hit_prefix}${version_repr} (set by $(pyenv-version-origin))" |
| 143 | elif (( ${BASH_VERSINFO[0]} <= 3 )) && exists "$1" "${current_versions[@]}"; then |
| 144 | echo "${hit_prefix}${version_repr} (set by $(pyenv-version-origin))" |
| 145 | else |
| 146 | echo "${miss_prefix}${version_repr}" |
| 147 | fi |
| 148 | num_versions=$((num_versions + 1)) |
| 149 | } |
| 150 | |
| 151 | # Include "system" in the non-bare output, if it exists |
| 152 | if [ -n "$include_system" ] && \ |
| 153 | (PYENV_VERSION=system pyenv-which python --skip-advice >/dev/null 2>&1 || \ |
| 154 | PYENV_VERSION=system pyenv-which python3 --skip-advice >/dev/null 2>&1 || \ |
| 155 | PYENV_VERSION=system pyenv-which python2 --skip-advice >/dev/null 2>&1) ; then |
| 156 | print_version system "/" |
| 157 | fi |
| 158 | |
| 159 | shopt -s dotglob nullglob |
| 160 | versions_dir_entries=("$versions_dir"/*) |
| 161 | if sort --version-sort </dev/null >/dev/null 2>&1; then |
| 162 | # system sort supports version sorting |
| 163 | OLDIFS="$IFS" |
| 164 | IFS=$'\n' |
| 165 | versions_dir_entries=($( |
| 166 | printf "%s\n" "${versions_dir_entries[@]}" | |
| 167 | sort --version-sort |
| 168 | )) |
| 169 | IFS="$OLDIFS" |
| 170 | fi |
| 171 | |
| 172 | for path in "${versions_dir_entries[@]}"; do |
| 173 | if [ -d "$path" ]; then |
| 174 | if [ -n "$skip_aliases" ] && [ -L "$path" ]; then |
| 175 | target="$(realpath "$path")" |
| 176 | [ "${target%/*}" == "$versions_dir" ] && continue |
| 177 | [ "${target%/*/envs/*}" == "$versions_dir" ] && continue |
| 178 | fi |
| 179 | print_version "${path##*/}" "$path" |
| 180 | # virtual environments created by anaconda/miniconda/pyenv-virtualenv |
| 181 | if [[ -z $skip_envs ]]; then |
| 182 | for env_path in "${path}/envs/"*; do |
| 183 | if [ -d "${env_path}" ]; then |
| 184 | print_version "${env_path#${PYENV_ROOT}/versions/}" "${env_path}" |
| 185 | fi |
| 186 | done |
| 187 | fi |
| 188 | fi |
| 189 | done |
| 190 | shopt -u dotglob nullglob |
| 191 | |
| 192 | if [ "$num_versions" -eq 0 ] && [ -n "$include_system" ]; then |
| 193 | echo "Warning: no Python detected on the system" >&2 |
| 194 | exit 1 |
| 195 | fi |