Browse Source

build(py2): Drop Python 2 support from the developer environment (#22903)

This change removes the code that helped a developer install Python 2 in their local development environment.

We also switch away from using the `virtualenv` package to create virtual environments
and instead use the module `venv` (`python -m virtualenv .venv` vs `python -m venv .venv`)
Armen Zambrano G 4 years ago
parent
commit
a67ec6079a

+ 5 - 12
.envrc

@@ -53,13 +53,12 @@ EOF
 }
 
 advice_init_venv() {
-    python_version="$1"
-    venv_name="$2"
+    venv_name="$1"
 
     deactivate 2>/dev/null || true
-    info "To create a virtualenv, please type: python${python_version} -m virtualenv ${venv_name}"
-    require "python${python_version}" ||
-        die "You'll need to install python${python_version}, or make it available on your PATH.
+    info "To create a virtualenv, please type: python3.6 -m venv ${venv_name}"
+    require "python3.6" ||
+        die "You'll need to install python3, or make it available on your PATH.
 It's recommended to use pyenv - please refer to https://docs.sentry.io/development/contribute/environment"
     return 1
 }
@@ -120,18 +119,12 @@ make setup-git-config
 ### Python ###
 
 venv_name=".venv"
-python_version="3.6"
-
-if [ "$SENTRY_PYTHON2" = "1" ]; then
-    venv_name=".venv2"
-    python_version="2.7"
-fi
 
 info "Activating virtualenv..."
 
 if [ ! -f "${venv_name}/bin/activate" ]; then
     info "You don't seem to have a virtualenv."
-    advice_init_venv "$python_version" "$venv_name"
+    advice_init_venv "$venv_name"
 fi
 
 # The user might be cd'ing into sentry with another non-direnv managed

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

@@ -65,8 +65,7 @@ jobs:
 
       - name: Set up development environment (as per docs)
         run: |
-          python -m pip install virtualenv
-          python -m virtualenv .venv
+          python -m venv .venv
           source .venv/bin/activate
           make install-py-dev
           make setup-git

+ 0 - 1
.python-version

@@ -1,2 +1 @@
 3.6.10
-2.7.16

+ 2 - 8
Makefile

@@ -1,9 +1,6 @@
 PIP := python -m pip --disable-pip-version-check
 WEBPACK := yarn build-acceptance
 
-# Currently, this is only required to install black via pre-commit.
-REQUIRED_PY3_VERSION := $(shell grep "3.6" .python-version)
-
 UNAME := $(shell command -v uname 2> /dev/null)
 ifdef UNAME
 	ifeq ($(shell uname), Darwin)
@@ -63,9 +60,6 @@ ifdef BIG_SUR
 	curl -sSL https://github.com/python/cpython/commit/8ea6353.patch | cat | \
 		LDFLAGS="-L$(shell xcrun --show-sdk-path)/usr/lib ${LDFLAGS}" \
 		pyenv install --skip-existing --patch 3.6.10
-	curl -sSL https://github.com/python/cpython/commit/8ea6353.patch | cat | \
-		LDFLAGS="-L$(shell xcrun --show-sdk-path)/usr/lib ${LDFLAGS}" \
-		pyenv install --skip-existing --patch 2.7.16
 else
 	@cat .python-version | xargs -n1 pyenv install --skip-existing
 endif
