您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

131 行
2.8 KiB

  1. #!/usr/bin/env bash
  2. # Summary: List all Python versions available to pyenv
  3. # Usage: pyenv versions [--bare] [--skip-aliases]
  4. #
  5. # Lists all Python versions found in `$PYENV_ROOT/versions/*'.
  6. set -e
  7. [ -n "$PYENV_DEBUG" ] && set -x
  8. unset bare
  9. unset skip_aliases
  10. # Provide pyenv completions
  11. for arg; do
  12. case "$arg" in
  13. --complete )
  14. echo --bare
  15. echo --skip-aliases
  16. exit ;;
  17. --bare ) bare=1 ;;
  18. --skip-aliases ) skip_aliases=1 ;;
  19. * )
  20. pyenv-help --usage versions >&2
  21. exit 1
  22. ;;
  23. esac
  24. done
  25. versions_dir="${PYENV_ROOT}/versions"
  26. if ! enable -f "${BASH_SOURCE%/*}"/pyenv-realpath.dylib realpath 2>/dev/null; then
  27. if [ -n "$PYENV_NATIVE_EXT" ]; then
  28. echo "pyenv: failed to load \`realpath' builtin" >&2
  29. exit 1
  30. fi
  31. READLINK=$(type -p greadlink readlink | head -1)
  32. if [ -z "$READLINK" ]; then
  33. echo "pyenv: cannot find readlink - are you missing GNU coreutils?" >&2
  34. exit 1
  35. fi
  36. resolve_link() {
  37. $READLINK "$1"
  38. }
  39. realpath() {
  40. local path="$1"
  41. local name
  42. # Use a subshell to avoid changing the current path
  43. (
  44. while [ -n "$path" ]; do
  45. name="${path##*/}"
  46. [ "$name" = "$path" ] || cd "${path%/*}"
  47. path="$(resolve_link "$name" || true)"
  48. done
  49. echo "${PWD}/$name"
  50. )
  51. }
  52. fi
  53. if [ -d "$versions_dir" ]; then
  54. versions_dir="$(realpath "$versions_dir")"
  55. fi
  56. if [ -n "$bare" ]; then
  57. hit_prefix=""
  58. miss_prefix=""
  59. current_versions=()
  60. include_system=""
  61. else
  62. hit_prefix="* "
  63. miss_prefix=" "
  64. OLDIFS="$IFS"
  65. IFS=: current_versions=($(pyenv-version-name || true))
  66. IFS="$OLDIFS"
  67. include_system="1"
  68. fi
  69. num_versions=0
  70. exists() {
  71. local car="$1"
  72. local cdar
  73. shift
  74. for cdar in "$@"; do
  75. if [ "${car}" == "${cdar}" ]; then
  76. return 0
  77. fi
  78. done
  79. return 1
  80. }
  81. print_version() {
  82. if exists "$1" "${current_versions[@]}"; then
  83. echo "${hit_prefix}$1 (set by $(pyenv-version-origin))"
  84. else
  85. echo "${miss_prefix}$1"
  86. fi
  87. num_versions=$((num_versions + 1))
  88. }
  89. # Include "system" in the non-bare output, if it exists
  90. if [ -n "$include_system" ] && PYENV_VERSION=system pyenv-which python >/dev/null 2>&1; then
  91. print_version system
  92. fi
  93. shopt -s nullglob
  94. for path in "$versions_dir"/*; do
  95. if [ -d "$path" ]; then
  96. if [ -n "$skip_aliases" ] && [ -L "$path" ]; then
  97. target="$(realpath "$path")"
  98. [ "${target%/*}" != "$versions_dir" ] || continue
  99. [ "${target%/*/envs/*}" != "$versions_dir" ] || continue
  100. fi
  101. print_version "${path##*/}"
  102. # virtual environments created by anaconda/miniconda
  103. for env_path in "${path}/envs/"*; do
  104. if [ -d "${env_path}" ]; then
  105. print_version "${env_path#${PYENV_ROOT}/versions/}"
  106. fi
  107. done
  108. fi
  109. done
  110. shopt -u nullglob
  111. if [ "$num_versions" -eq 0 ] && [ -n "$include_system" ]; then
  112. echo "Warning: no Python detected on the system" >&2
  113. exit 1
  114. fi