Skip to content

Commit 74b5781

Browse files
committed
exp(pypi): introduce 'all_whls', 'all_pkgs' and 'all_data' targets to replace requirements
Work towards bazel-contrib#2559
1 parent 1aa0d9f commit 74b5781

File tree

7 files changed

+73
-15
lines changed

7 files changed

+73
-15
lines changed

examples/build_file_generation/BUILD.bazel

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# The `load` statement imports the symbol for the rule, in the defined
44
# ruleset. When the symbol is loaded you can use the rule.
55
load("@bazel_gazelle//:def.bzl", "gazelle")
6-
load("@pip//:requirements.bzl", "all_whl_requirements")
76
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
87
load("@rules_python//python:py_binary.bzl", "py_binary")
98
load("@rules_python//python:py_library.bzl", "py_library")
@@ -28,7 +27,7 @@ modules_mapping(
2827
"^_|(\\._)+", # This is the default.
2928
"(\\.tests)+", # Add a custom one to get rid of the psutil tests.
3029
],
31-
wheels = all_whl_requirements,
30+
wheels = ["@pip//:all_whls"],
3231
)
3332

3433
# Gazelle python extension needs a manifest file mapping from

examples/bzlmod/BUILD.bazel

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# The names @pip and @python_39 are values that are repository
77
# names. Those names are defined in the MODULES.bazel file.
88
load("@bazel_skylib//rules:build_test.bzl", "build_test")
9-
load("@pip//:requirements.bzl", "all_data_requirements", "all_requirements", "all_whl_requirements", "requirement")
9+
load("@pip//:requirements.bzl", "requirement")
1010
load("@python_3_9//:defs.bzl", py_test_with_transition = "py_test")
1111
load("@python_versions//3.10:defs.bzl", compile_pip_requirements_3_10 = "compile_pip_requirements")
1212
load("@rules_python//python:py_binary.bzl", "py_binary")
@@ -72,17 +72,17 @@ py_test_with_transition(
7272
# See: https://github.com/bazelbuild/bazel-skylib/blob/main/docs/build_test_doc.md
7373
build_test(
7474
name = "all_wheels_build_test",
75-
targets = all_whl_requirements,
75+
targets = ["@pip//:all_whls"],
7676
)
7777

7878
build_test(
7979
name = "all_data_requirements_build_test",
80-
targets = all_data_requirements,
80+
targets = ["@pip//:all_data"],
8181
)
8282

8383
build_test(
8484
name = "all_requirements_build_test",
85-
targets = all_requirements,
85+
targets = ["@pip//:all_pkgs"],
8686
)
8787

8888
# Check the annotations API

examples/bzlmod_build_file_generation/BUILD.bazel

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
# The following code loads the base python requirements and gazelle
77
# requirements.
88
load("@bazel_gazelle//:def.bzl", "gazelle")
9-
load("@pip//:requirements.bzl", "all_whl_requirements")
109
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
1110
load("@rules_python//python:py_binary.bzl", "py_binary")
1211
load("@rules_python//python:py_library.bzl", "py_library")
@@ -35,7 +34,7 @@ modules_mapping(
3534
"^tzdata", # Get rid of tzdata on Windows.
3635
"^lazy_object_proxy\\.cext$", # Get rid of this on Linux because it isn't included on Windows.
3736
],
38-
wheels = all_whl_requirements,
37+
wheels = ["@pip//:all_whls"],
3938
)
4039

4140
modules_mapping(
@@ -49,7 +48,7 @@ modules_mapping(
4948
],
5049
include_stub_packages = True,
5150
modules_mapping_name = "modules_mapping_with_types.json",
52-
wheels = all_whl_requirements,
51+
wheels = ["@pip//:all_whls"],
5352
)
5453

5554
# Gazelle python extension needs a manifest file mapping from

gazelle/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,14 @@ Create an empty file with this name. It might be next to your `requirements.txt`
8888
To keep the metadata updated, put this in your `BUILD.bazel` file next to `gazelle_python.yaml`:
8989