@@ -84,10 +78,10 @@ setup-git-config:
 setup-git: ensure-venv setup-git-config
 	@echo "--> Installing git hooks"
 	mkdir -p .git/hooks && cd .git/hooks && ln -sf ../../config/hooks/* ./
-	@PYENV_VERSION=$(REQUIRED_PY3_VERSION) python3 -c '' || (echo 'Please run `make setup-pyenv` to install the required Python 3 version.'; exit 1)
+	@python3 -c '' || (echo 'Please run `make setup-pyenv` to install the required Python 3 version.'; exit 1)
 	@# pre-commit loosely pins virtualenv, which has caused problems in the past.
 	$(PIP) install "pre-commit==1.18.2" "virtualenv==20.0.32"
-	@PYENV_VERSION=$(REQUIRED_PY3_VERSION) pre-commit install --install-hooks
+	@pre-commit install --install-hooks
 	@echo ""
 
 node-version-check:

+ 0 - 1
pyproject.toml

@@ -9,7 +9,6 @@ include='\.py$'
 exclude='''
 (
       \.venv/
-      \.venv3/
     | node_modules/
     | migrations/
 )

+ 0 - 54
scripts/bootstrap-py2-venv

@@ -1,54 +0,0 @@
-# This script is meant to be sourced by bash, not executed.
-# As such, don't use exit, use return.
-
-# This script is purely a python virtualenv bootstrap, not the entire development environment.
-# For the entire thing, you should still use make bootstrap.
-# 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.
-
-command -v pyenv >/dev/null || {
-    echo "You need to install pyenv. https://develop.sentry.dev/environment/#python"
-    return 1
-}
-
-command -v direnv >/dev/null || {
-    echo "You need to install direnv. https://develop.sentry.dev/environment/#direnv"
-    return 1
-}
-
-export SENTRY_PYTHON2=1
-
-gitroot="$(git rev-parse --show-toplevel)"
-cd "$gitroot"
-export venv_name="${PWD}/.venv2"
-
-if [[ -f "${venv_name}/bin/activate" ]]; then
-    echo "You seem to have a virtualenv already. This script is meant to start one from scratch. Continue (y/N)?"
-    read resp
-    case "$resp" in
-      y|Y ) echo "Okay, let's do this." ;;
-      * ) echo "Aborted!"; return 1     ;;
-    esac
-fi
-
-deactivate 2>/dev/null || true
-rm -rf "$venv_name"
-
-if ! [[ "$(python2.7 -V 2>&1)" = "Python $(grep "2.7" .python-version)" ]]; then
-    abort "Your python2.7 version isn't as expected. Please run: make setup-pyenv"
-fi
-
-python2.7 -m pip install -U pip         || { echo "bootstrap failed!"; return 1; }
-python2.7 -m pip install -U virtualenv  || { echo "bootstrap failed!"; return 1; }
-python2.7 -m virtualenv "${venv_name}"  || { echo "bootstrap failed!"; return 1; }
-source "${venv_name}/bin/activate"      || { 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; }

+ 1 - 10
scripts/bootstrap-py3-venv

@@ -12,8 +12,6 @@
 
 # So just going to sprinkle returns everywhere.
 
-unset SENTRY_PYTHON2
-
 command -v pyenv >/dev/null || {
     echo "You need to install pyenv. https://develop.sentry.dev/environment/#python"
     return 1
@@ -31,12 +29,6 @@ export 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)?"
 
-    # Temporary special case for Python 2 .venv/ -> .venv2/ transition.
-    # Just a more helpful error message.
-    if [[ "$(python -V 2>&1)" = "Python $(grep "2.7" .python-version)" ]]; then
-        prompt_text="You have an existing Python 2 virtualenv. Remove it and replace with a Python 3 virtualenv (y/N)?"
-    fi
-
     echo "$prompt_text"
     read resp
     case "$resp" in
@@ -53,8 +45,7 @@ if ! [[ "$(python3 -V 2>&1)" = "Python $(grep "3.6" .python-version)" ]]; then
 fi
 
 python3 -m pip install -U pip           || { echo "bootstrap failed!"; return 1; }
-python3 -m pip install -U virtualenv    || { echo "bootstrap failed!"; return 1; }
-python3 -m virtualenv "${venv_name}"    || { 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; }
 make setup-git                          || { echo "bootstrap failed!"; return 1; }
 make install-py-dev                     || { echo "bootstrap failed!"; return 1; }

+ 12 - 28
scripts/ensure-venv.sh

@@ -12,12 +12,6 @@ bold="$(tput bold)"
 reset="$(tput sgr0)"
 
 venv_name=".venv"
-python_version="3.6"
-
-if [[ "$SENTRY_PYTHON2" = "1" ]]; then
-    venv_name=".venv2"
-    python_version="2.7"
-fi
 
 die () {
     cat <<EOF
@@ -29,42 +23,32 @@ EOF
 if [[ -n "$VIRTUAL_ENV" ]]; then
     major=`python -c "import sys; print(sys.version_info[0])"`
     minor=`python -c "import sys; print(sys.version_info[1])"`
-    # If .venv contains Python2 or SENTRY_PYTHON2 is set, then fail with instructions
-    if [[ "$major" -eq 2 ]] || [[ ! -z "$SENTRY_PYTHON2" ]]; then
-        cat << EOF
-${yellow}${bold}
-WARNING! You are running a Python 2 virtualenv, which is DEPRECATED.
-Support will be dropped beginning in 2021.
-${reset}
-EOF
-    else
-        # If .venv is less than Python 3.6 fail
-        [[ "$minor" -lt 6 ]] &&
-            die "Remove $VIRTUAL_ENV and try again since the Python version installed should be at least 3.6."
-        # If .venv is created with Python greater than 3.6 you might encounter problems and we want to ask you to downgrade
-        # unless you explicitely set an environment variable
-        if [[ "$minor" -gt 6 ]]; then
-            if [[ -n "$SENTRY_PYTHON_VERSION" ]]; then
-                cat << EOF
+    # If .venv is less than Python 3.6 fail
+    [[ "$minor" -lt 6 ]] &&
+        die "Remove $VIRTUAL_ENV and try again since the Python version installed should be at least 3.6."
+    # If .venv is created with Python greater than 3.6 you might encounter problems and we want to ask you to downgrade
+    # unless you explicitely set an environment variable
+    if [[ "$minor" -gt 6 ]]; then
+        if [[ -n "$SENTRY_PYTHON_VERSION" ]]; then
+            cat << EOF
 ${yellow}${bold}
 You have explicitly set a non-recommended Python version (${SENTRY_PYTHON_VERSION}). You're on your own.
 ${reset}
 EOF
-            else
-                cat << EOF
+        else
+            cat << EOF
 ${red}${bold}
 ERROR! You are running a virtualenv with a Python version different than 3.6
 We recommend you start with a fresh virtualenv or to set the variable SENTRY_PYTHON_VERSION
 to the Python version you want to use (e.g. 3.7).
 ${reset}
 EOF
-                exit 1
-            fi
+            exit 1
         fi
     fi
 else
     if [[ ! -f "${venv_name}/bin/activate" ]]; then
-        die "You don't seem to have a virtualenv. Please create one by running: python${python_version} -m virtualenv ${venv_name}"
+        die "You don't seem to have a virtualenv. Please create one by running: python3.6 -m venv ${venv_name}"
     fi
     die "You have a virtualenv, but it doesn't seem to be activated. Please run: source ${venv_name}/bin/activate"
 fi