Browse Source

fix(dev): Add .python-version back (#29571)

Back in #28213, we added different Python versions depending if executing on Arm or Intel chip-sets.

Last week we dropped Python 3.6 (#29504) and many engineers had to upgrade their dev env.

Unfortunately, engineers w/o `direnv` execution and in some edge cases, started having trouble installing the newer Python version.

This change attempts to simplify the code and hopefully help the edge cases we have seen.
Armen Zambrano G 3 years ago
parent
commit
3e0852785f

+ 2 - 1
.envrc

@@ -4,6 +4,7 @@
 # It also sets useful environment variables.
 # If you'd like to override or set any custom environment variables, this .envrc will read a .env file at the end.
 set -e
+
 SENTRY_ROOT="$(
     cd "$(dirname "${BASH_SOURCE[0]}")"
     pwd -P
@@ -54,7 +55,7 @@ report_to_sentry() {
 }
 
 debug() {
-    if [ "${SENTRY_DIRENV_DEBUG}" ]; then
+    if [ "${SENTRY_DIRENV_DEBUG-}" ]; then
         echo -e "${@}"
     fi
 }

+ 1 - 1
.github/actions/setup-python/action.yml

@@ -38,7 +38,7 @@ runs:
       shell: bash
       run: |
         if [ "${{ inputs.python-version }}" == "" ]; then
-          echo "::set-output name=python-version::$(SENTRY_NO_VENV_CHECK=1 ./scripts/do.sh get-pyenv-version)"
+          echo "::set-output name=python-version::$(cat .python-version)"
         else
           echo "::set-output name=python-version::${{ inputs.python-version }}"
         fi

+ 16 - 1
.github/workflows/development-environment.yml

@@ -14,7 +14,7 @@ on:
 
 jobs:
   dev-environment:
-    name: set up
+    name: dev docs set up
     runs-on: ${{ matrix.os }}
     timeout-minutes: 40
     strategy:
@@ -80,3 +80,18 @@ jobs:
           python -m venv .venv
           source .venv/bin/activate
           make develop init-config
+
+  bootstrap-script:
+    runs-on: macos-11
+    timeout-minutes: 45
+    env:
+      PIP_DISABLE_PIP_VERSION_CHECK: on
+      # Make the environment more similar to what Mac defaults to
+      SHELL: /bin/zsh
+
+    steps:
+      - name: Run bootstrap code
+        env:
+          STRAP_DEBUG: 1
+        run: |
+          bash <(curl -s https://raw.githubusercontent.com/getsentry/bootstrap-sentry/main/bootstrap.sh)

+ 3 - 5
.github/workflows/python-deps.yml

@@ -7,8 +7,10 @@ on:
       - 'requirements-*.txt'
 
 jobs:
+  # This workflow executes faster than the dev env one and focuses on changes
+  # to requirement files
   # This workflow makes sure that Python dependencies install correctly for
-  # a) our current version b) the next version we're targetting
+  # a) our current version
   python-deps:
     name: install
     runs-on: ${{ matrix.os }}
@@ -16,13 +18,9 @@ jobs:
     strategy:
       matrix:
         os: [macos-11.0, ubuntu-20.04]
-        python-version: [3.8.12]
       fail-fast: false
     env:
       PIP_DISABLE_PIP_VERSION_CHECK: on
-      # There's a check that prevents make install-py-dev to work if the developer has not
-      # explicitely set the intention to use a non-default Python version
-      SENTRY_PYTHON_VERSION: ${{ matrix.python-version }}
 
     steps:
       - uses: actions/checkout@v2

+ 1 - 0
.python-version

@@ -0,0 +1 @@
+3.8.12

+ 43 - 22
scripts/bootstrap-py3-venv

@@ -6,16 +6,14 @@
 # Eventually, the goal would be to have an idempotent single script that does
 # everything make bootstrap does and more (system dependencies and so on).
 
-# This... doesn't quite work.
-# set -e
-# trap "echo 'bootstrap FAILED.' && trap - ERR && return" ERR
-
 # So just going to sprinkle returns everywhere.
+
 SCRIPTS_DIR="$(
     cd "$(dirname "${BASH_SOURCE[0]}")"
     pwd -P
 )"
 source "${SCRIPTS_DIR}/lib.sh"
+configure-sentry-cli
 
 command -v pyenv >/dev/null || {
     echo "You need to install pyenv. https://develop.sentry.dev/environment/#python"
@@ -27,37 +25,60 @@ command -v direnv >/dev/null || {
     return 1
 }
 
-gitroot="$(git rev-parse --show-toplevel)"
-cd "$gitroot"
 # This executes very quickly if the Python versions are already installed
-make setup-pyenv
-export PYENV_VERSION=$(SENTRY_NO_VENV_CHECK=1 ${SCRIPTS_DIR}/do.sh get-pyenv-version)
+# This calling approach activates pyenv correctly -> eval "$(pyenv init --path)"
+. "$SCRIPTS_DIR/pyenv_setup.sh"
 venv_name="${PWD}/.venv"
 
 if [[ -f "${venv_name}/bin/activate" ]]; then
-    prompt_text="You have an existing virtualenv. This script will OVERWRITE it. Continue (y/N)?"
-
-    echo "$prompt_text"
+    echo "You have an existing virtualenv. This script will OVERWRITE it. Continue (y/N)?"
     read resp
     case "$resp" in
-      y|Y ) echo "Okay, let's do this." ;;
-      * ) echo "Aborted!"; return 1     ;;
+    y | Y) echo "Okay, let's do this." ;;
+    *)
+        echo "Aborted!"
+        return 1
+        ;;
     esac
 fi
 
 deactivate 2>/dev/null || true
 rm -rf "$venv_name"
 
