pyenv_setup.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/bash
  2. # This script correctly sets up pyenv
  3. #
  4. # Assumptions:
  5. # - This script assumes you're calling from the top directory of the repository
  6. #
  7. # shellcheck disable=SC2155 # Declare and assign separately to avoid masking return values.
  8. set -eu
  9. HERE="$(
  10. cd "$(dirname "${BASH_SOURCE[0]}")"
  11. pwd -P
  12. )"
  13. source "${HERE}/lib.sh"
  14. # We can use PYENV_VERSION to define different Python versions, otherwise, determine load default values
  15. [ -z ${PYENV_VERSION+x} ] && export PYENV_VERSION=$(get-pyenv-version)
  16. get_shell_startup_script() {
  17. local _startup_script=''
  18. if [[ -n "$SHELL" ]]; then
  19. case "$SHELL" in
  20. */bash)
  21. _startup_script="${HOME}/.bash_profile"
  22. ;;
  23. */zsh)
  24. _startup_script="${HOME}/.zprofile"
  25. ;;
  26. */fish)
  27. _startup_script="${HOME}/.config/fish/config.fish"
  28. ;;
  29. *)
  30. echo "$SHELL is currently not supported."
  31. exit 1
  32. ;;
  33. esac
  34. else
  35. echo "The environment variable \$SHELL needs to be defined."
  36. exit 1
  37. fi
  38. echo "$_startup_script"
  39. }
  40. # The first \n is important on Github workers since it was being appended to
  41. # the last line rather than on a new line. I never figured out why
  42. _append_to_startup_script() {
  43. if [[ -n "$SHELL" ]]; then
  44. case "$SHELL" in
  45. */bash)
  46. # shellcheck disable=SC2016
  47. echo "Visit https://github.com/pyenv/pyenv#installation on how to fully set up your Bash shell."
  48. ;;
  49. */zsh)
  50. # shellcheck disable=SC2016
  51. echo -e '# It is assumed that pyenv is installed via Brew, so this is all we need to do.\n' \
  52. 'eval "$(pyenv init --path)"' >>"${1}"
  53. ;;
  54. */fish)
  55. # shellcheck disable=SC2016
  56. echo -e '\n# pyenv init\nstatus is-login; and pyenv init --path | source' >>"${1}"
  57. ;;
  58. esac
  59. echo "--> Tail of ${1}"
  60. tail -n 3 "${1}"
  61. fi
  62. }
  63. append_to_config() {
  64. if [[ -n "$1" ]]; then
  65. if grep -qF "(pyenv init -)" "${1}"; then
  66. echo >&2 "!!! Please remove the old-style pyenv initialization and try again:"
  67. echo "sed -i.bak 's/(pyenv init -)/(pyenv init --path)/' ${1}"
  68. exit 1
  69. fi
  70. if ! grep -qF "pyenv init --path" "${1}"; then
  71. echo "Adding pyenv init --path to ${1}..."
  72. # pyenv init --path is needed to include the pyenv shims in your PATH
  73. _append_to_startup_script "${1}"
  74. fi
  75. fi
  76. }
  77. install_pyenv() {
  78. if require pyenv; then
  79. # NOTE: We're dropping support for older pyenv versions
  80. if [[ "$(pyenv -v | awk '{print $2}')" < 2.0.0 ]]; then
  81. echo >&2 "!!! We've dropped support for pyenv v1." \
  82. "Run the following (this is slow) and try again."
  83. # brew upgrade does not quite do the right thing
  84. # > ~/.pyenv/shims/python: line 8: /usr/local/Cellar/pyenv/1.2.26/libexec/pyenv: No such file or directory
  85. echo >&2 "brew update && brew uninstall pyenv && brew install pyenv"
  86. exit 1
  87. fi
  88. if query-apple-m1; then
  89. pyenv install --skip-existing "${PYENV_VERSION}"
  90. elif query-big-sur; then
  91. # We need to patch the source code on Big Sur before building Python
  92. # We can remove this once we upgrade to newer versions of Python
  93. # cat is used since pyenv would finish to soon when the Python version is already installed
  94. curl -sSL https://github.com/python/cpython/commit/8ea6353.patch | cat |
  95. pyenv install --skip-existing --patch "${PYENV_VERSION}"
  96. else
  97. pyenv install --skip-existing "${PYENV_VERSION}"
  98. fi
  99. else
  100. echo >&2 "!!! pyenv not found, try running bootstrap script again or run \`brew bundle\` in the sentry repo"
  101. exit 1
  102. fi
  103. }
  104. # Setup pyenv of path
  105. setup_pyenv() {
  106. configure-sentry-cli
  107. install_pyenv
  108. append_to_config "$(get_shell_startup_script)"
  109. # If the script is called with the "dot space right" approach (. ./scripts/pyenv_setup.sh),
  110. # the effects of this will be persistent outside of this script
  111. # Sets up PATH for pyenv
  112. eval "$(pyenv init --path)"
  113. python_version=$(python -V | sed s/Python\ //g)
  114. [[ $python_version == "${PYENV_VERSION}" ]] ||
  115. (echo "Wrong Python version: $python_version. Please report in #discuss-dev-tooling" && exit 1)
  116. }
  117. setup_pyenv