Skip to content

Commit af68cce

Browse files
committed
consolidate the config and group libraries
1 parent e5916e7 commit af68cce

File tree

11 files changed

+136
-66
lines changed

11 files changed

+136
-66
lines changed

examples/pip_parse_vendored/requirements.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def install_deps(**whl_library_kwargs):
9191
for requirement in group_requirements
9292
}
9393

94-
group_repo = "my_project_pip_deps_vendored__groups"
94+
group_repo = "my_project_pip_deps_vendored__config"
9595
group_library(
9696
name = group_repo,
9797
repo_prefix = "my_project_pip_deps_vendored_",

python/pip_install/pip_repository.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414

1515
""
1616

17-
load("//python/private/pypi:group_library.bzl", _group_library = "group_library")
1817
load("//python/private/pypi:package_annotation.bzl", _package_annotation = "package_annotation")
1918
load("//python/private/pypi:pip_repository.bzl", _pip_repository = "pip_repository")
2019
load("//python/private/pypi:whl_config_repository.bzl", _whl_config_repository = "whl_config_repository")
2120
load("//python/private/pypi:whl_library.bzl", _whl_library = "whl_library")
2221

2322
# Re-exports for backwards compatibility
24-
group_library = _group_library
23+
group_library = _whl_config_repository
2524
pip_repository = _pip_repository
2625
whl_library = _whl_library
2726
whl_config_repository = _whl_config_repository

python/private/pypi/group_library.bzl

Lines changed: 0 additions & 40 deletions
This file was deleted.

python/private/pypi/pip_repository.bzl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,24 +203,19 @@ def _pip_repository_impl(rctx):
203203

204204
rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS)
205205
rctx.template("requirements.bzl", rctx.attr._template, substitutions = {
206-
" # %%CONFIG_LIBRARY%%": """\
206+
" # %%CONFIG_REPO%%": """\
207207
config_repo = "{name}__config"
208208
whl_config_repository(
209209
name = config_repo,
210+
repo_prefix = "{name}_",
211+
groups = all_requirement_groups,
210212
whl_map = {{
211213
p: ""
212214
for p in all_whl_requirements_by_package
213215
}},
214216
)""".format(name = rctx.attr.name) if not rctx.attr.use_hub_alias_dependencies else """\
215217
config_repo = "{name}"
216218
""".format(name = rctx.attr.name),
217-
" # %%GROUP_LIBRARY%%": """\
218-
group_repo = "{name}__groups"
219-
group_library(
220-
name = group_repo,
221-
repo_prefix = "{name}_",
222-
groups = all_requirement_groups,
223-
)""".format(name = rctx.attr.name) if not rctx.attr.use_hub_alias_dependencies else "",
224219
"%%ALL_DATA_REQUIREMENTS%%": render.list([
225220
macro_tmpl.format(p, "data")
226221
for p in bzl_packages

python/private/pypi/requirements.bzl.tmpl.workspace

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ def install_deps(**whl_library_kwargs):
5252
for requirement in group_requirements
5353
}
5454

55-
# %%GROUP_LIBRARY%%
56-
57-
# %%CONFIG_LIBRARY%%
55+
# %%CONFIG_REPO%%
5856

5957
# Install wheels which may be participants in a group
6058
whl_config = dict(_config)

python/private/pypi/whl_config_repository.bzl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
""
1+
"""whl_config_library implementation for WORKSPACE setups."""
22

33
load("//python/private:text_util.bzl", "render")
4+
load(":generate_group_library_build_bazel.bzl", "generate_group_library_build_bazel")
45

56
def _impl(rctx):
7+
build_file_contents = generate_group_library_build_bazel(
8+
repo_prefix = rctx.attr.repo_prefix,
9+
groups = rctx.attr.groups,
10+
)
11+
rctx.file("_groups/BUILD.bazel", build_file_contents)
612
rctx.file("BUILD.bazel", "")
713
rctx.template(
814
"config.bzl",
915
rctx.attr._config_template,
1016
substitutions = {
11-
"%%PACKAGES%%": render.dict(rctx.attr.whl_map, value_repr = lambda x: "None"),
17+
"%%PACKAGES%%": render.dict(rctx.attr.whl_map or {}, value_repr = lambda x: "None"),
1218
},
1319
)
1420

1521
whl_config_repository = repository_rule(
1622
attrs = {
23+
"groups": attr.string_list_dict(
24+
doc = "A mapping of group names to requirements within that group.",
25+
),
26+
"repo_prefix": attr.string(
27+
doc = "Prefix used for the whl_library created components of each group",
28+
),
1729
"whl_map": attr.string_dict(
1830
mandatory = True,
1931
doc = """\
@@ -25,6 +37,11 @@ in the pip.parse tag class.
2537
default = ":config.bzl.tmpl.bzlmod",
2638
),
2739
},
28-
doc = """A rule for WORKSPACE to ensure correct whl_repository configuration. PRIVATE USE ONLY.""",
40+
doc = """
41+
Create a package containing only wrapper py_library and whl_library rules for implementing dependency groups.
42+
This is an implementation detail of dependency groups and should not be used alone.
43+
44+
PRIVATE USE ONLY, only used in WORKSPACE.
45+
""",
2946
implementation = _impl,
3047
)

