Use a proper bazel toolchain for Python

This patch removes the old `--python_top` approach and instead turns
out Python toolchain into a proper Bazel toolchain. It's now used in
toolchain resolution.

    $ bazel run aos:flatbuffers_static --toolchain_resolution_debug
    WARNING: Option 'ui' is deprecated
    INFO: Invocation ID: 5ed4f6d5-e72d-4705-a853-c9a91264d74c
    INFO: Build option --toolchain_resolution_debug has changed, discarding analysis cache.
    INFO: ToolchainResolution:   Type @bazel_tools//tools/cpp:toolchain_type: target platform //tools/platforms:linux_x86: execution //tools/platforms:linux_x86: Selected toolchain //tools/cpp:cc-compiler-k8
    INFO: ToolchainResolution:   Type @bazel_tools//tools/python:toolchain_type: target platform //tools/platforms:linux_x86: execution //tools/platforms:linux_x86: Selected toolchain //tools/python:py_runtime
    INFO: ToolchainResolution:     Type @bazel_tools//tools/cpp:toolchain_type: target platform //tools/platforms:linux_x86: Rejected toolchain //tools/cpp:cc-compiler-armhf-debian; mismatching values: raspberry_pi
    INFO: ToolchainResolution:     Type @bazel_tools//tools/cpp:toolchain_type: target platform //tools/platforms:linux_x86: Rejected toolchain //tools/cpp:cc-compiler-roborio; mismatching values: roborio
    INFO: ToolchainResolution:     Type @bazel_tools//tools/cpp:toolchain_type: target platform //tools/platforms:linux_x86: Rejected toolchain //tools/cpp:cc-compiler-cortex-m4f; mismatching values: none, cortex_m4f
    INFO: ToolchainResolution:     Type @bazel_tools//tools/cpp:toolchain_type: target platform //tools/platforms:linux_x86: Rejected toolchain @local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: arm, android
    INFO: ToolchainResolution: Target platform //tools/platforms:linux_x86: Selected execution platform //tools/platforms:linux_x86, type @bazel_tools//tools/python:toolchain_type -> toolchain //tools/python:py_runtime, type @bazel_tools//tools/cpp:toolchain_type -> toolchain //tools/cpp:cc-compiler-k8
    INFO: ToolchainResolution: Target platform //tools/platforms:linux_x86: Selected execution platform //tools/platforms:linux_x86,
    INFO: ToolchainResolution: Target platform //tools/platforms:linux_x86: Selected execution platform //tools/platforms:linux_x86,
    INFO: ToolchainResolution:   Type @bazel_tools//tools/cpp:toolchain_type: target platform //tools/platforms:linux_x86: execution //tools/platforms:linux_x86: Selected toolchain //tools/cpp:cc-compiler-k8
    INFO: ToolchainResolution:     Type @bazel_tools//tools/cpp:toolchain_type: target platform //tools/platforms:linux_x86: Rejected toolchain //tools/cpp:cc-compiler-armhf-debian; mismatching values: raspberry_pi
    INFO: ToolchainResolution:     Type @bazel_tools//tools/cpp:toolchain_type: target platform //tools/platforms:linux_x86: Rejected toolchain //tools/cpp:cc-compiler-roborio; mismatching values: roborio
    INFO: ToolchainResolution:     Type @bazel_tools//tools/cpp:toolchain_type: target platform //tools/platforms:linux_x86: Rejected toolchain //tools/cpp:cc-compiler-cortex-m4f; mismatching values: none, cortex_m4f
    INFO: ToolchainResolution:     Type @bazel_tools//tools/cpp:toolchain_type: target platform //tools/platforms:linux_x86: Rejected toolchain @local_config_cc//:cc-compiler-armeabi-v7a; mismatching values: arm, android
    INFO: ToolchainResolution: Target platform //tools/platforms:linux_x86: Selected execution platform //tools/platforms:linux_x86, type @bazel_tools//tools/cpp:toolchain_type -> toolchain //tools/cpp:cc-compiler-k8
    INFO: ToolchainResolution: Target platform //tools/platforms:linux_x86: Selected execution platform //tools/platforms:linux_x86,
    INFO: Analyzed target //aos:flatbuffers_static (0 packages loaded, 45236 targets configured).
    INFO: Found 1 target...
    INFO: Writing explanation of rebuilds to '/tmp/bazel_explain.log'
    Target //aos:flatbuffers_static up-to-date:
      bazel-bin/aos/flatbuffers_static
    INFO: Elapsed time: 2.740s, Critical Path: 0.07s
    INFO: 1 process: 1 internal.
    INFO: Build completed successfully, 1 total action
    INFO: Build completed successfully, 1 total action

Notice the selection of `//tools/python:py_runtime`.

Change-Id: I5adfae4cd11e73b5527a2fc98cef0de6a92464ab
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
diff --git a/.bazelrc b/.bazelrc
index 3b5ad1f..8290da4 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -17,12 +17,6 @@
 # Use the malloc we want.
 build --custom_malloc=//tools/cpp:malloc
 
-# Use our hermetic Python runtime.
-build --python_top=//tools/python:runtime
-build --noincompatible_use_python_toolchains
-build --noincompatible_py3_is_default
-build --noincompatible_py2_outputs_are_suffixed
-
 # Shortcuts for selecting the target platform.
 build:k8 --platforms=//tools/platforms:linux_x86
 build:roborio --platforms=//tools/platforms:linux_roborio
