Speed up pyenv prefix by not constructing advice text when it would be discarded (#3005)

by adding an internal flag
This commit is contained in:
Jon Crall 2024-07-03 20:49:12 -04:00 committed by GitHub
parent 384361167a
commit 672eef1561
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 53 additions and 25 deletions

View file

@ -30,9 +30,9 @@ OLDIFS="$IFS"
{ IFS=: { IFS=:
for version in ${PYENV_VERSION}; do for version in ${PYENV_VERSION}; do
if [ "$version" = "system" ]; then if [ "$version" = "system" ]; then
if PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python 2>/dev/null)" || \ if PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python --skip-advice 2>/dev/null)" || \
PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python3 2>/dev/null)" || \ PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python3 --skip-advice 2>/dev/null)" || \
PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python2 2>/dev/null)"; then PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python2 --skip-advice 2>/dev/null)"; then
shopt -s extglob shopt -s extglob
# In some distros (Arch), Python can be found in sbin as well as bin # In some distros (Arch), Python can be found in sbin as well as bin
PYENV_PREFIX_PATH="${PYTHON_PATH%/?(s)bin/*}" PYENV_PREFIX_PATH="${PYTHON_PATH%/?(s)bin/*}"

View file

@ -128,9 +128,9 @@ print_version() {
# Include "system" in the non-bare output, if it exists # Include "system" in the non-bare output, if it exists
if [ -n "$include_system" ] && \ if [ -n "$include_system" ] && \
(PYENV_VERSION=system pyenv-which python >/dev/null 2>&1 || \ (PYENV_VERSION=system pyenv-which python --skip-advice >/dev/null 2>&1 || \
PYENV_VERSION=system pyenv-which python3 >/dev/null 2>&1 || \ PYENV_VERSION=system pyenv-which python3 --skip-advice >/dev/null 2>&1 || \
PYENV_VERSION=system pyenv-which python2 >/dev/null 2>&1) ; then PYENV_VERSION=system pyenv-which python2 --skip-advice >/dev/null 2>&1) ; then
print_version system "/" print_version system "/"
fi fi

View file

@ -2,13 +2,14 @@
# #
# Summary: Display the full path to an executable # Summary: Display the full path to an executable
# #
# Usage: pyenv which <command> [--nosystem] # Usage: pyenv which <command> [--nosystem] [--skip-advice]
# #
# Displays the full path to the executable that pyenv will invoke when # Displays the full path to the executable that pyenv will invoke when
# you run the given command. # you run the given command.
# Use --nosystem argument in case when you don't need to search command in the # Use --nosystem argument in case when you don't need to search command in the
# system environment. # system environment.
# # Internal switch --skip-advice used to skip printing an error message on a
# failed search.
set -e set -e
[ -n "$PYENV_DEBUG" ] && set -x [ -n "$PYENV_DEBUG" ] && set -x
@ -18,11 +19,27 @@ if [ "$1" = "--complete" ]; then
exec pyenv-shims --short exec pyenv-shims --short
fi fi
if [ "$2" = "--nosystem" ]; then system="system"
SKIP_ADVICE=""
PYENV_COMMAND="$1"
while [[ $# -gt 0 ]]
do
case "$1" in
--skip-advice)
SKIP_ADVICE=1
shift
;;
--nosystem)
system="" system=""
else shift
system="system" ;;
fi *)
shift
;;
esac
done
remove_from_path() { remove_from_path() {
local path_to_remove="$1" local path_to_remove="$1"
@ -36,8 +53,6 @@ remove_from_path() {
echo "${result#:}" echo "${result#:}"
} }
PYENV_COMMAND="$1"
if [ -z "$PYENV_COMMAND" ]; then if [ -z "$PYENV_COMMAND" ]; then
pyenv-help --usage which >&2 pyenv-help --usage which >&2
exit 1 exit 1
@ -85,17 +100,18 @@ else
fi fi
echo "pyenv: $PYENV_COMMAND: command not found" >&2 echo "pyenv: $PYENV_COMMAND: command not found" >&2
if [ -z "$SKIP_ADVICE" ]; then
versions="$(pyenv-whence "$PYENV_COMMAND" || true)" versions="$(pyenv-whence "$PYENV_COMMAND" || true)"
if [ -n "$versions" ]; then if [ -n "$versions" ]; then
{ echo { echo
echo "The \`$1' command exists in these Python versions:" echo "The \`$PYENV_COMMAND' command exists in these Python versions:"
echo "$versions" | sed 's/^/ /g' echo "$versions" | sed 's/^/ /g'
echo echo
echo "Note: See 'pyenv help global' for tips on allowing both" echo "Note: See 'pyenv help global' for tips on allowing both"
echo " python2 and python3 to be found." echo " python2 and python3 to be found."
} >&2 } >&2
fi fi
fi
exit 127 exit 127
fi fi

View file

@ -155,3 +155,15 @@ exit
PYENV_VERSION=3.4 run pyenv-which python PYENV_VERSION=3.4 run pyenv-which python
assert_success "version=3.4.2" 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 <<OUT
pyenv: py.test: command not found
OUT
}