| 1 | #!/usr/bin/env bash |
| 2 | # Summary: Configure the shell environment for pyenv |
| 3 | # Usage: eval "$(pyenv init [-|--path] [--no-push-path] [--no-rehash] [<shell>])" |
| 4 | # pyenv init --install [<shell>] |
| 5 | # pyenv init --detect-shell [<shell>] |
| 6 | |
| 7 | set -e |
| 8 | [ -n "$PYENV_DEBUG" ] && set -x |
| 9 | |
| 10 | # Provide pyenv completions |
| 11 | if [ "$1" = "--complete" ]; then |
| 12 | echo - |
| 13 | echo --path |
| 14 | echo --install |
| 15 | echo --no-push-path |
| 16 | echo --no-rehash |
| 17 | echo --detect-shell |
| 18 | echo bash |
| 19 | echo fish |
| 20 | echo ksh |
| 21 | echo pwsh |
| 22 | echo zsh |
| 23 | exit |
| 24 | fi |
| 25 | |
| 26 | mode="help" |
| 27 | |
| 28 | while [ "$#" -gt 0 ]; do |
| 29 | case "$1" in |
| 30 | -) |
| 31 | mode="print" |
| 32 | ;; |
| 33 | --path) |
| 34 | mode="path" |
| 35 | ;; |
| 36 | --install) |
| 37 | mode="install" |
| 38 | ;; |
| 39 | --detect-shell) |
| 40 | mode="detect-shell" |
| 41 | ;; |
| 42 | --no-push-path) |
| 43 | no_push_path=1 |
| 44 | ;; |
| 45 | --no-rehash) |
| 46 | no_rehash=1 |
| 47 | ;; |
| 48 | *) |
| 49 | shell="$1" |
| 50 | ;; |
| 51 | esac |
| 52 | shift |
| 53 | done |
| 54 | |
| 55 | # If shell is not provided, detect it. |
| 56 | if [ -z "$shell" ]; then |
| 57 | if shell=$(tr '\0' ' ' 2>/dev/null </proc/"$PPID"/cmdline);then |
| 58 | : |
| 59 | else |
| 60 | shell=$(ps p "$PPID" -o 'args=' 2>/dev/null || true) |
| 61 | fi |
| 62 | shell="${shell%% *}" |
| 63 | shell="${shell##-}" |
| 64 | shell="${shell:-$SHELL}" |
| 65 | shell="${shell##*/}" |
| 66 | shell="${shell%%-*}" |
| 67 | fi |
| 68 | |
| 69 | function main() { |
| 70 | case "$mode" in |
| 71 | "help") |
| 72 | help_ |
| 73 | exit 1 |
| 74 | ;; |
| 75 | "path") |
| 76 | print_path |
| 77 | print_rehash |
| 78 | exit 0 |
| 79 | ;; |
| 80 | "print") |
| 81 | init_dirs |
| 82 | print_path |
| 83 | print_env |
| 84 | print_completion |
| 85 | print_rehash |
| 86 | print_shell_function |
| 87 | exit 0 |
| 88 | ;; |
| 89 | "detect-shell") |
| 90 | detect_profile |
| 91 | print_detect_shell |
| 92 | exit 0 |
| 93 | ;; |
| 94 | "install") |
| 95 | install_shell_startup_files |
| 96 | exit 0 |
| 97 | ;; |
| 98 | esac |
| 99 | # should never get here |
| 100 | exit 2 |
| 101 | } |
| 102 | |
| 103 | function detect_profile() { |
| 104 | case "$shell" in |
| 105 | bash ) |
| 106 | if [ -e "${HOME}/.bash_profile" ]; then |
| 107 | profile='~/.bash_profile' |
| 108 | else |
| 109 | profile='~/.profile' |
| 110 | fi |
| 111 | profile_explain="~/.bash_profile if it exists, otherwise ~/.profile" |
| 112 | rc='~/.bashrc' |
| 113 | ;; |
| 114 | fish ) |
| 115 | profile='~/.config/fish/config.fish' |
| 116 | rc='~/.config/fish/config.fish' |
| 117 | ;; |
| 118 | pwsh ) |
| 119 | profile='~/.config/powershell/profile.ps1' |
| 120 | rc='~/.config/powershell/profile.ps1' |
| 121 | ;; |
| 122 | zsh ) |
| 123 | profile='~/.zprofile' |
| 124 | rc='~/.zshrc' |
| 125 | ;; |
| 126 | ksh | ksh93 | mksh ) |
| 127 | # There are two implementations of Korn shell: AT&T (ksh93) and Mir (mksh). |
| 128 | # Systems may have them installed under those names, or as ksh, so those |
| 129 | # are recognized here. The obsolete ksh88 (subsumed by ksh93) and pdksh |
| 130 | # (subsumed by mksh) are not included, since they are unlikely to still |
| 131 | # be in use as interactive shells anywhere. |
| 132 | profile='~/.profile' |
| 133 | rc='~/.profile' |
| 134 | ;; |
| 135 | * ) |
| 136 | profile= |
| 137 | rc= |
| 138 | profile_explain='your shell'\''s login startup file' |
| 139 | rc_explain='your shell'\''s interactive startup file' |
| 140 | ;; |
| 141 | esac |
| 142 | } |
| 143 | |
| 144 | function print_detect_shell() { |
| 145 | echo "PYENV_SHELL_DETECT=$shell" |
| 146 | echo "PYENV_PROFILE_DETECT=$profile" |
| 147 | echo "PYENV_RC_DETECT=$rc" |
| 148 | } |
| 149 | |
| 150 | function help_() { |
| 151 | detect_profile |
| 152 | { |
| 153 | case "$shell" in |
| 154 | fish ) |
| 155 | echo "# Add pyenv executable to PATH by running" |
| 156 | echo "# the following interactively:" |
| 157 | echo |
| 158 | print_fish_user_path_setup |
| 159 | echo |
| 160 | echo "# Load pyenv automatically by appending" |
| 161 | echo "# the following to ~/.config/fish/config.fish:" |
| 162 | echo |
| 163 | print_fish_shell_setup |
| 164 | echo |
| 165 | ;; |
| 166 | pwsh ) |
| 167 | echo '# Load pyenv automatically by appending' |
| 168 | echo "# the following to $profile :" |
| 169 | echo |
| 170 | print_pwsh_shell_setup |
| 171 | ;; |
| 172 | * ) |
| 173 | echo '# Load pyenv automatically by appending' |
| 174 | echo -n "# the following to " |
| 175 | if [[ "$profile" == "$rc" && -z $rc_explain ]]; then |
| 176 | echo "${profile_explain:-$profile} :" |
| 177 | else |
| 178 | echo |
| 179 | echo "# ${profile_explain:-$profile} (for login shells)" |
| 180 | echo "# and ${rc_explain:-$rc} (for interactive shells) :" |
| 181 | fi |
| 182 | echo |
| 183 | print_posix_shell_setup |
| 184 | ;; |
| 185 | esac |
| 186 | echo |
| 187 | echo '# Restart your shell for the changes to take effect.' |
| 188 | echo |
| 189 | } >&2 |
| 190 | } |
| 191 | |
| 192 | function print_posix_shell_setup() { |
| 193 | echo 'export PYENV_ROOT="$HOME/.pyenv"' |
| 194 | echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' |
| 195 | echo 'eval "$(pyenv init - '$shell')"' |
| 196 | } |
| 197 | |
| 198 | function print_fish_shell_setup() { |
| 199 | echo 'pyenv init - fish | source' |
| 200 | } |
| 201 | |
| 202 | function print_fish_user_path_setup() { |
| 203 | echo 'set -Ux PYENV_ROOT $HOME/.pyenv' |
| 204 | echo 'if functions -q fish_add_path' |
| 205 | echo ' test -d $PYENV_ROOT/bin; and fish_add_path $PYENV_ROOT/bin' |
| 206 | echo 'else' |
| 207 | echo ' test -d $PYENV_ROOT/bin; and set -U fish_user_paths $PYENV_ROOT/bin $fish_user_paths' |
| 208 | echo 'end' |
| 209 | } |
| 210 | |
| 211 | function print_pwsh_shell_setup() { |
| 212 | echo '$Env:PYENV_ROOT="$Env:HOME/.pyenv"' |
| 213 | echo 'if (Test-Path -LP "$Env:PYENV_ROOT/bin" -PathType Container) {' |
| 214 | echo ' $Env:PATH="$Env:PYENV_ROOT/bin:$Env:PATH" }' |
| 215 | echo 'iex ((pyenv init -) -join "`n")' |
| 216 | } |
| 217 | |
| 218 | function expand_home_path() { |
| 219 | local path="$1" |
| 220 | printf '%s\n' "${path/#\~/$HOME}" |
| 221 | } |
| 222 | |
| 223 | function install_shell_startup_files() { |
| 224 | if [[ -z $HOME ]]; then |
| 225 | echo "pyenv: HOME must be set to configure shell startup files" >&2 |
| 226 | return 1 |
| 227 | fi |
| 228 | |
| 229 | detect_profile |
| 230 | |
| 231 | local files=() |
| 232 | local lines=() |
| 233 | local profile_path rc_path setup |
| 234 | |
| 235 | case "$shell" in |
| 236 | bash | zsh | ksh | ksh93 | mksh ) |
| 237 | rc_path="$(expand_home_path "$rc")" |
| 238 | profile_path="$(expand_home_path "$profile")" |
| 239 | setup="$(print_posix_shell_setup)" |
| 240 | files=("$rc_path") |
| 241 | lines=("$setup") |
| 242 | if [[ $profile_path != "$rc_path" ]]; then |
| 243 | files+=("$profile_path") |
| 244 | lines+=("$setup") |
| 245 | fi |
| 246 | ;; |
| 247 | fish ) |
| 248 | rc_path="$(expand_home_path "$rc")" |
| 249 | files=("$rc_path") |
| 250 | lines=("$(print_fish_shell_setup)") |
| 251 | ;; |
| 252 | pwsh ) |
| 253 | rc_path="$(expand_home_path "$rc")" |
| 254 | files=("$rc_path") |
| 255 | lines=("$(print_pwsh_shell_setup)") |
| 256 | ;; |
| 257 | * ) |
| 258 | echo "pyenv: cannot automatically configure startup files for $shell" >&2 |
| 259 | return 1 |
| 260 | ;; |
| 261 | esac |
| 262 | |
| 263 | local index |
| 264 | for ((index = 0; index < ${#files[@]}; index++)); do |
| 265 | check_startup_file "${files[$index]}" || return 1 |
| 266 | done |
| 267 | |
| 268 | if [[ $shell == fish ]]; then |
| 269 | install_fish_user_paths || return 1 |
| 270 | fi |
| 271 | |
| 272 | for ((index = 0; index < ${#files[@]}; index++)); do |
| 273 | append_lines "${files[$index]}" "${lines[$index]}" |
| 274 | done |
| 275 | } |
| 276 | |
| 277 | function check_startup_file() { |
| 278 | local file="$1" |
| 279 | local grep_status |
| 280 | |
| 281 | if [[ ! -e $file ]]; then |
| 282 | return 0 |
| 283 | fi |
| 284 | |
| 285 | if [[ ! -f $file || ! -r $file ]]; then |
| 286 | echo "pyenv: failed to inspect $file" >&2 |
| 287 | return 1 |
| 288 | fi |
| 289 | |
| 290 | if grep -Fi pyenv "$file" >/dev/null; then |
| 291 | echo "pyenv: cannot automatically apply changes to $file: it appears to already contain Pyenv-related code." >&2 |
| 292 | echo "pyenv: review the file's contents and apply changes manually if necessary." >&2 |
| 293 | echo "pyenv: run \`pyenv init $shell\` to see the suggested setup." >&2 |
| 294 | return 1 |
| 295 | else |
| 296 | grep_status=$? |
| 297 | if [[ $grep_status == 1 ]]; then |
| 298 | return 0 |
| 299 | fi |
| 300 | echo "pyenv: failed to inspect $file" >&2 |
| 301 | return "$grep_status" |
| 302 | fi |
| 303 | } |
| 304 | |
| 305 | function install_fish_user_paths() { |
| 306 | local fish_setup |
| 307 | |
| 308 | if ! command -v fish >/dev/null; then |
| 309 | echo "pyenv: fish is not available to configure fish universal variables" >&2 |
| 310 | return 1 |
| 311 | fi |
| 312 | |
| 313 | fish_setup="$(print_fish_user_path_setup)" |
| 314 | if ! fish -c "$fish_setup"; then |
| 315 | echo "pyenv: failed to configure fish universal variables" >&2 |
| 316 | return 1 |
| 317 | fi |
| 318 | } |
| 319 | |
| 320 | function append_lines() { |
| 321 | local file="$1" |
| 322 | local lines="$2" |
| 323 | local dir last_char |
| 324 | |
| 325 | dir="${file%/*}" |
| 326 | if [[ $dir != "$file" ]]; then |
| 327 | mkdir -p "$dir" |
| 328 | fi |
| 329 | |
| 330 | if [[ -s $file ]]; then |
| 331 | last_char="$(tail -c 1 "$file")" || return 1 |
| 332 | if [[ -n $last_char ]]; then |
| 333 | echo >> "$file" |
| 334 | fi |
| 335 | fi |
| 336 | |
| 337 | printf '%s\n' "$lines" >> "$file" |
| 338 | } |
| 339 | |
| 340 | function init_dirs() { |
| 341 | mkdir -p "${PYENV_ROOT}/"{shims,versions} |
| 342 | } |
| 343 | |
| 344 | function print_path() { |
| 345 | # if no_push_path is set, guard the PATH manipulation with a check on whether |
| 346 | # the shim is already in the PATH. |
| 347 | if [ -n "$no_push_path" ]; then |
| 348 | case "$shell" in |
| 349 | fish ) |
| 350 | echo 'if not contains -- "'"${PYENV_ROOT}/shims"'" $PATH' |
| 351 | print_path_prepend_shims |
| 352 | echo 'end' |
| 353 | ;; |
| 354 | pwsh ) |
| 355 | echo 'if ( $Env:PATH -notmatch "'"${PYENV_ROOT}/shims"'" ) {' |
| 356 | print_path_prepend_shims |
| 357 | echo '}' |
| 358 | ;; |
| 359 | * ) |
| 360 | echo 'if [[ ":$PATH:" != *'\':"${PYENV_ROOT}"/shims:\''* ]]; then' |
| 361 | print_path_prepend_shims |
| 362 | echo 'fi' |
| 363 | ;; |
| 364 | esac |
| 365 | else |
| 366 | case "$shell" in |
| 367 | fish ) |
| 368 | echo 'while set pyenv_index (contains -i -- "'"${PYENV_ROOT}/shims"'" $PATH)' |
| 369 | echo 'set -eg PATH[$pyenv_index]; end; set -e pyenv_index' |
| 370 | print_path_prepend_shims |
| 371 | ;; |
| 372 | pwsh ) |
| 373 | echo '$Env:PATH="$(($Env:PATH -split '"':'"' | where { -not ($_ -match '"'${PYENV_ROOT}/shims'"') }) -join '"':'"')"' |
| 374 | print_path_prepend_shims |
| 375 | ;; |
| 376 | * ) |
| 377 | # Some distros (notably Debian-based) set Bash's SSH_SOURCE_BASHRC compilation option |
| 378 | # that makes it source `bashrc` under SSH even when not interactive. |
| 379 | # This is inhibited by a guard in Debian's stock `bashrc` but some people remove it |
| 380 | # in order to get proper environment for noninteractive remote commands |
| 381 | # (SSH provides /etc/ssh/sshrc and ~/.ssh/rc for that but no-one seems to use them for some reason). |
| 382 | # This has caused an infinite `bashrc` execution loop for those people in the below nested Bash invocation (#2367). |
| 383 | # --norc negates this behavior of such a customized Bash. |
| 384 | echo 'PATH="$(bash --norc -ec '\''IFS=:; paths=($PATH); ' |
| 385 | echo 'for i in ${!paths[@]}; do ' |
| 386 | echo 'if [[ ${paths[i]} == "'\'\'"${PYENV_ROOT}/shims"\'\''" ]]; then unset '\'\\\'\''paths[i]'\'\\\'\''; ' |
| 387 | echo 'fi; done; ' |
| 388 | echo 'echo "${paths[*]}"'\'')"' |
| 389 | print_path_prepend_shims |
| 390 | ;; |
| 391 | esac |
| 392 | fi |
| 393 | } |
| 394 | |
| 395 | function print_path_prepend_shims() { |
| 396 | case "$shell" in |
| 397 | fish ) |
| 398 | echo 'set -gx PATH '\'"${PYENV_ROOT}/shims"\'' $PATH' |
| 399 | ;; |
| 400 | pwsh ) |
| 401 | echo '$Env:PATH="'"${PYENV_ROOT}"'/shims:$Env:PATH"' |
| 402 | ;; |
| 403 | * ) |
| 404 | echo 'export PATH="'"${PYENV_ROOT}"'/shims:${PATH}"' |
| 405 | ;; |
| 406 | esac |
| 407 | } |
| 408 | |
| 409 | function print_env() { |
| 410 | case "$shell" in |
| 411 | fish ) |
| 412 | echo "set -gx PYENV_SHELL $shell" |
| 413 | ;; |
| 414 | pwsh ) |
| 415 | echo '$Env:PYENV_SHELL="'"$shell"'"' |
| 416 | ;; |
| 417 | * ) |
| 418 | echo "export PYENV_SHELL=$shell" |
| 419 | ;; |
| 420 | esac |
| 421 | } |
| 422 | |
| 423 | function print_completion() { |
| 424 | completion="${_PYENV_INSTALL_PREFIX}/completions/pyenv.${shell}" |
| 425 | if [ -r "$completion" ]; then |
| 426 | case "$shell" in |
| 427 | pwsh ) |
| 428 | echo "iex (gc $completion -Raw)" |
| 429 | ;; |
| 430 | * ) |
| 431 | echo "source '$completion'" |
| 432 | ;; |
| 433 | esac |
| 434 | fi |
| 435 | } |
| 436 | |
| 437 | function print_rehash() { |
| 438 | if [ -z "$no_rehash" ]; then |
| 439 | case "$shell" in |
| 440 | pwsh ) |
| 441 | echo '& pyenv rehash' |
| 442 | ;; |
| 443 | * ) |
| 444 | echo 'command pyenv rehash' |
| 445 | ;; |
| 446 | esac |
| 447 | fi |
| 448 | } |
| 449 | |
| 450 | function print_shell_function() { |
| 451 | commands=(`pyenv-commands --sh`) |
| 452 | case "$shell" in |
| 453 | fish ) |
| 454 | echo \ |
| 455 | 'function pyenv |
| 456 | set command $argv[1] |
| 457 | set -e argv[1] |
| 458 | |
| 459 | switch "$command" |
| 460 | case '"${commands[*]}"' |
| 461 | source (pyenv "sh-$command" $argv|psub) |
| 462 | case "*" |
| 463 | command pyenv "$command" $argv |
| 464 | end |
| 465 | end' |
| 466 | ;; |
| 467 | pwsh ) |
| 468 | cat <<EOS |
| 469 | function pyenv { |
| 470 | \$command="" |
| 471 | if ( \$args.Count -gt 0 ) { |
| 472 | \$command, \$args = \$args |
| 473 | } |
| 474 | |
| 475 | if ( ("${commands[*]}" -split ' ') -contains \$command ) { |
| 476 | \$shell_cmds = (& (get-command -commandtype application pyenv) sh-\$command \$args) |
| 477 | if ( \$shell_cmds.Count -gt 0 ) { |
| 478 | iex (\$shell_cmds -join "\`n") |
| 479 | } |
| 480 | } else { |
| 481 | & (get-command -commandtype application pyenv) \$command \$args |
| 482 | } |
| 483 | } |
| 484 | EOS |
| 485 | ;; |
| 486 | ksh | ksh93 | mksh ) |
| 487 | echo \ |
| 488 | 'function pyenv { |
| 489 | typeset command=${1:-}' |
| 490 | ;; |
| 491 | * ) |
| 492 | echo \ |
| 493 | 'pyenv() { |
| 494 | local command=${1:-}' |
| 495 | ;; |
| 496 | esac |
| 497 | |
| 498 | if [ "$shell" != "fish" ] && [ "$shell" != "pwsh" ]; then |
| 499 | IFS="|" |
| 500 | echo \ |
| 501 | ' [ "$#" -gt 0 ] && shift |
| 502 | case "$command" in |
| 503 | '"${commands[*]:-/}"') |
| 504 | eval "$(pyenv "sh-$command" "$@")" |
| 505 | ;; |
| 506 | *) |
| 507 | command pyenv "$command" "$@" |
| 508 | ;; |
| 509 | esac |
| 510 | }' |
| 511 | fi |
| 512 | } |
| 513 | |
| 514 | main |