mirror of
https://github.com/pyenv/pyenv.git
synced 2024-11-21 20:47:00 -05:00
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:
parent
613e5142b3
commit
9368bebc35
4 changed files with 71 additions and 76 deletions
|
@ -27,24 +27,16 @@ do
|
||||||
if [ "$args" = "-" ]; then
|
if [ "$args" = "-" ]; then
|
||||||
mode="print"
|
mode="print"
|
||||||
shift
|
shift
|
||||||
fi
|
elif [ "$args" = "--path" ]; then
|
||||||
|
|
||||||
if [ "$args" = "--path" ]; then
|
|
||||||
mode="path"
|
mode="path"
|
||||||
shift
|
shift
|
||||||
fi
|
elif [ "$args" = "--detect-shell" ]; then
|
||||||
|
|
||||||
if [ "$args" = "--detect-shell" ]; then
|
|
||||||
mode="detect-shell"
|
mode="detect-shell"
|
||||||
shift
|
shift
|
||||||
fi
|
elif [ "$args" = "--no-push-path" ]; then
|
||||||
|
|
||||||
if [ "$args" = "--no-push-path" ]; then
|
|
||||||
no_push_path=1
|
no_push_path=1
|
||||||
shift
|
shift
|
||||||
fi
|
elif [ "$args" = "--no-rehash" ]; then
|
||||||
|
|
||||||
if [ "$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
|
||||||
|
|
|
@ -90,13 +90,10 @@ 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
|
local file
|
||||||
pyenv-versions --bare --skip-aliases | \
|
for file in "${PYENV_ROOT}/versions/*/bin/"*; do
|
||||||
while read -r version; do
|
|
||||||
for file in "${PYENV_ROOT}/versions/${version}/bin/"*; do
|
|
||||||
echo "${file##*/}"
|
echo "${file##*/}"
|
||||||
done
|
done
|
||||||
done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# The basename of each argument passed to `make_shims` will be
|
# The basename of each argument passed to `make_shims` will be
|
||||||
|
@ -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.
|
get_hooks() {
|
||||||
OLDIFS="$IFS"
|
shopt -s nullglob
|
||||||
IFS=$'\n' scripts=(`pyenv-hooks rehash`)
|
IFS=: hook_paths=($PYENV_HOOK_PATH)
|
||||||
IFS="$OLDIFS"
|
for path in "${hook_paths[@]}"; do
|
||||||
|
for script in "$path/$PYENV_COMMAND"/*.bash; do
|
||||||
for script in "${scripts[@]}"; do
|
|
||||||
source "$script"
|
source "$script"
|
||||||
done
|
done
|
||||||
|
done
|
||||||
|
shopt -u nullglob
|
||||||
|
}
|
||||||
|
get_hooks
|
||||||
|
|
||||||
install_registered_shims
|
install_registered_shims
|
||||||
remove_stale_shims
|
remove_stale_shims
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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;;"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
shopt -u nullglob
|
|
||||||
eval "source_shim(){ case \"\${1##*/}\" in ${shims[@]} *)return 1;;esac;}"
|
|
||||||
|
|
||||||
cat > "${PROTOTYPE_SOURCE_SHIM_PATH}" <<SH
|
create_source_prototype_shim() {
|
||||||
|
if [ -f "${PROTOTYPE_SOURCE_SHIM_PATH}" ]; then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
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
|
for shim in $(cat "${BASH_SOURCE%/*}/source.d/"*".list" | sort -u | sed -e 's/#.*$//' | sed -e '/^[[:space:]]*$/d'); do
|
||||||
if source_shim "${shim}"; then
|
if [ -n "${shim##*/}" ]; then
|
||||||
cp "${PROTOTYPE_SOURCE_SHIM_PATH}" "${shim}"
|
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…
Reference in a new issue