Add a Starlark flag to select() on to get the Python mode

This adds @bazel_tools//tools/python:python_version, whose value is always either "PY2" or "PY3" (never undefined), and which works regardless of the value of --experimental_better_python_version_mixing. Users who need to select() on the Python version should use this label in their `config_setting`s. Users should not select() on "force_python" or "python_version" because these flags may not always represent the true Python version state, and can lead to action conflict errors.

Work toward #6583.

RELNOTES: None
PiperOrigin-RevId: 226235967
diff --git a/tools/python/BUILD b/tools/python/BUILD
index a344b44..62c1b80 100644
--- a/tools/python/BUILD
+++ b/tools/python/BUILD
@@ -1,3 +1,5 @@
+load(":python_version.bzl", "define_python_version_flag")
+
 package(default_visibility = ["//visibility:public"])
 
 sh_binary(
@@ -6,27 +8,15 @@
 )
 
 filegroup(
-    name = "srcs_and_embedded_tools",
-    srcs = [
-        # Tools are build from the workspace for tests.
-        "2to3.sh",
-        "BUILD",
-    ],
-    visibility = ["//visibility:private"],
-)
-
-filegroup(
     name = "srcs",
-    srcs = [
-        ":srcs_and_embedded_tools",
+    srcs = glob(["**"]) + [
         "//tools/python/runfiles:srcs",
     ],
 )
 
 filegroup(
     name = "embedded_tools",
-    srcs = [
-        ":srcs_and_embedded_tools",
+    srcs = glob(["**"]) + [
         "//tools/python/runfiles:embedded_tools",
     ],
     visibility = ["//tools:__pkg__"],
@@ -39,3 +29,48 @@
     ],
     visibility = ["//tools:__pkg__"],
 )
+
+# This target can be used to inspect the current Python major version. To use,
+# put it in the `flag_values` attribute of a `config_setting` and test it
+# against the values "PY2" or "PY3". It will always match one or the other.
+#
+# If you do not need to test any other flags in combination with the Python
+# version, then as a convenience you may use the predefined `config_setting`s
+# `@bazel_tools//python:PY2` and `@bazel_tools//python:PY3`.
+#
+# Example usage:
+#
+#     config_setting(
+#         name = "py3_on_arm",
+#         values = {"cpu": "arm"},
+#         flag_values = {"@bazel_tools//python:python_version": "PY3"},
+#     )
+#
+#     my_target(
+#         ...
+#         some_attr = select({
+#             ":py3_on_arm": ...,
+#             ...
+#         }),
+#         ...
+#     )
+#
+# Caution: Do not `select()` on the built-in command-line flags `--force_python`
+# or `--python_version`, as they do not always reflect the true Python version
+# of the current target. `select()`-ing on them can lead to action conflicts and
+# will be disallowed.
+define_python_version_flag(
+    name = "python_version",
+)
+
+config_setting(
+    name = "PY2",
+    flag_values = {":python_version": "PY2"},
+    visibility = ["//visibility:public"],
+)
+
+config_setting(
+    name = "PY3",
+    flag_values = {":python_version": "PY3"},
+    visibility = ["//visibility:public"],
+)