Debian Codegraph @

libexec/pyenv-versions

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/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 [scip-shell system . . `[`. -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 echoscip-shell system . . echo. --bare
24 echoscip-shell system . . echo. --skip-aliases
25 echoscip-shell system . . 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 [scip-shell system . . `[`. -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 printfscip-shell system . . printf. '%s\0' "$versions_dir"/*/bin/* "$versions_dir"/*/envs/*/bin/* | xargsscip-shell system . . xargs. -0 -r basename -a | sortscip-shell system . . 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/nullscip-shell filesystem . . `/dev/null`.; then
54 if [scip-shell system . . `[`. -n "$PYENV_NATIVE_EXT" ]; then
55 echoscip-shell system . . echo. "pyenv: failed to load \`realpath' builtin" >&2
56 exit 1
57 fi
58
59 READLINK=$(type -P readlink)
60 if [scip-shell system . . `[`. -z "$READLINK" ]; then
61 echoscip-shell system . . echo. "pyenv: cannot find readlink - are you missing GNU coreutils?" >&2
62 exit 1
63 fi
64
65 resolve_linkscip-shell shell shell-project 0.0.0 resolve_link().() {
66 $READLINK "$1"
67 }
68
69 realpathscip-shell shell shell-project 0.0.0 realpath().() {
70 local path="$1"
71 local name
72
73 # Use a subshell to avoid changing the current path
74 (
75 while [scip-shell system . . `[`. -n "$path" ]; do
76 name="${path##*/}"
77 [scip-shell system . . `[`. "$name" = "$path" ] || cd "${path%/*}"
78 path="$(resolve_linkscip-shell shell shell-project 0.0.0 resolve_link(). "$name" || truescip-shell system . . true.)"
79 done
80
81 echoscip-shell system . . echo. "${PWD}/$name"
82 )
83 }
84 fi
85
86 if [scip-shell system . . `[`. -d "$versions_dir" ]; then
87 versions_dir="$(realpathscip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. -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 || truescip-shell system . . true.); do
104 current_versions["$i"]="1"
105 done
106 else
107 current_versions=($(pyenv-version-name || truescip-shell system . . true.))
108 fi
109 IFS="$OLDIFS"
110 include_system="1"
111 fi
112
113 num_versions=0
114
115 existsscip-shell shell shell-project 0.0.0 exists().() {
116 local car="$1"
117 local cdar
118 shift
119 for cdar in "$@"; do
120 if [scip-shell system . . `[`. "${car}" == "${cdar}" ]; then
121 return 0
122 fi
123 done
124 return 1
125 }
126
127 print_versionscip-shell shell shell-project 0.0.0 print_version().() {
128 local version="${1:?}"
129 if [[ -n $bare ]]; then
130 echoscip-shell system . . 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 --> $(readlinkscip-shell system . . readlink. "$path")"
138 else
139 version_repr="$version"
140 fi
141 if [[ ${BASH_VERSINFO[0]} -ge 4 && ${current_versions["$1"]} ]]; then
142 echoscip-shell system . . echo. "${hit_prefix}${version_repr} (set by $(pyenv-version-origin))"
143 elif (( ${BASH_VERSINFO[0]} <= 3 )) && existsscip-shell shell shell-project 0.0.0 exists(). "$1" "${current_versions[@]}"; then
144 echoscip-shell system . . echo. "${hit_prefix}${version_repr} (set by $(pyenv-version-origin))"
145 else
146 echoscip-shell system . . 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 [scip-shell system . . `[`. -n "$include_system" ] && \
153 (PYENV_VERSION=system pyenv-which python --skip-advice >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || \
154 PYENV_VERSION=system pyenv-which python3 --skip-advice >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || \
155 PYENV_VERSION=system pyenv-which python2 --skip-advice >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1) ; then
156 print_versionscip-shell shell shell-project 0.0.0 print_version(). system "/scip-shell filesystem . . `/`."
157 fi
158
159 shopt -s dotglob nullglob
160 versions_dir_entries=("$versions_dir"/*)
161 if sortscip-shell system . . sort. --version-sort </dev/nullscip-shell filesystem . . `/dev/null`. >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
162 # system sort supports version sorting
163 OLDIFS="$IFS"
164 IFS=$'\n'
165 versions_dir_entries=($(
166 printfscip-shell system . . printf. "%s\n" "${versions_dir_entries[@]}" |
167 sortscip-shell system . . sort. --version-sort
168 ))
169 IFS="$OLDIFS"
170 fi
171
172 for path in "${versions_dir_entries[@]}"; do
173 if [scip-shell system . . `[`. -d "$path" ]; then
174 if [scip-shell system . . `[`. -n "$skip_aliases" ] && [scip-shell system . . `[`. -L "$path" ]; then
175 target="$(realpathscip-shell shell shell-project 0.0.0 realpath(). "$path")"
176 [scip-shell system . . `[`. "${target%/*}" == "$versions_dir" ] && continue
177 [scip-shell system . . `[`. "${target%/*/envs/*}" == "$versions_dir" ] && continue
178 fi
179 print_versionscip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. -d "${env_path}" ]; then
184 print_versionscip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. "$num_versions" -eq 0 ] && [scip-shell system . . `[`. -n "$include_system" ]; then
193 echoscip-shell system . . echo. "Warning: no Python detected on the system" >&2
194 exit 1
195 fi