Start migrating to upstream pip package management

Rather than packaging up Debian packages, we can start using upstream
wheels.

There are still some issues to figure out. The biggest one is how to
make the upstream packages reproducible. Some packages like numpy are
distributed as wheels which should address the majority of concerns.
Some packages are only distributed in source form though. Those will
be compiled by rules_python against the host system. We'll need to
find a better way to deal with those.

For now you can use the new Python setup if you use
`--config=k8_upstream_python`. Once the majority of the issues are
fixed, I will get rid of the special casing. Then you'll only need
`--config=k8`.

Change-Id: I982ea057e0f47cdfda3d11d58275d7c07e34975a
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
diff --git a/tools/python/repo_defs.bzl b/tools/python/repo_defs.bzl
new file mode 100644
index 0000000..c0c212b
--- /dev/null
+++ b/tools/python/repo_defs.bzl
@@ -0,0 +1,33 @@
+def _pip_configure_impl(repository_ctx):
+    """Runs tools/python/pip_configure.py."""
+    script_path = repository_ctx.path(repository_ctx.attr._script).realpath
+    interpreter_path = repository_ctx.path(repository_ctx.attr._interpreter).realpath
+    requirements_path = repository_ctx.path(repository_ctx.attr._requirements).realpath
+
+    script_result = repository_ctx.execute([
+        interpreter_path,
+        "-BSs",
+        script_path,
+        requirements_path,
+    ])
+    if script_result.return_code != 0:
+        fail("{} failed: {} ({})".format(
+            script_path,
+            script_result.stdout,
+            script_result.stderr,
+        ))
+
+pip_configure = repository_rule(
+    implementation = _pip_configure_impl,
+    attrs = {
+        "_interpreter": attr.label(
+            default = "@python3_9_x86_64-unknown-linux-gnu//:bin/python3",
+        ),
+        "_script": attr.label(
+            default = "@//tools/python:pip_configure.py",
+        ),
+        "_requirements": attr.label(
+            default = "@//tools/python:requirements.txt",
+        ),
+    },
+)