pyenv_setup.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. set -eu
  7. HERE="$(
  8. cd "$(dirname "${BASH_SOURCE[0]}")"
  9. pwd -P
  10. )"
  11. # shellcheck disable=SC1090
  12. source "${HERE}/lib.sh"
  13. get_shell_startup_script() {
  14. local _startup_script=''
  15. if [[ -n "$SHELL" ]]; then
  16. case "$SHELL" in
  17. */bash)
  18. _startup_script="${HOME}/.bash_profile"
  19. ;;
  20. */zsh)
  21. _startup_script="${HOME}/.zprofile"
  22. ;;
  23. */fish)
  24. _startup_script="${HOME}/.config/fish/config.fish"
  25. ;;
  26. *)
  27. echo "$SHELL is currently not supported."
  28. exit 1
  29. ;;
  30. esac
  31. else
  32. echo "The environment variable \$SHELL needs to be defined."
  33. exit 1
  34. fi
  35. echo "$_startup_script"
  36. }
  37. # The first \n is important on Github workers since it was being appended to
  38. # the last line rather than on a new line. I never figured out why
  39. _append_to_startup_script() {
  40. if [[ -n "$SHELL" ]]; then
  41. case "$SHELL" in
  42. */bash)
  43. # shellcheck disable=SC2016
  44. echo "Visit https://github.com/pyenv/pyenv#installation on how to fully set up your Bash shell.";;
  45. */zsh)
  46. # shellcheck disable=SC2016
  47. echo -e '# It is assumed that pyenv is installed via Brew, so this is all we need to do.\n' \
  48. 'eval "$(pyenv init --path)"' >>"${1}"
  49. ;;
  50. */fish)
  51. # shellcheck disable=SC2016
  52. echo -e '\n# pyenv init\nstatus is-login; and pyenv init --path | source' >> "${1}"
  53. ;;
  54. esac
  55. echo "--> Tail of ${1}"
  56. tail -n 3 "${1}"
  57. fi
  58. }
  59. append_to_config() {
  60. if [[ -n "$1" ]]; then
  61. if grep -qF "(pyenv init -)" "${1}"; then
  62. echo >&2 "!!! Please remove the old-style pyenv initialization and try again:"
  63. echo "sed -i.bak 's/(pyenv init -)/(pyenv init --path)/' ${1}"
  64. exit 1
  65. fi
  66. if ! grep -qF "pyenv init --path" "${1}"; then
  67. echo "Adding pyenv init --path to ${1}..."
  68. # pyenv init --path is needed to include the pyenv shims in your PATH
  69. _append_to_startup_script "${1}"
  70. fi
  71. fi
  72. }
  73. install_pyenv() {
  74. if require pyenv; then
  75. echo "Installing Python (if missing) via pyenv"
  76. local pyenv_version
  77. pyenv_version=$(pyenv -v | awk '{print $2}')
  78. python_version=$(xargs -n1 <.python-version)
  79. # NOTE: We're dropping support for older pyenv versions
  80. if [[ "$pyenv_version" < 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. # We need to patch the source code on Big Sur before building Python
  89. # We can remove this once we upgrade to newer versions of Python
  90. if query_big_sur; then
  91. # cat is used since pyenv would finish to soon when the Python version is already installed
  92. curl -sSL https://github.com/python/cpython/commit/8ea6353.patch | cat |
  93. pyenv install --skip-existing --patch "$python_version"
  94. else
  95. pyenv install --skip-existing "$python_version"
  96. fi
  97. else
  98. echo >&2 "!!! pyenv not found, try running bootstrap script again or run \`brew bundle\` in the sentry repo"
  99. exit 1
  100. fi
  101. }
  102. # Setup pyenv of path
  103. setup_pyenv() {
  104. configure-sentry-cli
  105. install_pyenv
  106. _startup_script=$(get_shell_startup_script)
  107. append_to_config "$_startup_script"
  108. # If the script is called with the "dot space right" approach (. ./scripts/pyenv_setup.sh),
  109. # the effects of this will be persistent outside of this script
  110. echo "Activating pyenv and validating Python version"
  111. # Sets up PATH for pyenv
  112. eval "$(pyenv init --path)"
  113. python_version=$(python -V | sed s/Python\ //g)
  114. [[ $python_version == $(cat .python-version) ]] ||
  115. (echo "Wrong Python version: $python_version. Please report in #discuss-dev-tooling" && exit 1)
  116. }
  117. setup_pyenv