diff --git a/WORKSPACE b/WORKSPACE
index 31c8103..61991e9 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -125,6 +125,7 @@
     "//tools/cpp:cc-toolchain-cortex-m4f",
     # Find a good way to select between these two M4F toolchains.
     #"//tools/cpp:cc-toolchain-cortex-m4f-k22",
+    "//tools/python:python_toolchain",
 )
 
 http_archive(
diff --git a/build_tests/BUILD b/build_tests/BUILD
index 0766256..d142c13 100644
--- a/build_tests/BUILD
+++ b/build_tests/BUILD
@@ -85,8 +85,6 @@
     name = "python3_opencv",
     srcs = ["python_opencv.py"],
     main = "python_opencv.py",
-    python_version = "PY3",
-    srcs_version = "PY2AND3",
     target_compatible_with = ["@platforms//os:linux"],
     deps = ["@opencv_contrib_nonfree_amd64//:python_opencv"],
 )
diff --git a/debian/BUILD b/debian/BUILD
index 348163e..0ea9836 100644
--- a/debian/BUILD
+++ b/debian/BUILD
@@ -116,8 +116,6 @@
         "download_packages.py",
     ],
     main = "download_packages.py",
-    python_version = "PY3",
-    srcs_version = "PY2AND3",
     target_compatible_with = ["@platforms//os:linux"],
 )
 
diff --git a/third_party/googletest/googlemock/test/BUILD.bazel b/third_party/googletest/googlemock/test/BUILD.bazel
index 2c3f5dc..882a423 100644
--- a/third_party/googletest/googlemock/test/BUILD.bazel
+++ b/third_party/googletest/googlemock/test/BUILD.bazel
@@ -107,7 +107,6 @@
         ":gmock_output_test_",
         ":gmock_output_test_golden.txt",
     ],
-    python_version = "PY2",
     tags = [
         "no_test_msvc2015",
         "no_test_msvc2017",
diff --git a/tools/python/BUILD b/tools/python/BUILD
index 2181b31..32093d3 100644
--- a/tools/python/BUILD
+++ b/tools/python/BUILD
@@ -1,9 +1,31 @@
+load("@rules_python//python:defs.bzl", "py_runtime_pair")
+
 py_runtime(
-    name = "runtime",
+    name = "python3_runtime",
     files = [
         "runtime_binary.sh",
         "@python_repo//:all_files",
     ],
     interpreter = "runtime_binary.sh",
-    visibility = ["//visibility:public"],
+    python_version = "PY3",
+)
+
+py_runtime_pair(
+    name = "py_runtime",
+    py2_runtime = None,
+    py3_runtime = ":python3_runtime",
+)
+
+toolchain(
+    name = "python_toolchain",
+    target_compatible_with = [
+        "@platforms//cpu:x86_64",
+        "@platforms//os:linux",
+    ],
+    exec_compatible_with = [
+        "@platforms//cpu:x86_64",
+        "@platforms//os:linux",
+    ],
+    toolchain = ":py_runtime",
+    toolchain_type = "@rules_python//python:toolchain_type",
 )
diff --git a/y2018/control_loops/python/BUILD b/y2018/control_loops/python/BUILD
index 3c75b97..26b2da4 100644
--- a/y2018/control_loops/python/BUILD
+++ b/y2018/control_loops/python/BUILD
@@ -172,8 +172,6 @@
         "graph_generate.py",
     ],
     legacy_create_init = False,
-    python_version = "PY3",
-    srcs_version = "PY3",
     target_compatible_with = ["@platforms//cpu:x86_64"],
     deps = [
         ":python_init",
@@ -190,8 +188,6 @@
         "graph_generate.py",
     ],
     legacy_create_init = False,
-    python_version = "PY2",
-    srcs_version = "PY2",
     target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":python_init",
diff --git a/y2020/vision/sift/BUILD b/y2020/vision/sift/BUILD
index 98c6ec6..93e2858 100644
--- a/y2020/vision/sift/BUILD
+++ b/y2020/vision/sift/BUILD
@@ -25,8 +25,6 @@
         "@amd64_debian_sysroot//:sysroot_files",
     ],
     main = "fast_gaussian_runner.py",
-    python_version = "PY3",
-    srcs_version = "PY2AND3",
     target_compatible_with = ["@platforms//os:linux"],
     toolchains = [
         "@bazel_tools//tools/cpp:current_cc_toolchain",
@@ -220,8 +218,6 @@
 py_binary(
     name = "demo_sift_training",
     srcs = ["demo_sift_training.py"],
-    python_version = "PY3",
-    srcs_version = "PY2AND3",
     target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":sift_fbs_python",
diff --git a/y2020/vision/tools/python_code/BUILD b/y2020/vision/tools/python_code/BUILD
index fc7322d..1e27f65 100644
--- a/y2020/vision/tools/python_code/BUILD
+++ b/y2020/vision/tools/python_code/BUILD
@@ -61,7 +61,6 @@
         "test_images/*.png",
     ]),
     main = "target_definition.py",
-    python_version = "PY3",
     target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":target_definition",
@@ -80,7 +79,6 @@
     data = glob(["calib_files/*.json"]) + glob([
         "test_images/*.png",
     ]),
-    python_version = "PY3",
     target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":camera_definition",
@@ -100,7 +98,6 @@
     data = glob(["calib_files/*.json"]) + glob([
         "test_images/*.png",
     ]),
-    python_version = "PY3",
     target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":camera_definition",
@@ -156,8 +153,6 @@
         "test_images/*.png",
     ]),
     main = "load_sift_training.py",
-    python_version = "PY3",
-    srcs_version = "PY2AND3",
     target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":load_sift_training",