9090
```starlark
91-
load("@pip//:requirements.bzl", "all_whl_requirements")
9291
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
9392
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")
9493

9594
# This rule fetches the metadata for python packages we depend on. That data is
9695
# required for the gazelle_python_manifest rule to update our manifest file.
9796
modules_mapping(
9897
name = "modules_map",
99-
wheels = all_whl_requirements,
98+
wheels = ["@pip//all_whls"],
10099
)
101100

102101
# Gazelle python extension needs a manifest file mapping from

gazelle/modules_mapping/def.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ modules_mapping = rule(
6464
),
6565
"wheels": attr.label_list(
6666
allow_files = True,
67-
doc = "The list of wheels, usually the 'all_whl_requirements' from @<pip_repository>//:requirements.bzl",
67+
doc = """The list of wheels, usually the '["@<pip_repository>//:all_whls"]'""",
6868
mandatory = True,
6969
),
7070
"_generator": attr.label(

python/private/pypi/hub_repository.bzl

+33-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,32 @@ load(":render_pkg_aliases.bzl", "render_multiplatform_pkg_aliases")
1919
load(":whl_config_setting.bzl", "whl_config_setting")
2020

2121
_BUILD_FILE_CONTENTS = """\
22+
load("@rules_python//python:py_library.bzl", "py_library")
23+
2224
package(default_visibility = ["//visibility:public"])
2325
2426
# Ensure the `requirements.bzl` source can be accessed by stardoc, since users load() from it
2527
exports_files(["requirements.bzl"])
28+
29+
filegroup(
30+
name = "all_whls",
31+
srcs = {all_whls},
32+
)
33+
34+
filegroup(
35+
name = "all_data",
36+
srcs = {all_data},
37+
)
38+
39+
py_library(
40+
name = "all_pkgs",
41+
deps = {all_pkgs},
42+
)
2643
"""
2744

2845
def _impl(rctx):
29-
bzl_packages = rctx.attr.packages or rctx.attr.whl_map.keys()
46+
all_bzl_packages = sorted(rctx.attr.whl_map.keys())
47+
bzl_packages = rctx.attr.packages or all_bzl_packages
3048
aliases = render_multiplatform_pkg_aliases(
3149
aliases = {
3250
key: _whl_config_settings_from_json(values)
@@ -44,7 +62,20 @@ def _impl(rctx):
4462
# `requirement`, et al. macros.
4563
macro_tmpl = "@@{name}//{{}}:{{}}".format(name = rctx.attr.name)
4664

47-
rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS)
65+
rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS.format(
66+
all_pkgs = render.indent(render.list([
67+
"//" + pkg
68+
for pkg in all_bzl_packages
69+
])).lstrip(),
70+
all_data = render.indent(render.list([
71+
"//{}:data".format(pkg)
72+
for pkg in all_bzl_packages
73+
])).lstrip(),
74+
all_whls = render.indent(render.list([
75+
"//{}:whl".format(pkg)
76+
for pkg in all_bzl_packages
77+
])).lstrip(),
78+
))
4879
rctx.template("requirements.bzl", rctx.attr._template, substitutions = {
4980
"%%ALL_DATA_REQUIREMENTS%%": render.list([
5081
macro_tmpl.format(p, "data")

python/private/pypi/pip_repository.bzl

+31-1
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,27 @@ def use_isolated(ctx, attr):
6464
return use_isolated
6565

6666
_BUILD_FILE_CONTENTS = """\
67+
load("@rules_python//python:py_library.bzl", "py_library")
68+
6769
package(default_visibility = ["//visibility:public"])
6870
6971
# Ensure the `requirements.bzl` source can be accessed by stardoc, since users load() from it
7072
exports_files(["requirements.bzl"])
73+
74+
filegroup(
75+
name = "all_whls",
76+
srcs = {all_whls},
77+
)
78+
79+
filegroup(
80+
name = "all_data",
81+
srcs = {all_data},
82+
)
83+
84+
py_library(
85+
name = "all_pkgs",
86+
deps = {all_pkgs},
87+
)
7188
"""
7289

7390
def _pip_repository_impl(rctx):
@@ -183,7 +200,20 @@ def _pip_repository_impl(rctx):
183200
for path, contents in aliases.items():
184201
rctx.file(path, contents)
185202

186-
rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS)
203+
rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS.format(
204+
all_pkgs = render.indent(render.list([
205+
"//" + pkg
206+
for pkg in bzl_packages
207+
])).lstrip(),
208+
all_data = render.indent(render.list([
209+
"//{}:data".format(pkg)
210+
for pkg in bzl_packages
211+
])).lstrip(),
212+
all_whls = render.indent(render.list([
213+
"//{}:whl".format(pkg)
214+
for pkg in bzl_packages
215+
])).lstrip(),
216+
))
187217
rctx.template("requirements.bzl", rctx.attr._template, substitutions = {
188218
" # %%GROUP_LIBRARY%%": """\
189219
group_repo = "{name}__groups"

0 commit comments

Comments
 (0)