From ce92b39b315f2ab8e987bd43ba15e8654a0c9264 Mon Sep 17 00:00:00 2001 From: Matt Mackay Date: Thu, 30 Jun 2022 10:22:26 -0400 Subject: [PATCH] refactor: move all re-exports to private/reexports.bzl --- python/defs.bzl | 77 +++++++++--------------------------- python/private/reexports.bzl | 56 ++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 58 deletions(-) diff --git a/python/defs.bzl b/python/defs.bzl index f3a74eb8e2..88f28c5fc0 100644 --- a/python/defs.bzl +++ b/python/defs.bzl @@ -12,16 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Core rules for building Python projects. - -Currently the definitions here are re-exports of the native rules, "blessed" to -work under `--incompatible_load_python_rules_from_bzl`. As the native rules get -migrated to Starlark, their implementations will be moved here. +""" +Core rules for building Python projects. """ load("@bazel_tools//tools/python:srcs_version.bzl", _find_requirements = "find_requirements") load("@bazel_tools//tools/python:toolchain.bzl", _py_runtime_pair = "py_runtime_pair") -load("//python/private:reexports.bzl", "internal_PyInfo", "internal_PyRuntimeInfo") +load( + "//python/private:reexports.bzl", + "internal_PyInfo", + "internal_PyRuntimeInfo", + _py_binary = "py_binary", + _py_library = "py_library", + _py_runtime = "py_runtime", + _py_test = "py_test", +) # Exports of native-defined providers. @@ -29,18 +34,6 @@ PyInfo = internal_PyInfo PyRuntimeInfo = internal_PyRuntimeInfo -# 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 - def _current_py_toolchain_impl(ctx): toolchain = ctx.toolchains[ctx.attr._toolchain] @@ -84,36 +77,6 @@ current_py_toolchain = rule( ], ) -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 - """ - - # 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 - """ - - # 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 - """ - - # buildifier: disable=native-python - native.py_test(**_add_tags(attrs)) - def _py_import_impl(ctx): # See https://github.com/bazelbuild/bazel/blob/0.24.0/src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java#L104 . import_paths = [ @@ -164,18 +127,16 @@ py_import = rule( }, ) -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 - """ - - # buildifier: disable=native-python - native.py_runtime(**_add_tags(attrs)) - # Re-exports of Starlark-defined symbols in @bazel_tools//tools/python. py_runtime_pair = _py_runtime_pair find_requirements = _find_requirements + +py_library = _py_library + +py_binary = _py_binary + +py_test = _py_test + +py_runtime = _py_runtime diff --git a/python/private/reexports.bzl b/python/private/reexports.bzl index f827564293..6ad9e0cdcc 100644 --- a/python/private/reexports.bzl +++ b/python/private/reexports.bzl @@ -14,6 +14,10 @@ """Internal re-exports of built-in symbols. +Currently the definitions here are re-exports of the native rules, "blessed" to +work under `--incompatible_load_python_rules_from_bzl`. As the native rules get +migrated to Starlark, their implementations will be removed from here. + We want to re-export a built-in symbol as if it were defined in a Starlark file, so that users can for instance do: @@ -33,6 +37,18 @@ different name. Then we can load it from defs.bzl and export it there under the original name. """ +# 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. @@ -41,3 +57,43 @@ 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 + """ + + # 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 + """ + + # 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 + """ + + # 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 + """ + + # buildifier: disable=native-python + native.py_runtime(**_add_tags(attrs))