Uninstall homebrew packages that affect the Python build

The big-sur Kokoro builder installs Homebrew's python@3.9 package,
which broke these extension modules:

 * The _lzma module has an x86_64 slice that depends on
   /usr/local/opt/xz/lib/liblzma.5.dylib and an arm64 slice that has no
   liblzma dependency. It is loadable on x86_64 but not arm64 (missing
   _lzma_alone_decoder). This module shouldn't be distributed with our
   Python build, because while the macOS SDK has liblzma.5.tbd and
   liblzma.tbd, it has no lzma.h.

 * The _dbm module has an x86_64 slice that depends on
   /usr/local/opt/gdbm/lib/libgdbm_compat.4.dylib. It is loadable on
   arm64 but not x86_64 (missing libgdbm_compat.4.dylib). It should
   have no dependency on a libgdbm_compat dylib.

 * The _gdbm module has an x86_64 slice that depends on
   /usr/local/opt/gdbm/lib/libgdbm.6.dylib. It isn't loadable on either
   x86_64 (missing libgdbm.6.dylib) or arm64 (missing _gdbm_close).
   This module shouldn't be distributed with our Python build.

Test: manual
Change-Id: I0bc7dfefca26a08adad85a0492d5bc2cb756cfc9
diff --git a/kokoro/kokoro_build.sh b/kokoro/kokoro_build.sh
index 7b36c29..767cd17 100755
--- a/kokoro/kokoro_build.sh
+++ b/kokoro/kokoro_build.sh
@@ -20,10 +20,42 @@
 extra_notices=
 
 if [ "$(uname)" == "Darwin" ]; then
+    # The Kokoro big-sur builder has some extra x86-64 libraries installed in
+    # /usr/local (using homebrew), which override the MacOS SDK. At least 3
+    # modules are affected (_dbm, _gdbm, and _lzma).
+    brew_all_pkgs=$(brew list)
+    printf "Brew packages installed:\n%s\n\n" "$brew_all_pkgs"
+    brew_pkgs=
+    for name in gdbm xz; do
+        if echo "$brew_all_pkgs" | grep -q "^${name}\(@\|$\)"; then
+            brew_pkgs="$brew_pkgs $name"
+        fi
+    done
+    if [ -n "$brew_pkgs" ]; then
+        # A local developer probably won't have $KOKORO_ARTIFACTS_DIR set.
+        if [ -n "$KOKORO_ARTIFACTS_DIR" ]; then
+            # Pass --ignore-dependencies because some Homebrew packages still
+            # need the packages we want to remove, notably python@3.9, which
+            # we're using later to run kokoro/build.py.
+            cmd="brew uninstall --ignore-dependencies $brew_pkgs"
+            echo "Will run in 5 seconds (press Ctrl-C to abort): $cmd"
+            sleep 5
+            $cmd
+        else
+            echo "!!! WARNING: Your machine has Homebrew packages installed that could"
+            echo "!!! affect how some extension modules are built:"
+            echo "!!!"
+            echo "!!!    $brew_pkgs"
+            echo "!!!"
+        fi
+    fi
+
     # http://g3doc/devtools/kokoro/g3doc/userdocs/macos/selecting_xcode
     if [ -d /Applications/Xcode_12.5.1.app ]; then
         xcode=/Applications/Xcode_12.5.1.app
-        sudo xcode-select -s $xcode/Contents/Developer
+        cmd="sudo xcode-select -s $xcode/Contents/Developer"
+        echo "Running: $cmd"
+        $cmd
         export SDKROOT=$xcode/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
     fi
     echo "Selected Xcode: $(xcode-select -p)"