python/private/pypi/whl_library_targets.bzl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,18 @@ def whl_library_targets(
274274
if group_name and "//:" in dep_template:
275275
# This is the legacy behaviour where the group library is outside the hub repo
276276
label_tmpl = dep_template.format(
277-
name = "_groups",
277+
name = "_config",
278278
target = normalize_name(group_name) + "_{}",
279+
).replace(
280+
"//:",
281+
"//_groups:",
279282
)
280283
impl_vis = [dep_template.format(
281-
name = "_groups",
284+
name = "_config",
282285
target = "__pkg__",
286+
).replace(
287+
"//:",
288+
"//_groups:",
283289
)]
284290

285291
native.alias(

tests/pypi/extension/extension_tests.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def _test_simple(env):
168168
}})
169169
pypi.whl_libraries().contains_exactly({
170170
"pypi_315_simple": {
171+
"config_load": "@pypi//:config.bzl",
171172
"dep_template": "@pypi//{name}:{target}",
172173
"python_interpreter_target": "unit_test_interpreter_target",
173174
"requirement": "simple==0.0.1 --hash=sha256:deadbeef --hash=sha256:deadbaaf",

tests/pypi/generate_whl_library_build_bazel/generate_whl_library_build_bazel_tests.bzl

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ whl_library_targets(
3737
"exclude_via_attr",
3838
"data_exclude_all",
3939
],
40-
dep_template = "@pypi//{name}:{target}",
40+
dep_template = "@pypi_{name}//:{target}",
4141
dependencies = ["foo"],
4242
dependencies_by_platform = {
4343
"baz": ["bar"],
@@ -59,7 +59,7 @@ whl_library_targets(
5959
# SOMETHING SPECIAL AT THE END
6060
"""
6161
actual = generate_whl_library_build_bazel(
62-
dep_template = "@pypi//{name}:{target}",
62+
dep_template = "@pypi_{name}//:{target}",
6363
name = "foo.whl",
6464
dependencies = ["foo"],
6565
dependencies_by_platform = {"baz": ["bar"]},
@@ -83,9 +83,74 @@ whl_library_targets(
8383

8484
_tests.append(_test_all_legacy)
8585

86+
def _test_all_workspace(env):
87+
want = """\
88+
load("@pypi//:config.bzl", "packages")
89+
load("@rules_python//python/private/pypi:whl_library_targets.bzl", "whl_library_targets_from_requires")
90+
91+
package(default_visibility = ["//visibility:public"])
92+
93+
whl_library_targets_from_requires(
94+
copy_executables = {
95+
"exec_src": "exec_dest",
96+
},
97+
copy_files = {
98+
"file_src": "file_dest",
99+
},
100+
data = ["extra_target"],
101+
data_exclude = [
102+
"exclude_via_attr",
103+
"data_exclude_all",
104+
],
105+
dep_template = "@pypi//{name}:{target}",
106+
entry_points = {
107+
"foo": "bar.py",
108+
},
109+
group_deps = [
110+
"foo",
111+
"fox",
112+
"qux",
113+
],
114+
group_name = "qux",
115+
include = packages,
116+
name = "foo.whl",
117+
requires_dist = [
118+
"foo",
119+
"bar-baz",
120+
"qux",
121+
],
122+
srcs_exclude = ["srcs_exclude_all"],
123+
)
124+
125+
# SOMETHING SPECIAL AT THE END
126+
"""
127+
actual = generate_whl_library_build_bazel(
128+
dep_template = "@pypi//{name}:{target}",
129+
name = "foo.whl",
130+
requires_dist = ["foo", "bar-baz", "qux"],
131+
entry_points = {
132+
"foo": "bar.py",
133+
},
134+
data_exclude = ["exclude_via_attr"],
135+
annotation = struct(
136+
copy_files = {"file_src": "file_dest"},
137+
copy_executables = {"exec_src": "exec_dest"},
138+
data = ["extra_target"],
139+
data_exclude_glob = ["data_exclude_all"],
140+
srcs_exclude_glob = ["srcs_exclude_all"],
141+
additive_build_content = """# SOMETHING SPECIAL AT THE END""",
142+
),
143+
config_load = "@pypi//:config.bzl",
144+
group_name = "qux",
145+
group_deps = ["foo", "fox", "qux"],
146+
)
147+
env.expect.that_str(actual.replace("@@", "@")).equals(want)
148+
149+
_tests.append(_test_all_workspace)
150+
86151
def _test_all(env):
87152
want = """\
88-
load("@pypi//:requirements.bzl", "packages")
153+
load("@pypi//:config.bzl", "packages")
89154
load("@rules_python//python/private/pypi:whl_library_targets.bzl", "whl_library_targets_from_requires")
90155
91156
package(default_visibility = ["//visibility:public"])
@@ -140,6 +205,7 @@ whl_library_targets_from_requires(
140205
srcs_exclude_glob = ["srcs_exclude_all"],
141206
additive_build_content = """# SOMETHING SPECIAL AT THE END""",
142207
),
208+
config_load = "@pypi//:config.bzl",
143209
group_name = "qux",
144210
group_deps = ["foo", "fox", "qux"],
145211
)
@@ -149,7 +215,7 @@ _tests.append(_test_all)
149215

150216
def _test_all_with_loads(env):
151217
want = """\
152-
load("@pypi//:requirements.bzl", "packages")
218+
load("@pypi//:config.bzl", "packages")
153219
load("@rules_python//python/private/pypi:whl_library_targets.bzl", "whl_library_targets_from_requires")
154220
155221
package(default_visibility = ["//visibility:public"])
@@ -205,6 +271,7 @@ whl_library_targets_from_requires(
205271
additive_build_content = """# SOMETHING SPECIAL AT THE END""",
206272
),
207273
group_name = "qux",
274+
config_load = "@pypi//:config.bzl",
208275
group_deps = ["foo", "fox", "qux"],
209276
)
210277
env.expect.that_str(actual.replace("@@", "@")).equals(want)

0 commit comments

Comments
 (0)