From 9368bebc35f02d7acd77ebc27d2a978a2eef074c Mon Sep 17 00:00:00 2001 From: John Shimek Date: Tue, 11 Jun 2024 17:32:46 -0500 Subject: [PATCH] Lots of optimizations to improving `pyenv init -`. This is only meant to be an example. There's a lot of simplification that works because my usage of Pyenv is simple. --- libexec/pyenv-init | 40 +++++++++++++++------------------- libexec/pyenv-rehash | 33 ++++++++++++++-------------- pyenv.d/rehash/conda.bash | 44 +++++++++++++++++++------------------- pyenv.d/rehash/source.bash | 30 +++++++++++++------------- 4 files changed, 71 insertions(+), 76 deletions(-) diff --git a/libexec/pyenv-init b/libexec/pyenv-init index 492360e6..c45e5b5b 100755 --- a/libexec/pyenv-init +++ b/libexec/pyenv-init @@ -27,24 +27,16 @@ do if [ "$args" = "-" ]; then mode="print" shift - fi - - if [ "$args" = "--path" ]; then + elif [ "$args" = "--path" ]; then mode="path" shift - fi - - if [ "$args" = "--detect-shell" ]; then + elif [ "$args" = "--detect-shell" ]; then mode="detect-shell" shift - fi - - if [ "$args" = "--no-push-path" ]; then + elif [ "$args" = "--no-push-path" ]; then no_push_path=1 shift - fi - - if [ "$args" = "--no-rehash" ]; then + elif [ "$args" = "--no-rehash" ]; then no_rehash=1 shift fi @@ -202,6 +194,11 @@ function print_path() { echo 'set -eg PATH[$pyenv_index]; end; set -e pyenv_index' print_path_prepend_shims ;; + zsh ) + echo 'stale_path=($PYENV_ROOT/shims)' + echo 'path=(${path:|stale_path})' + print_path_prepend_shims + ;; * ) # Some distros (notably Debian-based) set Bash's SSH_SOURCE_BASHRC compilation option # that makes it source `bashrc` under SSH even when not interactive. @@ -226,6 +223,10 @@ function print_path_prepend_shims() { fish ) echo 'set -gx PATH '\'"${PYENV_ROOT}/shims"\'' $PATH' ;; + zsh ) + echo 'path=($PYENV_ROOT/shims $path)' + echo 'export PATH' + ;; * ) echo 'export PATH="'"${PYENV_ROOT}"'/shims:${PATH}"' ;; @@ -257,7 +258,6 @@ function print_rehash() { } function print_shell_function() { - commands=(`pyenv-commands --sh`) case "$shell" in fish ) cat < 3)); then fi done } - + else # Same for bash < 4. registered_shims=" " @@ -165,6 +162,7 @@ else # Same for bash < 4. fi done } + fi shopt -s nullglob @@ -174,17 +172,20 @@ shopt -s nullglob create_prototype_shim remove_outdated_shims # shellcheck disable=SC2046 -make_shims $(list_executable_names | sort -u) +make_shims $(list_executable_names) -# Allow plugins to register shims. -OLDIFS="$IFS" -IFS=$'\n' scripts=(`pyenv-hooks rehash`) -IFS="$OLDIFS" - -for script in "${scripts[@]}"; do - source "$script" -done +get_hooks() { + shopt -s nullglob + IFS=: hook_paths=($PYENV_HOOK_PATH) + for path in "${hook_paths[@]}"; do + for script in "$path/$PYENV_COMMAND"/*.bash; do + source "$script" + done + done + shopt -u nullglob +} +get_hooks install_registered_shims remove_stale_shims diff --git a/pyenv.d/rehash/conda.bash b/pyenv.d/rehash/conda.bash index 884cd4f2..44d7ca4b 100644 --- a/pyenv.d/rehash/conda.bash +++ b/pyenv.d/rehash/conda.bash @@ -11,28 +11,6 @@ conda_exists() { [ -n "${condas}" ] } -shims=() -shopt -s nullglob -for shim in $(cat "${BASH_SOURCE%/*}/conda.d/"*".list" | sort | uniq | sed -e 's/#.*$//' | sed -e '/^[[:space:]]*$/d'); do - if [ -n "${shim##*/}" ]; then - shims[${#shims[*]}]="${shim})return 0;;" - fi -done -shopt -u nullglob -eval "conda_shim(){ case \"\${1##*/}\" in ${shims[@]} *)return 1;;esac;}" - -# override `make_shims` to avoid conflict between pyenv-virtualenv's `envs.bash` -# https://github.com/pyenv/pyenv-virtualenv/blob/v20160716/etc/pyenv.d/rehash/envs.bash -make_shims() { - local file shim - for file do - shim="${file##*/}" - if ! conda_shim "${shim}" 1>&2; then - register_shim "$shim" - fi - done -} - deregister_conda_shims() { # adapted for Bash 4.x's associative array (#1749) if declare -p registered_shims 2> /dev/null | grep -Eq '^(declare|typeset) -A'; then @@ -54,5 +32,27 @@ deregister_conda_shims() { } if conda_exists; then + shims=() + shopt -s nullglob + for shim in $(cat "${BASH_SOURCE%/*}/conda.d/"*".list" | sort -u | sed -e 's/#.*$//' -e '/^[[:space:]]*$/d'); do + if [ -n "${shim##*/}" ]; then + shims[${#shims[*]}]="${shim})return 0;;" + fi + done + shopt -u nullglob + eval "conda_shim(){ case \"\${1##*/}\" in ${shims[@]} *)return 1;;esac;}" + + # override `make_shims` to avoid conflict between pyenv-virtualenv's `envs.bash` + # https://github.com/pyenv/pyenv-virtualenv/blob/v20160716/etc/pyenv.d/rehash/envs.bash + make_shims() { + local file shim + for file do + shim="${file##*/}" + if ! conda_shim "${shim}" 1>&2; then + register_shim "$shim" + fi + done + } + deregister_conda_shims fi diff --git a/pyenv.d/rehash/source.bash b/pyenv.d/rehash/source.bash index 6bcbcbd7..0f0f3b42 100644 --- a/pyenv.d/rehash/source.bash +++ b/pyenv.d/rehash/source.bash @@ -1,16 +1,12 @@ PROTOTYPE_SOURCE_SHIM_PATH="${SHIM_PATH}/.pyenv-source-shim" -shims=() -shopt -s nullglob -for shim in $(cat "${BASH_SOURCE%/*}/source.d/"*".list" | sort | uniq | sed -e 's/#.*$//' | sed -e '/^[[:space:]]*$/d'); do - if [ -n "${shim##*/}" ]; then - shims[${#shims[*]}]="${shim})return 0;;" - fi -done -shopt -u nullglob -eval "source_shim(){ case \"\${1##*/}\" in ${shims[@]} *)return 1;;esac;}" -cat > "${PROTOTYPE_SOURCE_SHIM_PATH}" < "${PROTOTYPE_SOURCE_SHIM_PATH}" <