pyenv_setup.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd -P)"
  8. # shellcheck disable=SC1090
  9. source "${HERE}/lib.sh"
  10. get_shell_startup_script() {
  11. local _startup_script=''
  12. if [[ -n "$SHELL" ]]; then
  13. case "$SHELL" in
  14. */bash)
  15. _startup_script="${HOME}/.bash_profile"
  16. ;;
  17. */zsh)
  18. _startup_script="${HOME}/.zshrc"
  19. ;;
  20. */fish)
  21. _startup_script="${HOME}/.config/fish/config.fish"
  22. ;;
  23. *)
  24. echo "$SHELL is currently not supported."
  25. exit 1
  26. esac
  27. else
  28. echo "The environment variable \$SHELL needs to be defined."
  29. exit 1
  30. fi
  31. echo "$_startup_script"
  32. }
  33. _append_to_startup_script() {
  34. if [[ -n "$SHELL" ]]; then
  35. case "$SHELL" in
  36. */bash)
  37. # shellcheck disable=SC2016
  38. echo -e '\nif command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> "${1}"
  39. ;;
  40. */zsh)
  41. # shellcheck disable=SC2016
  42. echo -e '\nif command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> "${1}"
  43. ;;
  44. */fish)
  45. echo -e '\n\n# pyenv init\nif command -v pyenv 1>/dev/null 2>&1\n pyenv init - | source\nend' >> "$1"
  46. esac
  47. echo "--> Tail of ${1}"
  48. tail -n 3 "${1}"
  49. fi
  50. }
  51. append_to_config() {
  52. if [[ -n "$1" ]]; then
  53. echo "Adding pyenv init (if missing) to ${1}..."
  54. # shellcheck disable=SC2016
  55. if ! grep -qF "pyenv init" "${1}"; then
  56. # pyenv init - is needed to include the pyenv shims in your PATH
  57. # The first \n is very important since on Github workers the output was being appended to
  58. # the last line rather than on a new line. I never figured out why
  59. _append_to_startup_script "${1}"
  60. fi
  61. fi
  62. }
  63. install_pyenv() {
  64. if require pyenv; then
  65. echo "Installing Python (if missing) via pyenv"
  66. local pyenv_version
  67. pyenv_version=$(pyenv -v | awk '{print $2}')
  68. python_version=$(xargs -n1 < .python-version)
  69. if query_big_sur; then
  70. local flag
  71. # NOTE: pyenv 1.2.22 or greater does not require using LDFLAGS
  72. # https://github.com/pyenv/pyenv/pull/1711
  73. if [[ "$pyenv_version" < 1.2.22 ]]; then
  74. flag="-L$(xcrun --show-sdk-path)/usr/lib ${LDFLAGS}"
  75. fi
  76. # cat is used since pyenv would finish to soon when the Python version is already installed
  77. curl -sSL https://github.com/python/cpython/commit/8ea6353.patch | cat | \
  78. LDFLAGS="$flag" pyenv install --skip-existing --patch "$python_version"
  79. else
  80. pyenv install --skip-existing "$python_version"
  81. fi
  82. else
  83. echo "!!! pyenv not found, try running bootstrap script again or run \`brew bundle\` in the sentry repo"
  84. exit 1
  85. fi
  86. }
  87. # Setup pyenv of path
  88. setup_pyenv() {
  89. install_pyenv
  90. _startup_script=$(get_shell_startup_script)
  91. append_to_config "$_startup_script"
  92. # If the script is called with the "dot space right" approach (. ./scripts/pyenv_setup.sh),
  93. # the effects of this will be persistent outside of this script
  94. echo "Activating pyenv and validating Python version"
  95. eval "$(pyenv init -)"
  96. python_version=$(python -V | sed s/Python\ //g)
  97. [[ $python_version == $(cat .python-version) ]] || \
  98. (echo "Wrong Python version: $python_version. Please report in #discuss-dev-tooling" && exit 1)
  99. }
  100. setup_pyenv