aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Levasseur <rlevasseur@google.com>2023-03-23 12:59:58 -0700
committerGitHub <noreply@github.com>2023-03-23 12:59:58 -0700
commit3b9c85e7a466f5f904929ecfcfb11f0c444fef16 (patch)
treebcc215b7c006ec5b05a9239b5316102184cd60ed
parentc394c46fc1b21853bc68a7a47c1fe2db828d1dd0 (diff)
downloadbazelbuild-rules_python-3b9c85e7a466f5f904929ecfcfb11f0c444fef16.tar.gz
cleanup: factor reexports.bzl into the respective implementation files (#1137)
This helps avoid loading one rule requiring loading everything. Within Google, this makes some Starlark testing frameworks avoid having to mock far away dependencies of dependencies.
-rw-r--r--python/BUILD.bazel18
-rw-r--r--python/defs.bzl17
-rw-r--r--python/private/BUILD.bazel6
-rw-r--r--python/private/reexports.bzl136
-rw-r--r--python/private/util.bzl12
-rw-r--r--python/py_binary.bzl16
-rw-r--r--python/py_library.bzl14
-rw-r--r--python/py_runtime.bzl14
-rw-r--r--python/py_runtime_pair.bzl72
-rw-r--r--python/py_test.bzl16
10 files changed, 159 insertions, 162 deletions
diff --git a/python/BUILD.bazel b/python/BUILD.bazel
index a524d2f..2582d73 100644
--- a/python/BUILD.bazel
+++ b/python/BUILD.bazel
@@ -56,9 +56,15 @@ bzl_library(
visibility = ["//visibility:public"],
deps = [
":current_py_toolchain_bzl",
+ ":py_binary_bzl",
":py_import_bzl",
+ ":py_info_bzl",
+ ":py_library_bzl",
+ ":py_runtime_bzl",
+ ":py_runtime_info_bzl",
+ ":py_runtime_pair_bzl",
+ ":py_test_bzl",
"//python/private:bazel_tools_bzl",
- "//python/private:reexports_bzl",
],
)
@@ -76,7 +82,7 @@ bzl_library(
bzl_library(
name = "py_binary_bzl",
srcs = ["py_binary.bzl"],
- deps = ["//python/private:reexports_bzl"],
+ deps = ["//python/private:util_bzl"],
)
bzl_library(
@@ -99,19 +105,19 @@ bzl_library(
bzl_library(
name = "py_library_bzl",
srcs = ["py_library.bzl"],
- deps = ["//python/private:reexports_bzl"],
+ deps = ["//python/private:util_bzl"],
)
bzl_library(
name = "py_runtime_bzl",
srcs = ["py_runtime.bzl"],
- deps = ["//python/private:reexports_bzl"],
+ deps = ["//python/private:util_bzl"],
)
bzl_library(
name = "py_runtime_pair_bzl",
srcs = ["py_runtime_pair.bzl"],
- deps = ["//python/private:reexports_bzl"],
+ deps = ["//python/private:bazel_tools_bzl"],
)
bzl_library(
@@ -123,7 +129,7 @@ bzl_library(
bzl_library(
name = "py_test_bzl",
srcs = ["py_test.bzl"],
- deps = ["//python/private:reexports_bzl"],
+ deps = ["//python/private:util_bzl"],
)
# NOTE: Remember to add bzl_library targets to //tests:bzl_libraries
diff --git a/python/defs.bzl b/python/defs.bzl
index ec70c1b..6ded66a 100644
--- a/python/defs.bzl
+++ b/python/defs.bzl
@@ -14,16 +14,13 @@
"""Core rules for building Python projects."""
load("@bazel_tools//tools/python:srcs_version.bzl", _find_requirements = "find_requirements")
-load(
- "//python/private:reexports.bzl",
- "internal_PyInfo",
- "internal_PyRuntimeInfo",
- _py_binary = "py_binary",
- _py_library = "py_library",
- _py_runtime = "py_runtime",
- _py_runtime_pair = "py_runtime_pair",
- _py_test = "py_test",
-)
+load("//python:py_binary.bzl", _py_binary = "py_binary")
+load("//python:py_info.bzl", internal_PyInfo = "PyInfo")
+load("//python:py_library.bzl", _py_library = "py_library")
+load("//python:py_runtime.bzl", _py_runtime = "py_runtime")
+load("//python:py_runtime_info.bzl", internal_PyRuntimeInfo = "PyRuntimeInfo")
+load("//python:py_runtime_pair.bzl", _py_runtime_pair = "py_runtime_pair")
+load("//python:py_test.bzl", _py_test = "py_test")
load(":current_py_toolchain.bzl", _current_py_toolchain = "current_py_toolchain")
load(":py_import.bzl", _py_import = "py_import")
diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel
index 21e3c16..4068ea4 100644
--- a/python/private/BUILD.bazel
+++ b/python/private/BUILD.bazel
@@ -41,6 +41,12 @@ bzl_library(
deps = [":bazel_tools_bzl"],
)
+bzl_library(
+ name = "util_bzl",
+ srcs = ["util.bzl"],
+ visibility = ["//python:__subpackages__"],
+)
+
# @bazel_tools can't define bzl_library itself, so we just put a wrapper around it.
bzl_library(
name = "bazel_tools_bzl",
diff --git a/python/private/reexports.bzl b/python/private/reexports.bzl
index 987187c..a300a20 100644
--- a/python/private/reexports.bzl
+++ b/python/private/reexports.bzl
@@ -37,20 +37,6 @@ different name. Then we can load it from defs.bzl and export it there under
the original name.
"""
-load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")
-
-# The implementation of the macros and tagging mechanism follows the example
-# set by rules_cc and rules_java.
-
-_MIGRATION_TAG = "__PYTHON_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"
-
-def _add_tags(attrs):
- if "tags" in attrs and attrs["tags"] != None:
- attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG]
- else:
- attrs["tags"] = [_MIGRATION_TAG]
- return attrs
-
# Don't use underscore prefix, since that would make the symbol local to this
# file only. Use a non-conventional name to emphasize that this is not a public
# symbol.
@@ -59,125 +45,3 @@ internal_PyInfo = PyInfo
# buildifier: disable=name-conventions
internal_PyRuntimeInfo = PyRuntimeInfo
-
-def py_library(**attrs):
- """See the Bazel core [py_library](https://docs.bazel.build/versions/master/be/python.html#py_library) documentation.
-
- Args:
- **attrs: Rule attributes
- """
- if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
- fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
-
- # buildifier: disable=native-python
- native.py_library(**_add_tags(attrs))
-
-def py_binary(**attrs):
- """See the Bazel core [py_binary](https://docs.bazel.build/versions/master/be/python.html#py_binary) documentation.
-
- Args:
- **attrs: Rule attributes
- """
- if attrs.get("python_version") == "PY2":
- fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
- if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
- fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
-
- # buildifier: disable=native-python
- native.py_binary(**_add_tags(attrs))
-
-def py_test(**attrs):
- """See the Bazel core [py_test](https://docs.bazel.build/versions/master/be/python.html#py_test) documentation.
-
- Args:
- **attrs: Rule attributes
- """
- if attrs.get("python_version") == "PY2":
- fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
- if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
- fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
-
- # buildifier: disable=native-python
- native.py_test(**_add_tags(attrs))
-
-def py_runtime(**attrs):
- """See the Bazel core [py_runtime](https://docs.bazel.build/versions/master/be/python.html#py_runtime) documentation.
-
- Args:
- **attrs: Rule attributes
- """
- if attrs.get("python_version") == "PY2":
- fail("Python 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
-
- # buildifier: disable=native-python
- native.py_runtime(**_add_tags(attrs))
-
-# NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our
-# doc generator gives useful API docs.
-def py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs):
- """A toolchain rule for Python.
-
- This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3.
- However, Python 2 is no longer supported, so it now only wraps a single Python 3
- runtime.
-
- Usually the wrapped runtimes are declared using the `py_runtime` rule, but any
- rule returning a `PyRuntimeInfo` provider may be used.
-
- This rule returns a `platform_common.ToolchainInfo` provider with the following
- schema:
-
- ```python
- platform_common.ToolchainInfo(
- py2_runtime = None,
- py3_runtime = <PyRuntimeInfo or None>,
- )
- ```
-
- Example usage:
-
- ```python
- # In your BUILD file...
-
- load("@rules_python//python:defs.bzl", "py_runtime_pair")
-
- py_runtime(
- name = "my_py3_runtime",
- interpreter_path = "/system/python3",
- python_version = "PY3",
- )
-
- py_runtime_pair(
- name = "my_py_runtime_pair",
- py3_runtime = ":my_py3_runtime",
- )
-
- toolchain(
- name = "my_toolchain",
- target_compatible_with = <...>,
- toolchain = ":my_py_runtime_pair",
- toolchain_type = "@rules_python//python:toolchain_type",
- )
- ```
-
- ```python
- # In your WORKSPACE...
-
- register_toolchains("//my_pkg:my_toolchain")
- ```
-
- Args:
- name: str, the name of the target
- py2_runtime: optional Label; must be unset or None; an error is raised
- otherwise.
- py3_runtime: Label; a target with `PyRuntimeInfo` for Python 3.
- **attrs: Extra attrs passed onto the native rule
- """
- if attrs.get("py2_runtime"):
- fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
- _py_runtime_pair(
- name = name,
- py2_runtime = py2_runtime,
- py3_runtime = py3_runtime,
- **attrs
- )
diff --git a/python/private/util.bzl b/python/private/util.bzl
index 8ea1f49..25a50aa 100644
--- a/python/private/util.bzl
+++ b/python/private/util.bzl
@@ -29,3 +29,15 @@ def copy_propagating_kwargs(from_kwargs, into_kwargs = None):
if attr in from_kwargs and attr not in into_kwargs:
into_kwargs[attr] = from_kwargs[attr]
return into_kwargs
+
+# The implementation of the macros and tagging mechanism follows the example
+# set by rules_cc and rules_java.
+
+_MIGRATION_TAG = "__PYTHON_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"
+
+def add_migration_tag(attrs):
+ if "tags" in attrs and attrs["tags"] != None:
+ attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG]
+ else:
+ attrs["tags"] = [_MIGRATION_TAG]
+ return attrs
diff --git a/python/py_binary.bzl b/python/py_binary.bzl
index 9d145d8..6b6f7e0 100644
--- a/python/py_binary.bzl
+++ b/python/py_binary.bzl
@@ -14,6 +14,18 @@
"""Public entry point for py_binary."""
-load("//python/private:reexports.bzl", _py_binary = "py_binary")
+load("//python/private:util.bzl", "add_migration_tag")
-py_binary = _py_binary
+def py_binary(**attrs):
+ """See the Bazel core [py_binary](https://docs.bazel.build/versions/master/be/python.html#py_binary) documentation.
+
+ Args:
+ **attrs: Rule attributes
+ """
+ if attrs.get("python_version") == "PY2":
+ fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
+ if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
+ fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
+
+ # buildifier: disable=native-python
+ native.py_binary(**add_migration_tag(attrs))
diff --git a/python/py_library.bzl b/python/py_library.bzl
index 1aff68c..d54cbb2 100644
--- a/python/py_library.bzl
+++ b/python/py_library.bzl
@@ -14,6 +14,16 @@
"""Public entry point for py_library."""
-load("//python/private:reexports.bzl", _py_library = "py_library")
+load("//python/private:util.bzl", "add_migration_tag")
-py_library = _py_library
+def py_library(**attrs):
+ """See the Bazel core [py_library](https://docs.bazel.build/versions/master/be/python.html#py_library) documentation.
+
+ Args:
+ **attrs: Rule attributes
+ """
+ if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
+ fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
+
+ # buildifier: disable=native-python
+ native.py_library(**add_migration_tag(attrs))
diff --git a/python/py_runtime.bzl b/python/py_runtime.bzl
index 5e80308..b70f9d4 100644
--- a/python/py_runtime.bzl
+++ b/python/py_runtime.bzl
@@ -14,6 +14,16 @@
"""Public entry point for py_runtime."""
-load("//python/private:reexports.bzl", _py_runtime = "py_runtime")
+load("//python/private:util.bzl", "add_migration_tag")
-py_runtime = _py_runtime
+def py_runtime(**attrs):
+ """See the Bazel core [py_runtime](https://docs.bazel.build/versions/master/be/python.html#py_runtime) documentation.
+
+ Args:
+ **attrs: Rule attributes
+ """
+ if attrs.get("python_version") == "PY2":
+ fail("Python 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
+
+ # buildifier: disable=native-python
+ native.py_runtime(**add_migration_tag(attrs))
diff --git a/python/py_runtime_pair.bzl b/python/py_runtime_pair.bzl
index 3f3ecf4..951c606 100644
--- a/python/py_runtime_pair.bzl
+++ b/python/py_runtime_pair.bzl
@@ -14,6 +14,74 @@
"""Public entry point for py_runtime_pair."""
-load("//python/private:reexports.bzl", _py_runtime_pair = "py_runtime_pair")
+load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair")
-py_runtime_pair = _py_runtime_pair
+# NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our
+# doc generator gives useful API docs.
+def py_runtime_pair(name, py2_runtime = None, py3_runtime = None, **attrs):
+ """A toolchain rule for Python.
+
+ This used to wrap up to two Python runtimes, one for Python 2 and one for Python 3.
+ However, Python 2 is no longer supported, so it now only wraps a single Python 3
+ runtime.
+
+ Usually the wrapped runtimes are declared using the `py_runtime` rule, but any
+ rule returning a `PyRuntimeInfo` provider may be used.
+
+ This rule returns a `platform_common.ToolchainInfo` provider with the following
+ schema:
+
+ ```python
+ platform_common.ToolchainInfo(
+ py2_runtime = None,
+ py3_runtime = <PyRuntimeInfo or None>,
+ )
+ ```
+
+ Example usage:
+
+ ```python
+ # In your BUILD file...
+
+ load("@rules_python//python:defs.bzl", "py_runtime_pair")
+
+ py_runtime(
+ name = "my_py3_runtime",
+ interpreter_path = "/system/python3",
+ python_version = "PY3",
+ )
+
+ py_runtime_pair(
+ name = "my_py_runtime_pair",
+ py3_runtime = ":my_py3_runtime",
+ )
+
+ toolchain(
+ name = "my_toolchain",
+ target_compatible_with = <...>,
+ toolchain = ":my_py_runtime_pair",
+ toolchain_type = "@rules_python//python:toolchain_type",
+ )
+ ```
+
+ ```python
+ # In your WORKSPACE...
+
+ register_toolchains("//my_pkg:my_toolchain")
+ ```
+
+ Args:
+ name: str, the name of the target
+ py2_runtime: optional Label; must be unset or None; an error is raised
+ otherwise.
+ py3_runtime: Label; a target with `PyRuntimeInfo` for Python 3.
+ **attrs: Extra attrs passed onto the native rule
+ """
+ if attrs.get("py2_runtime"):
+ fail("PYthon 2 is no longer supported: see https://github.com/bazelbuild/rules_python/issues/886")
+ _py_runtime_pair(
+ name = name,
+ py2_runtime = py2_runtime,
+ py3_runtime = py3_runtime,
+ **attrs
+ )
diff --git a/python/py_test.bzl b/python/py_test.bzl
index 84470bc..09580c0 100644
--- a/python/py_test.bzl
+++ b/python/py_test.bzl
@@ -14,6 +14,18 @@
"""Public entry point for py_test."""
-load("//python/private:reexports.bzl", _py_test = "py_test")
+load("//python/private:util.bzl", "add_migration_tag")
-py_test = _py_test
+def py_test(**attrs):
+ """See the Bazel core [py_test](https://docs.bazel.build/versions/master/be/python.html#py_test) documentation.
+
+ Args:
+ **attrs: Rule attributes
+ """
+ if attrs.get("python_version") == "PY2":
+ fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
+ if attrs.get("srcs_version") in ("PY2", "PY2ONLY"):
+ fail("Python 2 is no longer supported: https://github.com/bazelbuild/rules_python/issues/886")
+
+ # buildifier: disable=native-python
+ native.py_test(**add_migration_tag(attrs))