8000 refactor: move all re-exports to private/reexports.bzl by mattem · Pull Request #739 · bazel-contrib/rules_python · GitHub
[go: up one dir, main page]

Skip to content

refactor: move all re-exports to private/reexports.bzl #739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 19 additions & 58 deletions python/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,28 @@
# 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.
"""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment above feels like it should go to the new file, and the comment here should just be "public API" or sth

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.

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]

Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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
56 changes: 56 additions & 0 deletions python/private/reexports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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.
Expand All @@ -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))
0