From 9fe80f28e5ffa6ee3bfe1a2810bb4d2dd14d74d7 Mon Sep 17 00:00:00 2001 From: Ivan Pozdeev Date: Thu, 2 Feb 2023 17:41:53 +0300 Subject: [PATCH 1/3] Add `pyenv versions' option to skip envs for `pyenv-latest' --- libexec/pyenv-versions | 23 +++++++++++++---------- test/versions.bats | 26 +++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/libexec/pyenv-versions b/libexec/pyenv-versions index fc309a03..285d7a7f 100755 --- a/libexec/pyenv-versions +++ b/libexec/pyenv-versions @@ -1,23 +1,24 @@ #!/usr/bin/env bash # Summary: List all Python versions available to pyenv -# Usage: pyenv versions [--bare] [--skip-aliases] +# Usage: pyenv versions [--bare] [--skip-aliases] [--skip-envs] # # Lists all Python versions found in `$PYENV_ROOT/versions/*'. set -e [ -n "$PYENV_DEBUG" ] && set -x -unset bare -unset skip_aliases +unset bare skip_aliases skip_envs # Provide pyenv completions for arg; do case "$arg" in --complete ) echo --bare echo --skip-aliases + echo --skip-envs exit ;; --bare ) bare=1 ;; --skip-aliases ) skip_aliases=1 ;; + --skip-envs ) skip_envs=1 ;; * ) pyenv-help --usage versions >&2 exit 1 @@ -109,7 +110,7 @@ print_version() { if [[ -z "$bare" && -L "$path" ]]; then # Only resolve the link itself for printing, do not resolve further. # Doing otherwise would misinform the user of what the link contains. - version_repr="$version --> $(resolve_link "$version")" + version_repr="$version --> $(resolve_link "$path")" else version_repr="$version" fi @@ -152,12 +153,14 @@ for path in "${versions_dir_entries[@]}"; do [ "${target%/*/envs/*}" == "$versions_dir" ] && continue fi print_version "${path##*/}" "$path" - # virtual environments created by anaconda/miniconda - for env_path in "${path}/envs/"*; do - if [ -d "${env_path}" ]; then - print_version "${env_path#${PYENV_ROOT}/versions/}" "${env_path}" - fi - done + # virtual environments created by anaconda/miniconda/pyenv-virtualenv + if [[ -z $skip_envs ]]; then + for env_path in "${path}/envs/"*; do + if [ -d "${env_path}" ]; then + print_version "${env_path#${PYENV_ROOT}/versions/}" "${env_path}" + fi + done + fi fi done shopt -u dotglob nullglob diff --git a/test/versions.bats b/test/versions.bats index 36db4967..ce101d30 100644 --- a/test/versions.bats +++ b/test/versions.bats @@ -66,18 +66,38 @@ OUT assert_success "3.3" } -@test "multiple versions" { +@test "multiple versions and envs" { stub_system_python create_version "2.7.6" - create_version "3.3.3" create_version "3.4.0" + create_version "3.4.0/envs/foo" + create_version "3.4.0/envs/bar" + create_version "3.5.2" run pyenv-versions assert_success assert_output < Date: Thu, 2 Feb 2023 17:58:10 +0300 Subject: [PATCH 2/3] Streamline `pyenv-versions --bare' Achieves a ~20% speedup. As it's used in pyenv-latest, this affects all version lookups --- libexec/pyenv-versions | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libexec/pyenv-versions b/libexec/pyenv-versions index 285d7a7f..3aaaa055 100755 --- a/libexec/pyenv-versions +++ b/libexec/pyenv-versions @@ -71,8 +71,6 @@ else current_versions=() fi if [ -n "$bare" ]; then - hit_prefix="" - miss_prefix="" include_system="" else hit_prefix="* " @@ -106,8 +104,12 @@ exists() { print_version() { local version="${1:?}" + if [[ -n $bare ]]; then + echo "$version" + return + fi local path="${2:?}" - if [[ -z "$bare" && -L "$path" ]]; then + if [[ -L "$path" ]]; then # Only resolve the link itself for printing, do not resolve further. # Doing otherwise would misinform the user of what the link contains. version_repr="$version --> $(resolve_link "$path")" From 72c5b034cadcc8a13d64c316508a3fbe1ffdf11e Mon Sep 17 00:00:00 2001 From: Ivan Pozdeev Date: Thu, 2 Feb 2023 18:14:42 +0300 Subject: [PATCH 3/3] Avoid the need to filter out envs in `pyenv-latest' --- libexec/pyenv-latest | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libexec/pyenv-latest b/libexec/pyenv-latest index 5c247105..d4c44b60 100755 --- a/libexec/pyenv-latest +++ b/libexec/pyenv-latest @@ -31,7 +31,7 @@ exitcode=0 IFS=$'\n' if [[ -z $FROM_KNOWN ]]; then - DEFINITION_CANDIDATES=( $(pyenv-versions --bare) ) + DEFINITION_CANDIDATES=( $(pyenv-versions --bare --skip-envs) ) else DEFINITION_CANDIDATES=( $(python-build --definitions ) ) fi @@ -48,10 +48,9 @@ IFS=$'\n' $(printf '%s\n' "${DEFINITION_CANDIDATES[@]}" | \ grep -Ee "^$prefix_re[-.]" || true)) - #FIXME: /envs/ should be excluded in Pyenv-Virtualenv via a hook DEFINITION_CANDIDATES=(\ $(printf '%s\n' "${DEFINITION_CANDIDATES[@]}" | \ - sed -E -e '/-dev$/d' -e '/-src$/d' -e '/-latest$/d' -e '/(a|b|rc)[0-9]+$/d' -e '/\/envs\//d')); + sed -E -e '/-dev$/d' -e '/-src$/d' -e '/-latest$/d' -e '/(a|b|rc)[0-9]+$/d')); # Compose a sorting key, followed by | and original value DEFINITION_CANDIDATES=(\