-if [[ -z "$SENTRY_PYTHON_VERSION" ]] && ! query-valid-python-version; then
+if [ -z "${SENTRY_PYTHON_VERSION-}" ] && ! query-valid-python-version; then
     echo "Your python3 version isn't as expected. Please run: make setup-pyenv"
     return 1
 fi
 
-python3 -m pip install -U pip           || { echo "bootstrap failed!"; return 1; }
-python3 -m venv "${venv_name}"          || { echo "bootstrap failed!"; return 1; }
-source "${venv_name}/bin/activate"      || { echo "bootstrap failed!"; return 1; }
-python3 -m pip install -U pip wheel     || { echo "bootstrap failed!"; return 1; }
-make setup-git                          || { echo "bootstrap failed!"; return 1; }
-make install-py-dev                     || { echo "bootstrap failed!"; return 1; }
-deactivate                              || { echo "bootstrap failed!"; return 1; }
-direnv allow                            || { echo "bootstrap failed!"; return 1; }
+python3 -m pip install -U pip || {
+    echo "bootstrap failed!"
+    return 1
+}
+python3 -m venv "${venv_name}" || {
+    echo "bootstrap failed!"
+    return 1
+}
+source "${venv_name}/bin/activate" || {
+    echo "bootstrap failed!"
+    return 1
+}
+python3 -m pip install -U pip wheel || {
+    echo "bootstrap failed!"
+    return 1
+}
+make setup-git || {
+    echo "bootstrap failed!"
+    return 1
+}
+make install-py-dev || {
+    echo "bootstrap failed!"
+    return 1
+}
+deactivate || {
+    echo "bootstrap failed!"
+    return 1
+}
+direnv allow || {
+    echo "bootstrap failed!"
+    return 1
+}

+ 4 - 9
scripts/lib.sh

