Remove python hacks from update.sh

1. Python 2 is no longer needed to run recipes, so we can delete it.
2. vpython ships with its own Python interpreter, so we no longer need
   to install our own copy of Cpython.

In order to make sure all the `python` and `python3` executables are
deleted from all users' checkouts, we have to add a mechanism to
garbage-collect the entire tools directory, which will be helpful when
removing any tools in the future as well.

Change-Id: I1ac8eaca26f8c7d2870a64408661a8c7cec98dec
Reviewed-on: https://fuchsia-review.googlesource.com/c/infra/prebuilt/+/853649
Reviewed-by: Marc-Antoine Ruel <maruel@google.com>
diff --git a/cipd.ensure b/cipd.ensure
index bb178d1..a726058 100644
--- a/cipd.ensure
+++ b/cipd.ensure
@@ -53,23 +53,4 @@
 infra/tools/preview_email/${platform} git_revision:904cc4d81f59dc26b13f1101a0aba4b84ff33417
 
 # vpython
-@Subdir tools/vpython_upstream
 infra/tools/luci/vpython/${platform} git_revision:904cc4d81f59dc26b13f1101a0aba4b84ff33417
-
-# Chromium-managed python3 (required by vpython3)
-# Keep version in sync with https://chromium.googlesource.com/chromium/tools/depot_tools/+/main/bootstrap/manifest.txt
-# TODO(crbug.com/1176014): Assuming gLinux Python 3.9 doesn't have the same
-# issues as 3.8, this will likely no longer be necessary once vpython supports
-# Python 3.9. At that point we can delete this shim and go back to running
-# vpython3 directly by moving it back to the top level of the "tools" directory.
-@Subdir tools/python3
-infra/3pp/tools/cpython3/${platform} version:2@3.8.10.chromium.20
-
-# Chromium-managed python2
-# Recipes assume that a `python` executable is in $PATH and that it is Python 2,
-# so we include it here in case the system python installation does not satisfy
-# those requirements.
-# TODO(olivernewman): Delete once the recipe engine no longer depends on Python
-# 2 at all.
-@Subdir tools/python2
-infra/3pp/tools/cpython/${platform} version:2@2.7.18.chromium.40
diff --git a/update.sh b/update.sh
index ca6a410..d0677e2 100755
--- a/update.sh
+++ b/update.sh
@@ -8,41 +8,29 @@
 set -o errexit
 set -o pipefail
 
+# Increment this value when removing tools from cipd.ensure. That will ensure
+# the corresponding executables get removed from every user's prebuilts the next
+# time they run this script.
+VERSION="1"
+
 script_root="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)"
 
+tools_dir="$script_root/tools"
+version_file="$tools_dir/version.txt"
+
+got_version="0"
+if [ -f "$version_file" ]; then
+  got_version="$(cat "$version_file")"
+fi
+if ! [ "$got_version" = "$VERSION" ]; then
+  # Tools are out of date, remove the whole directory to garbage-collect any
+  # tools that may have been deleted from cipd.ensure.
+  rm -rf "$tools_dir"
+fi
+
 "${script_root}/cipd.py" ensure \
   -ensure-file "${script_root}/cipd.ensure" \
   -root "${script_root}" \
   -log-level warning
 
-# Write vpython3 wrapper. The gLinux version of Python 3.8 is broken, so we need
-# to make sure that vpython uses the custom Chromium version of Python from
-# CIPD.
-# TODO(crbug.com/1176014): Assuming gLinux Python 3.9 doesn't have the same
-# issues as 3.8, this will likely no longer be necessary once vpython supports
-# Python 3.9. At that point we can delete this shim and go back to running
-# vpython3 directly.
-echo '#!/usr/bin/env bash
-
-tools_dir="$(dirname "$0")"
-exec "${tools_dir}/vpython_upstream/vpython3" -vpython-interpreter "${tools_dir}/python3/bin/python3" "$@"
-' > "${script_root}/tools/vpython3"
-
-# Write vpython wrapper. gLinux no longer ships python2 by default, so we need
-# to make sure to use the vendored one.
-#
-# This is also necessary because vpython and vpython3 are in the same CIPD
-# package, so if we nest vpython3 in a subdirectory of "tools" then we need to
-# do the same for vpython.
-echo '#!/usr/bin/env bash
-
-tools_dir="$(dirname "$0")"
-exec "${tools_dir}/vpython_upstream/vpython" -vpython-interpreter "${tools_dir}/python2/bin/python2" "$@"
-' > "${script_root}/tools/vpython"
-
-chmod +x "${script_root}/tools/vpython"
-chmod +x "${script_root}/tools/vpython3"
-
-# Make sure that `python` on $PATH points to Python 2, as the recipe engine
-# assumes.
-ln -s -f "${script_root}/tools/python2/bin/python" "${script_root}/tools/python"
+echo "$VERSION" > "$version_file"