瀏覽代碼

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.
pull/3037/head
John Shimek 3 月之前
父節點
當前提交
9368bebc35
共有 4 個文件被更改,包括 70 次插入75 次删除
  1. +17
    -23
      libexec/pyenv-init
  2. +17
    -16
      libexec/pyenv-rehash
  3. +22
    -22
      pyenv.d/rehash/conda.bash
  4. +14
    -14
      pyenv.d/rehash/source.bash

+ 17
- 23
libexec/pyenv-init 查看文件

@ -27,24 +27,16 @@ do
if [ "$args" = "-" ]; then if [ "$args" = "-" ]; then
mode="print" mode="print"
shift shift
fi
if [ "$args" = "--path" ]; then
elif [ "$args" = "--path" ]; then
mode="path" mode="path"
shift shift
fi
if [ "$args" = "--detect-shell" ]; then
elif [ "$args" = "--detect-shell" ]; then
mode="detect-shell" mode="detect-shell"
shift shift
fi
if [ "$args" = "--no-push-path" ]; then
elif [ "$args" = "--no-push-path" ]; then
no_push_path=1 no_push_path=1
shift shift
fi
if [ "$args" = "--no-rehash" ]; then
elif [ "$args" = "--no-rehash" ]; then
no_rehash=1 no_rehash=1
shift shift
fi fi
@ -202,6 +194,11 @@ function print_path() {
echo 'set -eg PATH[$pyenv_index]; end; set -e pyenv_index' echo 'set -eg PATH[$pyenv_index]; end; set -e pyenv_index'
print_path_prepend_shims 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 # Some distros (notably Debian-based) set Bash's SSH_SOURCE_BASHRC compilation option
# that makes it source `bashrc` under SSH even when not interactive. # that makes it source `bashrc` under SSH even when not interactive.
@ -226,6 +223,10 @@ function print_path_prepend_shims() {
fish ) fish )
echo 'set -gx PATH '\'"${PYENV_ROOT}/shims"\'' $PATH' 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}"' echo 'export PATH="'"${PYENV_ROOT}"'/shims:${PATH}"'
;; ;;
@ -257,7 +258,6 @@ function print_rehash() {
} }
function print_shell_function() { function print_shell_function() {
commands=(`pyenv-commands --sh`)
case "$shell" in case "$shell" in
fish ) fish )
cat <<EOS cat <<EOS
@ -266,7 +266,7 @@ function pyenv
set -e argv[1] set -e argv[1]
switch "\$command" switch "\$command"
case ${commands[*]}
case activate deactivate rehash shell
source (pyenv "sh-\$command" \$argv|psub) source (pyenv "sh-\$command" \$argv|psub)
case '*' case '*'
command pyenv "\$command" \$argv command pyenv "\$command" \$argv
@ -284,20 +284,13 @@ EOS
cat <<EOS cat <<EOS
pyenv() { pyenv() {
local command local command
EOS
;;
esac
if [ "$shell" != "fish" ]; then
IFS="|"
cat <<EOS
command="\${1:-}" command="\${1:-}"
if [ "\$#" -gt 0 ]; then if [ "\$#" -gt 0 ]; then
shift shift
fi fi
case "\$command" in case "\$command" in
${commands[*]:-/})
activate|deactivate|rehash|shell)
eval "\$(pyenv "sh-\$command" "\$@")" eval "\$(pyenv "sh-\$command" "\$@")"
;; ;;
*) *)
@ -306,7 +299,8 @@ EOS
esac esac
} }
EOS EOS
fi
;;
esac
} }
main main

+ 17
- 16
libexec/pyenv-rehash 查看文件

@ -90,12 +90,9 @@ remove_outdated_shims() {
# List basenames of executables for every Python version # List basenames of executables for every Python version
list_executable_names() { 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
echo "${file##*/}"
done
local file
for file in "${PYENV_ROOT}/versions/*/bin/"*; do
echo "${file##*/}"
done done
} }
@ -140,7 +137,7 @@ if ((${BASH_VERSINFO[0]} > 3)); then
fi fi
done done
} }
else # Same for bash < 4. else # Same for bash < 4.
registered_shims=" " registered_shims=" "
@ -165,6 +162,7 @@ else # Same for bash < 4.
fi fi
done done
} }
fi fi
shopt -s nullglob shopt -s nullglob
@ -174,17 +172,20 @@ shopt -s nullglob
create_prototype_shim create_prototype_shim
remove_outdated_shims remove_outdated_shims
# shellcheck disable=SC2046 # 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 install_registered_shims
remove_stale_shims remove_stale_shims

+ 22
- 22
pyenv.d/rehash/conda.bash 查看文件

@ -11,28 +11,6 @@ conda_exists() {
[ -n "${condas}" ] [ -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() { deregister_conda_shims() {
# adapted for Bash 4.x's associative array (#1749) # adapted for Bash 4.x's associative array (#1749)
if declare -p registered_shims 2> /dev/null | grep -Eq '^(declare|typeset) -A'; then 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 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 deregister_conda_shims
fi fi

+ 14
- 14
pyenv.d/rehash/source.bash 查看文件

@ -1,16 +1,12 @@
PROTOTYPE_SOURCE_SHIM_PATH="${SHIM_PATH}/.pyenv-source-shim" 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 fi
done
shopt -u nullglob
eval "source_shim(){ case \"\${1##*/}\" in ${shims[@]} *)return 1;;esac;}"
cat > "${PROTOTYPE_SOURCE_SHIM_PATH}" <<SH
cat > "${PROTOTYPE_SOURCE_SHIM_PATH}" <<SH
[ -n "\$PYENV_DEBUG" ] && set -x [ -n "\$PYENV_DEBUG" ] && set -x
export PYENV_ROOT="${PYENV_ROOT}" export PYENV_ROOT="${PYENV_ROOT}"
program="\$("$(command -v pyenv)" which "\${BASH_SOURCE##*/}")" program="\$("$(command -v pyenv)" which "\${BASH_SOURCE##*/}")"
@ -18,14 +14,18 @@ if [ -e "\${program}" ]; then
. "\${program}" "\$@" . "\${program}" "\$@"
fi fi
SH SH
chmod +x "${PROTOTYPE_SOURCE_SHIM_PATH}"
chmod +x "${PROTOTYPE_SOURCE_SHIM_PATH}"
}
shopt -s nullglob 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 fi
done done
shopt -u nullglob
rm -f "${PROTOTYPE_SOURCE_SHIM_PATH}" rm -f "${PROTOTYPE_SOURCE_SHIM_PATH}"

Loading…
取消
儲存