@@ -24,6 +24,10 @@ require() {
 }
 
 configure-sentry-cli() {
+    # XXX: For version 1.70.1 there's a bug hitting SENTRY_CLI_NO_EXIT_TRAP: unbound variable
+    # We can remove this after it's fixed
+    # https://github.com/getsentry/sentry-cli/pull/1059
+    export SENTRY_CLI_NO_EXIT_TRAP=${SENTRY_CLI_NO_EXIT_TRAP-0}
     if [ -n "${SENTRY_DSN+x}" ] && [ -z "${SENTRY_DEVENV_NO_REPORT+x}" ]; then
         if ! require sentry-cli; then
             curl -sL https://sentry.io/get-cli/ | bash
@@ -47,15 +51,6 @@ query-apple-m1() {
     query-mac && [[ $(uname -m) = 'arm64' ]]
 }
 
-get-pyenv-version() {
-    if [[ -n "${SENTRY_PYTHON_VERSION:-}" ]]; then
-        echo "${SENTRY_PYTHON_VERSION}"
-        return 0
-    fi
-
-    echo '3.8.12'
-}
-
 query-valid-python-version() {
     if [[ -n "${SENTRY_PYTHON_VERSION:-}" ]]; then
         python_version=$(python3 -V 2>&1 | awk '{print $2}')

+ 5 - 19
scripts/pyenv_setup.sh

@@ -14,9 +14,6 @@ HERE="$(
 )"
 source "${HERE}/lib.sh"
 
-# We can use PYENV_VERSION to define different Python versions, otherwise, determine load default values
-[ -z ${PYENV_VERSION+x} ] && export PYENV_VERSION=$(get-pyenv-version)
-
 get_shell_startup_script() {
   local _startup_script=''
   if [[ -n "$SHELL" ]]; then
@@ -53,8 +50,8 @@ _append_to_startup_script() {
       ;;
     */zsh)
       # shellcheck disable=SC2016
-      echo -e '# It is assumed that pyenv is installed via Brew, so this is all we need to do.\n' \
-        'eval "$(pyenv init --path)"' >>"${1}"
+      echo -e '# It is assumed that pyenv is installed via Brew, so this is all we need to do.' \
+        '\neval "$(pyenv init --path)"' >>"${1}"
       ;;
     */fish)
       # shellcheck disable=SC2016
@@ -69,6 +66,7 @@ _append_to_startup_script() {
 
 append_to_config() {
   if [[ -n "$1" ]]; then
+    [ ! -f "$1" ] && touch "$1"
     if grep -qF "(pyenv init -)" "${1}"; then
       echo >&2 "!!! Please remove the old-style pyenv initialization and try again:"
       echo "sed -i.bak 's/(pyenv init -)/(pyenv init --path)/' ${1}"
@@ -93,19 +91,7 @@ install_pyenv() {
       echo >&2 "brew update && brew uninstall pyenv && brew install pyenv"
       exit 1
     fi
-    # Only try to patch the source code if:
-    # - The user is on Big Sur
-    # - The user is not trying to use their own Python version
-    # - The version is 3.6.x
-    if query-big-sur && [[ -z "${SENTRY_PYTHON_VERSION:-}" ]] && [[ ${PYENV_VERSION} < 3.7.0 ]]; then
-      # For Python 3.6.x, we need to patch the source code on Big Sur before building Python
-      # We can remove this once we upgrade to newer versions of Python
-      # cat is used since pyenv would finish to soon when the Python version is already installed
-      curl -sSL https://github.com/python/cpython/commit/8ea6353.patch | cat |
-        pyenv install --skip-existing --patch "${PYENV_VERSION}"
-    else
-      pyenv install --skip-existing "${PYENV_VERSION}"
-    fi
+    pyenv install --skip-existing
   else
     echo >&2 "!!! pyenv not found, try running bootstrap script again or run \`brew bundle\` in the sentry repo"
     exit 1
@@ -123,7 +109,7 @@ setup_pyenv() {
   # Sets up PATH for pyenv
   eval "$(pyenv init --path)"
   python_version=$(python -V | sed s/Python\ //g)
-  [[ $python_version == "${PYENV_VERSION}" ]] ||
+  [[ $python_version == "$(cat .python-version)" ]] ||
     (echo "Wrong Python version: $python_version. Please report in #discuss-dev-tooling" && exit 1)
 }