You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 regels
1.8 KiB

11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
  1. #!/usr/bin/env bash
  2. #
  3. # Summary: Display the full path to an executable
  4. #
  5. # Usage: pyenv which <command>
  6. #
  7. # Displays the full path to the executable that pyenv will invoke when
  8. # you run the given command.
  9. set -e
  10. [ -n "$PYENV_DEBUG" ] && set -x
  11. # Provide pyenv completions
  12. if [ "$1" = "--complete" ]; then
  13. exec pyenv shims --short
  14. fi
  15. expand_path() {
  16. if [ ! -d "$1" ]; then
  17. return 1
  18. fi
  19. local cwd="$(pwd)"
  20. cd "$1"
  21. pwd
  22. cd "$cwd"
  23. }
  24. remove_from_path() {
  25. local path_to_remove="$(expand_path "$1")"
  26. local result=""
  27. if [ -z "$path_to_remove" ]; then
  28. echo "${PATH}"
  29. return
  30. fi
  31. local paths
  32. IFS=: paths=($PATH)
  33. for path in "${paths[@]}"; do
  34. path="$(expand_path "$path" || true)"
  35. if [ -n "$path" ] && [ "$path" != "$path_to_remove" ]; then
  36. result="${result}${path}:"
  37. fi
  38. done
  39. echo "${result%:}"
  40. }
  41. OLDIFS="$IFS"
  42. IFS=: versions=($(pyenv-version-name))
  43. IFS=: PYENV_VERSION="${versions[*]}"
  44. IFS="$OLDIFS"
  45. PYENV_COMMAND="$1"
  46. if [ -z "$PYENV_COMMAND" ]; then
  47. pyenv-help --usage which >&2
  48. exit 1
  49. fi
  50. for version in "${versions[@]}"; do
  51. if [ "$version" = "system" ]; then
  52. PATH="$(remove_from_path "${PYENV_ROOT}/shims")"
  53. PYENV_COMMAND_PATH="$(command -v "$PYENV_COMMAND" || true)"
  54. else
  55. PYENV_COMMAND_PATH="${PYENV_ROOT}/versions/${version}/bin/${PYENV_COMMAND}"
  56. fi
  57. if [ -x "$PYENV_COMMAND_PATH" ]; then
  58. break
  59. fi
  60. done
  61. OLDIFS="$IFS"
  62. IFS=$'\n' scripts=(`pyenv-hooks which`)
  63. IFS="$OLDIFS"
  64. for script in "${scripts[@]}"; do
  65. source "$script"
  66. done
  67. if [ -x "$PYENV_COMMAND_PATH" ]; then
  68. echo "$PYENV_COMMAND_PATH"
  69. else
  70. echo "pyenv: $PYENV_COMMAND: command not found" >&2
  71. versions="$(pyenv-whence "$PYENV_COMMAND" || true)"
  72. if [ -n "$versions" ]; then
  73. { echo
  74. echo "The \`$1' command exists in these Python versions:"
  75. echo "$versions" | sed 's/^/ /g'
  76. echo
  77. } >&2
  78. fi
  79. exit 127
  80. fi