From 672eef15618072112ae157f1b023cee0b5b01da4 Mon Sep 17 00:00:00 2001 From: Jon Crall Date: Wed, 3 Jul 2024 20:49:12 -0400 Subject: [PATCH] Speed up pyenv prefix by not constructing advice text when it would be discarded (#3005) by adding an internal flag --- libexec/pyenv-prefix | 6 ++--- libexec/pyenv-versions | 6 ++--- libexec/pyenv-which | 54 +++++++++++++++++++++++++++--------------- test/which.bats | 12 ++++++++++ 4 files changed, 53 insertions(+), 25 deletions(-) diff --git a/libexec/pyenv-prefix b/libexec/pyenv-prefix index 38b8f823..261b865f 100755 --- a/libexec/pyenv-prefix +++ b/libexec/pyenv-prefix @@ -30,9 +30,9 @@ OLDIFS="$IFS" { IFS=: for version in ${PYENV_VERSION}; do if [ "$version" = "system" ]; then - if PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python 2>/dev/null)" || \ - PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python3 2>/dev/null)" || \ - PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python2 2>/dev/null)"; then + if PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python --skip-advice 2>/dev/null)" || \ + PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python3 --skip-advice 2>/dev/null)" || \ + PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python2 --skip-advice 2>/dev/null)"; then shopt -s extglob # In some distros (Arch), Python can be found in sbin as well as bin PYENV_PREFIX_PATH="${PYTHON_PATH%/?(s)bin/*}" diff --git a/libexec/pyenv-versions b/libexec/pyenv-versions index 0dd57dec..dbccaa32 100755 --- a/libexec/pyenv-versions +++ b/libexec/pyenv-versions @@ -128,9 +128,9 @@ print_version() { # Include "system" in the non-bare output, if it exists 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 + (PYENV_VERSION=system pyenv-which python --skip-advice >/dev/null 2>&1 || \ + PYENV_VERSION=system pyenv-which python3 --skip-advice >/dev/null 2>&1 || \ + PYENV_VERSION=system pyenv-which python2 --skip-advice >/dev/null 2>&1) ; then print_version system "/" fi diff --git a/libexec/pyenv-which b/libexec/pyenv-which index 29b44318..6f12761e 100755 --- a/libexec/pyenv-which +++ b/libexec/pyenv-which @@ -2,13 +2,14 @@ # # Summary: Display the full path to an executable # -# Usage: pyenv which [--nosystem] +# Usage: pyenv which [--nosystem] [--skip-advice] # # Displays the full path to the executable that pyenv will invoke when # you run the given command. # Use --nosystem argument in case when you don't need to search command in the # system environment. -# +# Internal switch --skip-advice used to skip printing an error message on a +# failed search. set -e [ -n "$PYENV_DEBUG" ] && set -x @@ -18,11 +19,27 @@ if [ "$1" = "--complete" ]; then exec pyenv-shims --short fi -if [ "$2" = "--nosystem" ]; then - system="" -else - system="system" -fi +system="system" +SKIP_ADVICE="" +PYENV_COMMAND="$1" + +while [[ $# -gt 0 ]] +do + case "$1" in + --skip-advice) + SKIP_ADVICE=1 + shift + ;; + --nosystem) + system="" + shift + ;; + *) + shift + ;; + esac +done + remove_from_path() { local path_to_remove="$1" @@ -36,8 +53,6 @@ remove_from_path() { echo "${result#:}" } -PYENV_COMMAND="$1" - if [ -z "$PYENV_COMMAND" ]; then pyenv-help --usage which >&2 exit 1 @@ -85,16 +100,17 @@ else fi echo "pyenv: $PYENV_COMMAND: command not found" >&2 - - versions="$(pyenv-whence "$PYENV_COMMAND" || true)" - if [ -n "$versions" ]; then - { echo - echo "The \`$1' command exists in these Python versions:" - echo "$versions" | sed 's/^/ /g' - echo - echo "Note: See 'pyenv help global' for tips on allowing both" - echo " python2 and python3 to be found." - } >&2 + if [ -z "$SKIP_ADVICE" ]; then + versions="$(pyenv-whence "$PYENV_COMMAND" || true)" + if [ -n "$versions" ]; then + { echo + echo "The \`$PYENV_COMMAND' command exists in these Python versions:" + echo "$versions" | sed 's/^/ /g' + echo + echo "Note: See 'pyenv help global' for tips on allowing both" + echo " python2 and python3 to be found." + } >&2 + fi fi exit 127 diff --git a/test/which.bats b/test/which.bats index 193b95a4..e6c4ee69 100644 --- a/test/which.bats +++ b/test/which.bats @@ -155,3 +155,15 @@ exit PYENV_VERSION=3.4 run pyenv-which python assert_success "version=3.4.2" } + +@test "skip advice supresses error messages" { + create_executable "2.7" "python" + create_executable "3.3" "py.test" + create_executable "3.4" "py.test" + + PYENV_VERSION=2.7 run pyenv-which py.test --skip-advice + assert_failure + assert_output <