[Bazel] Use pyenv to select Python 3.9.5 on our linux/bazel builds. (#10009)

We still need to use python2 when running autotools.
diff --git a/kokoro/common/pyenv.sh b/kokoro/common/pyenv.sh
new file mode 100644
index 0000000..f89dc26
--- /dev/null
+++ b/kokoro/common/pyenv.sh
@@ -0,0 +1,16 @@
+# Shared logic to choose a Python version with pyenv.
+#
+# This file should be `source`d.
+
+# Requested version of Python can be overridden by env variable.
+: ${PYTHON_VERSION:=3.9.5}
+
+if pyenv --version >/dev/null ; then
+  eval "$(pyenv init -)"
+  if ! pyenv global ${PYTHON_VERSION}; then
+    echo "Python ${PYTHON_VERSION} is not available. Versions available:" >&2
+    pyenv versions >&2
+    exit 1
+  fi
+fi
+echo "Using $(python --version || python3 --version)"
diff --git a/kokoro/linux/bazel/build.sh b/kokoro/linux/bazel/build.sh
index fd0f51f..7d053c1 100755
--- a/kokoro/linux/bazel/build.sh
+++ b/kokoro/linux/bazel/build.sh
@@ -9,6 +9,7 @@
 
 # Change to repo root
 cd $(dirname $0)/../../..
+source kokoro/common/pyenv.sh
 
 git submodule update --init --recursive
 
@@ -24,7 +25,11 @@
   @com_google_protobuf_examples//...
 
 # Verify that we can build successfully from generated tar files.
-./autogen.sh && ./configure && make -j$(nproc) dist
+(
+  pyenv versions
+  pyenv shell 2.7.9  # python2 required for old googletest autotools support
+  ./autogen.sh && ./configure && make -j$(nproc) dist
+)
 DIST=`ls *.tar.gz`
 tar -xf $DIST
 cd ${DIST//.tar.gz}
diff --git a/kokoro/linux/bazel_distcheck/build.sh b/kokoro/linux/bazel_distcheck/build.sh
index b122761..517b6fd 100755
--- a/kokoro/linux/bazel_distcheck/build.sh
+++ b/kokoro/linux/bazel_distcheck/build.sh
@@ -23,14 +23,17 @@
    @com_google_protobuf_examples//...
 )
 
-CONTAINER_NAME=gcr.io/protobuf-build/bazel/linux
-CONTAINER_VERSION=5.1.1-e41ccfa1648716433276ebe077c665796550fcbb
-
 use_bazel.sh 5.0.0 || true
 bazel version
 
 # Change to repo root
 cd $(dirname $0)/../../..
+source kokoro/common/pyenv.sh
+
+# Build distribution archive
+date
+bazel build //pkg:dist_all_tar
+bazel shutdown
 
 # Construct temp directory for running the dist build.
 # If you want to run locally and keep the build dir, create a directory
@@ -43,43 +46,19 @@
   trap dist_cleanup EXIT
 fi
 
-# Let Bazel share the distdir.
-TMP_DISTDIR=${DIST_WORK_ROOT}/bazel-distdir
-mkdir -p ${TMP_DISTDIR}
-
-# Build distribution archive
-date
-bazel fetch --distdir=${TMP_DISTDIR} //pkg:dist_all_tar
-bazel build --distdir=${TMP_DISTDIR} //pkg:dist_all_tar
-DIST_ARCHIVE=$(readlink $(bazel info bazel-bin)/pkg/dist_all_tar.tar.gz)
-bazel shutdown
-
-# The `pkg_tar` rule emits a symlink based on the rule name. The actual
-# file is named with the current version.
-date
-echo "Resolved archive path: ${DIST_ARCHIVE}"
-
 # Extract the dist archive.
 date
 DIST_WORKSPACE=${DIST_WORK_ROOT}/protobuf
 mkdir -p ${DIST_WORKSPACE}
-tar -C ${DIST_WORKSPACE} --strip-components=1 -axf ${DIST_ARCHIVE}
+tar -C ${DIST_WORKSPACE} --strip-components=1 -axf bazel-bin/pkg/dist_all_tar.tar.gz
 
 # Perform build steps in the extracted dist sources.
 
 cd ${DIST_WORKSPACE}
 FAILED=false
 
-until docker pull gcr.io/protobuf-build/bazel/linux:${CONTAINER_VERSION}; do
-  sleep 10
-done
-
 date
-docker run --rm \
-  -v ${DIST_WORKSPACE}:/workspace \
-  -v ${TMP_DISTDIR}:${TMP_DISTDIR} \
-  ${CONTAINER_NAME}:${CONTAINER_VERSION} \
-  test --distdir=${TMP_DISTDIR} --test_output=errors -k \
+bazel test --test_output=errors -k \
   "${BUILD_ONLY_TARGETS[@]}" "${TEST_TARGETS[@]}" || FAILED=true
 
 if ${FAILED}; then