Implement autodetecting Python toolchain

This replaces the stub default Python toolchain with one that actually locates the target platform's Python interpreter at runtime. Try it out with

    bazel build //some_py_binary --experimental_use_python_toolchains

and note that, unlike before (#4815), the correct Python interpreter gets invoked by default regardless of whether you specify `--python_version=PY2` or `--python_version=PY3`.

This toolchain is only intended as a last resort, if the user doesn't define and register a better toolchain (that satisfies the target platform constraints).

Work toward #7375 and #4815. Follow-up work needed to add a test (#7843) and windows support (#7844).

RELNOTES: None
PiperOrigin-RevId: 240417315
diff --git a/tools/python/utils.bzl b/tools/python/utils.bzl
new file mode 100644
index 0000000..9278e32
--- /dev/null
+++ b/tools/python/utils.bzl
@@ -0,0 +1,53 @@
+# Copyright 2019 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Utilities for the @bazel_tools//tools/python package.
+
+This file does not access any Python-rules-specific logic, and is therefore
+less likely to be broken by Python-related changes. That in turn means this
+file is less likely to cause bootstrapping issues.
+"""
+
+def _expand_pyversion_template_impl(ctx):
+    if ctx.outputs.out2:
+        ctx.actions.expand_template(
+            template = ctx.file.template,
+            output = ctx.outputs.out2,
+            substitutions = {"%VERSION%": "2"},
+            is_executable = True,
+        )
+    if ctx.outputs.out3:
+        ctx.actions.expand_template(
+            template = ctx.file.template,
+            output = ctx.outputs.out3,
+            substitutions = {"%VERSION%": "3"},
+            is_executable = True,
+        )
+
+expand_pyversion_template = rule(
+    implementation = _expand_pyversion_template_impl,
+    attrs = {
+        "template": attr.label(
+            allow_single_file = True,
+            doc = "The input template file.",
+        ),
+        "out2": attr.output(doc = """\
+The output file produced by substituting "%VERSION%" with "2"."""),
+        "out3": attr.output(doc = """\
+The output file produced by substituting "%VERSION%" with "3"."""),
+    },
+    doc = """\
+Given a template file, generates two expansions by replacing the substring
+"%VERSION%" with "2" and "3".""",
+)