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.
This commit is contained in:
John Shimek 2024-06-11 17:32:46 -05:00
parent 613e5142b3
commit 9368bebc35
4 changed files with 71 additions and 76 deletions

View file

@ -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 <<EOS
@ -266,7 +266,7 @@ function pyenv
set -e argv[1]
switch "\$command"
case ${commands[*]}
case activate deactivate rehash shell
source (pyenv "sh-\$command" \$argv|psub)
case '*'
command pyenv "\$command" \$argv
@ -284,20 +284,13 @@ EOS
cat <<EOS
pyenv() {
local command
EOS
;;
esac
if [ "$shell" != "fish" ]; then
IFS="|"
cat <<EOS
command="\${1:-}"
if [ "\$#" -gt 0 ]; then
shift
fi
case "\$command" in
${commands[*]:-/})
activate|deactivate|rehash|shell)
eval "\$(pyenv "sh-\$command" "\$@")"
;;
*)
@ -306,7 +299,8 @@ EOS
esac
}
EOS
fi
;;
esac
}
main

View file

@ -90,13 +90,10 @@ remove_outdated_shims() {
# List basenames of executables for every Python version
list_executable_names() {
local version file
pyenv-versions --bare --skip-aliases | \
while read -r version; do
for file in "${PYENV_ROOT}/versions/${version}/bin/"*; do
local file
for file in "${PYENV_ROOT}/versions/*/bin/"*; do
echo "${file##*/}"
done
done
}
# The basename of each argument passed to `make_shims` will be
@ -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
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

View file

@ -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

View file

@ -1,14 +1,10 @@
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;;"
create_source_prototype_shim() {
if [ -f "${PROTOTYPE_SOURCE_SHIM_PATH}" ]; then
return
fi
done
shopt -u nullglob
eval "source_shim(){ case \"\${1##*/}\" in ${shims[@]} *)return 1;;esac;}"
cat > "${PROTOTYPE_SOURCE_SHIM_PATH}" <<SH
[ -n "\$PYENV_DEBUG" ] && set -x
@ -19,13 +15,17 @@ if [ -e "\${program}" ]; then
fi
SH
chmod +x "${PROTOTYPE_SOURCE_SHIM_PATH}"
}
shopt -s nullglob
for shim in "${SHIM_PATH}/"*; do
if source_shim "${shim}"; then
cp "${PROTOTYPE_SOURCE_SHIM_PATH}" "${shim}"
for shim in $(cat "${BASH_SOURCE%/*}/source.d/"*".list" | sort -u | sed -e 's/#.*$//' | sed -e '/^[[:space:]]*$/d'); do
if [ -n "${shim##*/}" ]; then
source_shim="${SHIM_PATH}/${shim}"
if [ -e "${SOURCE_SHIM}" ]; then
create_source_prototype_shim
cp "${PROTOTYPE_SOURCE_SHIM_PATH}" "${source_shim}"
fi
fi
done
shopt -u nullglob
rm -f "${PROTOTYPE_SOURCE_SHIM_PATH}"