Skip to content

Commit

Permalink
Add macros for unpacking wheels and using the wheels content in py_li…
Browse files Browse the repository at this point in the history
…brary.

PiperOrigin-RevId: 688169758
  • Loading branch information
tensorflower-gardener authored and copybara-github committed Oct 21, 2024
1 parent 05e85cf commit cdd69db
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions opensource_only.files
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ third_party/py/python_init_repositories.bzl:
third_party/py/python_init_rules.bzl:
third_party/py/python_init_toolchains.bzl:
third_party/py/python_repo.bzl:
third_party/py/python_wheel_library.bzl:
third_party/pybind11.BUILD:
third_party/pybind11_bazel/BUILD:
third_party/python_runtime/BUILD:
Expand Down
39 changes: 39 additions & 0 deletions third_party/py/python_wheel_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
""" Macros to unpack a wheel and use its content as a py_library. """

def _unpacked_wheel_impl(ctx):
output_dir = ctx.actions.declare_directory(ctx.label.name)
ctx.actions.run(
inputs = [ctx.file.wheel],
outputs = [output_dir],
executable = ctx.executable.zipper,
arguments = ["x", ctx.file.wheel.path, "-d", output_dir.path],
)
return [
DefaultInfo(files = depset([output_dir])),
]

_unpacked_wheel = rule(
implementation = _unpacked_wheel_impl,
attrs = {
"wheel": attr.label(mandatory = True, allow_single_file = True),
"zipper": attr.label(
default = Label("@bazel_tools//tools/zip:zipper"),
cfg = "exec",
executable = True,
),
},
)

def wheel_library(name, wheel, deps):
unpacked_wheel_name = name + "_unpacked_wheel"
_unpacked_wheel(
name = unpacked_wheel_name,
wheel = wheel,
)
native.py_library(
name = name,
data = [":" + unpacked_wheel_name],
imports = [unpacked_wheel_name],
deps = deps,
visibility = ["//visibility:public"],
)

0 comments on commit cdd69db

Please sign in to comment.