xref: /aosp_15_r20/external/bazelbuild-rules_python/python/private/whl_filegroup/whl_filegroup.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1"""Implementation of whl_filegroup rule."""
2
3def _whl_filegroup_impl(ctx):
4    out_dir = ctx.actions.declare_directory(ctx.attr.name)
5    ctx.actions.run(
6        outputs = [out_dir],
7        inputs = [ctx.file.whl],
8        arguments = [
9            ctx.file.whl.path,
10            out_dir.path,
11            ctx.attr.pattern,
12        ],
13        executable = ctx.executable._extract_wheel_files_tool,
14        mnemonic = "PyExtractWheelFiles",
15        progress_message = "Extracting %s files from %s" % (ctx.attr.pattern, ctx.file.whl.short_path),
16    )
17    return [DefaultInfo(
18        files = depset([out_dir]),
19        runfiles = ctx.runfiles(files = [out_dir] if ctx.attr.runfiles else []),
20    )]
21
22whl_filegroup = rule(
23    _whl_filegroup_impl,
24    doc = """Extract files matching a regular expression from a wheel file.
25
26An empty pattern will match all files.
27
28Example usage:
29```starlark
30load("@rules_cc//cc:defs.bzl", "cc_library")
31load("@rules_python//python:pip.bzl", "whl_filegroup")
32
33whl_filegroup(
34    name = "numpy_includes",
35    pattern = "numpy/core/include/numpy",
36    whl = "@pypi//numpy:whl",
37)
38
39cc_library(
40    name = "numpy_headers",
41    hdrs = [":numpy_includes"],
42    includes = ["numpy_includes/numpy/core/include"],
43    deps = ["@rules_python//python/cc:current_py_cc_headers"],
44)
45```
46""",
47    attrs = {
48        "pattern": attr.string(default = "", doc = "Only file paths matching this regex pattern will be extracted."),
49        "runfiles": attr.bool(default = False, doc = "Whether to include the output TreeArtifact in this target's runfiles."),
50        "whl": attr.label(mandatory = True, allow_single_file = True, doc = "The wheel to extract files from."),
51        "_extract_wheel_files_tool": attr.label(
52            default = Label("//python/private/whl_filegroup:extract_wheel_files"),
53            cfg = "exec",
54            executable = True,
55        ),
56    },
57)
58