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.

89 lines
2.0 KiB

  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. remove_from_path() {
  16. local path_to_remove="$1"
  17. local path_before
  18. local result=":${PATH//\~/$HOME}:"
  19. while [ "$path_before" != "$result" ]; do
  20. path_before="$result"
  21. result="${result//:$path_to_remove:/:}"
  22. done
  23. result="${result%:}"
  24. echo "${result#:}"
  25. }
  26. PYENV_COMMAND="$1"
  27. if [ -z "$PYENV_COMMAND" ]; then
  28. pyenv-help --usage which >&2
  29. exit 1
  30. fi
  31. OLDIFS="$IFS"
  32. IFS=: versions=(${PYENV_VERSION:-$(pyenv-version-name)})
  33. IFS="$OLDIFS"
  34. for version in "${versions[@]}"; do
  35. if [ "$version" = "system" ]; then
  36. PATH="$(remove_from_path "${PYENV_ROOT}/shims")"
  37. PYENV_COMMAND_PATH="$(command -v "$PYENV_COMMAND" || true)"
  38. else
  39. PYENV_COMMAND_PATH="${PYENV_ROOT}/versions/${version}/bin/${PYENV_COMMAND}"
  40. fi
  41. if [ -x "$PYENV_COMMAND_PATH" ]; then
  42. break
  43. fi
  44. done
  45. OLDIFS="$IFS"
  46. IFS=$'\n' scripts=(`pyenv-hooks which`)
  47. IFS="$OLDIFS"
  48. for script in "${scripts[@]}"; do
  49. source "$script"
  50. done
  51. if [ -x "$PYENV_COMMAND_PATH" ]; then
  52. echo "$PYENV_COMMAND_PATH"
  53. else
  54. any_not_installed=0
  55. for version in "${versions[@]}"; do
  56. if [ "$version" = "system" ]; then
  57. continue
  58. fi
  59. if ! [ -d "${PYENV_ROOT}/versions/${version}" ]; then
  60. echo "pyenv: version \`$version' is not installed (set by $(pyenv-version-origin))" >&2
  61. any_not_installed=1
  62. fi
  63. done
  64. if [ "$any_not_installed" = 1 ]; then
  65. exit 1
  66. fi
  67. echo "pyenv: $PYENV_COMMAND: command not found" >&2
  68. versions="$(pyenv-whence "$PYENV_COMMAND" || true)"
  69. if [ -n "$versions" ]; then
  70. { echo
  71. echo "The \`$1' command exists in these Python versions:"
  72. echo "$versions" | sed 's/^/ /g'
  73. echo
  74. } >&2
  75. fi
  76. exit 127
  77. fi