[bazel] detect airgapped mode in `pip_wheel` repo rule

In #12091, a custom bazel repository rule was setup to pre-download
/ build python wheels to transport them to an airgapped machine so the
`pip_install` rule does not attempt to go to the internet. This commit
enables the custom bazel repository rule previously added to detect if
it is executing in airgapped mode (via an environment variable), and if
so, use the pre-built/local Python wheels repo.

This fixes #12090.

Signed-off-by: Timothy Trippel <ttrippel@google.com>
diff --git a/third_party/python/pip.bzl b/third_party/python/pip.bzl
index b8726ac..c31c312 100644
--- a/third_party/python/pip.bzl
+++ b/third_party/python/pip.bzl
@@ -19,6 +19,19 @@
 """
 
 def _pip_wheel_impl(rctx):
+    # First, check if an existing pre-built Python wheels repo exists, and if
+    # so, use it instead of building one.
+    python_wheel_repo = rctx.os.environ.get("BAZEL_PYTHON_WHEELS_REPO", None)
+    repo_top_env = rctx.os.environ.get("REPO_TOP", None)
+    if python_wheel_repo:
+        rctx.report_progress("Mounting existing Python wheels repo")
+        if repo_top_env == None:
+            fail("pip_wheel failed: 'REPO_TOP' environment variable unset")
+        rctx.symlink("{}/{}".format(repo_top_env, python_wheel_repo), ".")
+        return
+
+    # If a pre-built Python wheels repo does not exist, we need to build it.
+
     # First, we install the Python wheel package so we can build other wheels.
     args = [
         rctx.path(rctx.attr.python_interpreter),
@@ -27,8 +40,7 @@
         "install",
         "wheel",
     ]
-    progress_message = "Installing the Python wheel package"
-    rctx.report_progress(progress_message)
+    rctx.report_progress("Installing the Python wheel package")
     result = rctx.execute(
         args,
         timeout = rctx.attr.timeout,
@@ -48,8 +60,7 @@
         "-w",
         "./",
     ]
-    progress_message = "Pre-building Python wheels"
-    rctx.report_progress(progress_message)
+    rctx.report_progress("Pre-building Python wheels")
     result = rctx.execute(
         args,
         timeout = rctx.attr.timeout,
@@ -64,8 +75,7 @@
     # were built above. This enables us to make changes to the main
     # python_requirements.txt file without impacting this step.
     args = [rctx.path(rctx.attr._gen_requirements_script)]
-    progress_message = "Generating sanitzed requirements file"
-    rctx.report_progress(progress_message)
+    rctx.report_progress("Generating sanitzed requirements file")
     result = rctx.execute(
         args,
         timeout = rctx.attr.timeout,
@@ -107,9 +117,10 @@
             allow_single_file = True,
             doc = "Shell script that generates a requirements file without VCS links.",
             executable = True,
-            cfg = "host",
+            cfg = "exec",
         ),
     },
+    environ = ["REPO_TOP", "BAZEL_PYTHON_WHEELS_REPO"],
 )
 
 def pip_deps():