#!/usr/bin/env bash # Summary: Configure the shell environment for pyenv # Usage: eval "$(pyenv init [-|--path] [--no-push-path] [--detect-shell] [--no-rehash] [])" set -e [ -n "$PYENV_DEBUG" ] && set -x # Provide pyenv completions if [ "$1" = "--complete" ]; then echo - echo --path echo --no-push-path echo --no-rehash echo --detect-shell echo bash echo fish echo ksh echo zsh exit fi mode="help" while [ "$#" -gt 0 ]; do case "$1" in -) mode="print" ;; --path) mode="path" ;; --detect-shell) mode="detect-shell" ;; --no-push-path) no_push_path=1 ;; --no-rehash) no_rehash=1 ;; *) shell="$1" ;; esac shift done # If shell is not provided, detect it. if [ -z "$shell" ]; then if shell=$(tr '\0' ' ' 2>/dev/null &2 } function init_dirs() { mkdir -p "${PYENV_ROOT}/"{shims,versions} } function print_path() { # if no_push_path is set, guard the PATH manipulation with a check on whether # the shim is already in the PATH. if [ -n "$no_push_path" ]; then case "$shell" in fish ) echo 'if not contains -- "'"${PYENV_ROOT}/shims"'" $PATH' print_path_prepend_shims echo 'end' ;; * ) echo 'if [[ ":$PATH:" != *'\':"${PYENV_ROOT}"/shims:\''* ]]; then' print_path_prepend_shims echo 'fi' ;; esac else case "$shell" in fish ) echo 'while set pyenv_index (contains -i -- "'"${PYENV_ROOT}/shims"'" $PATH)' echo 'set -eg PATH[$pyenv_index]; end; set -e pyenv_index' 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. # This is inhibited by a guard in Debian's stock `bashrc` but some people remove it # in order to get proper environment for noninteractive remote commands # (SSH provides /etc/ssh/sshrc and ~/.ssh/rc for that but no-one seems to use them for some reason). # This has caused an infinite `bashrc` execution loop for those people in the below nested Bash invocation (#2367). # --norc negates this behavior of such a customized Bash. echo 'PATH="$(bash --norc -ec '\''IFS=:; paths=($PATH); ' echo 'for i in ${!paths[@]}; do ' echo 'if [[ ${paths[i]} == "'\'\'"${PYENV_ROOT}/shims"\'\''" ]]; then unset '\'\\\'\''paths[i]'\'\\\'\''; ' echo 'fi; done; ' echo 'echo "${paths[*]}"'\'')"' print_path_prepend_shims ;; esac fi } function print_path_prepend_shims() { case "$shell" in fish ) echo 'set -gx PATH '\'"${PYENV_ROOT}/shims"\'' $PATH' ;; * ) echo 'export PATH="'"${PYENV_ROOT}"'/shims:${PATH}"' ;; esac } function print_env() { case "$shell" in fish ) echo "set -gx PYENV_SHELL $shell" ;; * ) echo "export PYENV_SHELL=$shell" ;; esac } function print_completion() { completion="${0%/*/*}/completions/pyenv.${shell}" if [ -r "$completion" ]; then echo "source '$completion'" fi } function print_rehash() { if [ -z "$no_rehash" ]; then echo 'command pyenv rehash 2>/dev/null' fi } function print_shell_function() { commands=(`pyenv-commands --sh`) case "$shell" in fish ) echo \ 'function pyenv set command $argv[1] set -e argv[1] switch "$command" case '"${commands[*]}"' source (pyenv "sh-$command" $argv|psub) case "*" command pyenv "$command" $argv end end' ;; ksh | ksh93 | mksh ) echo \ 'function pyenv { typeset command=${1:-}' ;; * ) echo \ 'pyenv() { local command=${1:-}' ;; esac if [ "$shell" != "fish" ]; then IFS="|" echo \ ' [ "$#" -gt 0 ] && shift case "$command" in '"${commands[*]:-/}"') eval "$(pyenv "sh-$command" "$@")" ;; *) command pyenv "$command" "$@" ;; esac }' fi } main