Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

121 rader
2.9 KiB

  1. #!/usr/bin/env bash
  2. #
  3. # Summary: Set or show the shell-specific Python version
  4. #
  5. # Usage: pyenv shell <version>...
  6. # pyenv shell -
  7. # pyenv shell --unset
  8. #
  9. # Sets a shell-specific Python version by setting the `PYENV_VERSION'
  10. # environment variable in your shell. This version overrides local
  11. # application-specific versions and the global version.
  12. #
  13. # <version> should be a string matching a Python version known to pyenv.
  14. # The special version string `system' will use your default system Python.
  15. # Run `pyenv versions' for a list of available Python versions.
  16. #
  17. # When `-` is passed instead of the version string, the previously set
  18. # version will be restored. With `--unset`, the `PYENV_VERSION`
  19. # environment variable gets unset, restoring the environment to the
  20. # state before the first `pyenv shell` call.
  21. set -e
  22. [ -n "$PYENV_DEBUG" ] && set -x
  23. # Provide pyenv completions
  24. if [ "$1" = "--complete" ]; then
  25. echo --unset
  26. echo system
  27. exec pyenv-versions --bare
  28. fi
  29. versions=("$@")
  30. shell="$(basename "${PYENV_SHELL:-$SHELL}")"
  31. if [ -z "$versions" ]; then
  32. if [ -z "$PYENV_VERSION" ]; then
  33. echo "pyenv: no shell-specific version configured" >&2
  34. exit 1
  35. else
  36. echo 'echo "$PYENV_VERSION"'
  37. exit
  38. fi
  39. fi
  40. if [ "$versions" = "--unset" ]; then
  41. case "$shell" in
  42. fish )
  43. echo 'set -gu PYENV_VERSION_OLD "$PYENV_VERSION"'
  44. echo "set -e PYENV_VERSION"
  45. ;;
  46. * )
  47. echo 'PYENV_VERSION_OLD="$PYENV_VERSION"'
  48. echo "unset PYENV_VERSION"
  49. ;;
  50. esac
  51. exit
  52. fi
  53. if [ "$versions" = "-" ]; then
  54. case "$shell" in
  55. fish )
  56. cat <<EOS
  57. if set -q PYENV_VERSION_OLD
  58. if [ -n "\$PYENV_VERSION_OLD" ]
  59. set PYENV_VERSION_OLD_ "\$PYENV_VERSION"
  60. set -gx PYENV_VERSION "\$PYENV_VERSION_OLD"
  61. set -gu PYENV_VERSION_OLD "\$PYENV_VERSION_OLD_"
  62. set -e PYENV_VERSION_OLD_
  63. else
  64. set -gu PYENV_VERSION_OLD "\$PYENV_VERSION"
  65. set -e PYENV_VERSION
  66. end
  67. else
  68. echo "pyenv: PYENV_VERSION_OLD is not set" >&2
  69. false
  70. end
  71. EOS
  72. ;;
  73. * )
  74. cat <<EOS
  75. if [ -n "\${PYENV_VERSION_OLD+x}" ]; then
  76. if [ -n "\$PYENV_VERSION_OLD" ]; then
  77. PYENV_VERSION_OLD_="\$PYENV_VERSION"
  78. export PYENV_VERSION="\$PYENV_VERSION_OLD"
  79. PYENV_VERSION_OLD="\$PYENV_VERSION_OLD_"
  80. unset PYENV_VERSION_OLD_
  81. else
  82. PYENV_VERSION_OLD="\$PYENV_VERSION"
  83. unset PYENV_VERSION
  84. fi
  85. else
  86. echo "pyenv: PYENV_VERSION_OLD is not set" >&2
  87. false
  88. fi
  89. EOS
  90. ;;
  91. esac
  92. exit
  93. fi
  94. # Make sure the specified version is installed.
  95. if pyenv-prefix "${versions[@]}" >/dev/null; then
  96. OLDIFS="$IFS"
  97. IFS=: version="${versions[*]}"
  98. IFS="$OLDIFS"
  99. if [ "$version" != "$PYENV_VERSION" ]; then
  100. case "$shell" in
  101. fish )
  102. echo 'set -gu PYENV_VERSION_OLD "$PYENV_VERSION"'
  103. echo "set -gx PYENV_VERSION \"$version\""
  104. ;;
  105. * )
  106. echo 'PYENV_VERSION_OLD="$PYENV_VERSION"'
  107. echo "export PYENV_VERSION=\"${version}\""
  108. ;;
  109. esac
  110. fi
  111. else
  112. echo "false"
  113. exit 1
  114. fi