Skip to content

Commit a91a147

Browse files
committed
wip
1 parent e6e60ac commit a91a147

11 files changed

+87
-20
lines changed

examples/pip_parse_vendored/requirements.bzl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
load("@rules_python//python:pip.bzl", "pip_utils")
7-
load("@rules_python//python/pip_install:pip_repository.bzl", "group_library", "whl_library")
7+
load("@rules_python//python/pip_install:pip_repository.bzl", "group_library", "whl_config_repository", "whl_library")
88

99
all_requirements = [
1010
"@my_project_pip_deps_vendored_certifi//:pkg",
@@ -32,8 +32,6 @@ all_data_requirements = [
3232
"@my_project_pip_deps_vendored_urllib3//:data",
3333
]
3434

35-
packages = sorted(all_whl_requirements_by_package)
36-
3735
_packages = [
3836
("my_project_pip_deps_vendored_certifi", "certifi==2023.7.22 --hash=sha256:539cc1d13202e33ca466e88b2807e29f4c13049d6d87031a3c110744495cb082 --hash=sha256:92d6037539857d8206b8f6ae472e8b77db8058fec5937a1ef3f54304089edbb9"),
3937
("my_project_pip_deps_vendored_charset_normalizer", "charset-normalizer==2.1.1 --hash=sha256:5a3d016c7c547f69d6f81fb0db9449ce888b418b5b9952cc5e6e66843e9dd845 --hash=sha256:83e9a75d1911279afd89352c68b45348559d1fc0506b054b346651b5e7fee29f"),
@@ -100,6 +98,15 @@ def install_deps(**whl_library_kwargs):
10098
groups = all_requirement_groups,
10199
)
102100

101+
config_repo = "my_project_pip_deps_vendored__config"
102+
whl_config_repository(
103+
name = config_repo,
104+
whl_map = {
105+
p: ""
106+
for p in all_whl_requirements_by_package
107+
},
108+
)
109+
103110
# Install wheels which may be participants in a group
104111
whl_config = dict(_config)
105112
whl_config.update(whl_library_kwargs)
@@ -114,6 +121,6 @@ def install_deps(**whl_library_kwargs):
114121
group_name = group_name,
115122
group_deps = group_deps,
116123
annotation = _get_annotation(requirement),
117-
packages = packages,
124+
config_load = "@{}//:config.bzl".format(config_repo),
118125
**whl_config
119126
)

python/pip_install/pip_repository.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
load("//python/private/pypi:group_library.bzl", _group_library = "group_library")
1818
load("//python/private/pypi:package_annotation.bzl", _package_annotation = "package_annotation")
1919
load("//python/private/pypi:pip_repository.bzl", _pip_repository = "pip_repository")
20+
load("//python/private/pypi:whl_config_repository.bzl", _whl_config_repository = "whl_config_repository")
2021
load("//python/private/pypi:whl_library.bzl", _whl_library = "whl_library")
2122

2223
# Re-exports for backwards compatibility
2324
group_library = _group_library
2425
pip_repository = _pip_repository
2526
whl_library = _whl_library
27+
whl_config_repository = _whl_config_repository
2628
package_annotation = _package_annotation
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Extra configuration values that are exposed from the hub repository for spoke repositories to access.
2+
3+
NOTE: This is internal `rules_python` API and if you would like to depend on it, please raise an issue
4+
with your usecase. This may change in between rules_python versions without any notice.
5+
6+
@generated by rules_python pip.parse bzlmod extension.
7+
"""
8+
9+
packages = %%PACKAGES%%

python/private/pypi/generate_whl_library_build_bazel.bzl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,11 @@ def generate_whl_library_build_bazel(
8383
"target_platforms",
8484
"default_python_version",
8585
]
86-
dep_template = kwargs.get("dep_template")
87-
packages = kwargs.pop("packages", [])
86+
packages_load = kwargs.pop("config_load")
8887
if not kwargs.get("requires_dist"):
8988
# no deps, we can leave the extra loads out
9089
pass
91-
elif packages:
92-
kwargs["include"] = render.list(packages)
9390
else:
94-
packages_load = dep_template.format(name = "", target = "requirements.bzl")
9591
loads.append("""load("{}", "{}")""".format(packages_load, "packages"))
9692
kwargs["include"] = "packages"
9793

python/private/pypi/hub_builder.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ def _common_args(self, module_ctx, *, pip_attr):
451451
# attrs.
452452
whl_library_args = dict(
453453
dep_template = "@{}//{{name}}:{{target}}".format(self.name),
454+
config_load = "@{}//:config.bzl".format(self.name),
454455
)
455456
maybe_args = dict(
456457
# The following values are safe to omit if they have false like values

python/private/pypi/hub_repository.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ def _impl(rctx):
4646
macro_tmpl = "@@{name}//{{}}:{{}}".format(name = rctx.attr.name)
4747

4848
rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS)
49+
rctx.template(
50+
"config.bzl",
51+
rctx.attr._config_template,
52+
substitutions = {
53+
"%%PACKAGES%%": render.dict(rctx.attr.whl_map, value_repr = lambda x: "None"),
54+
},
55+
)
4956
rctx.template("requirements.bzl", rctx.attr._requirements_bzl_template, substitutions = {
5057
"%%ALL_DATA_REQUIREMENTS%%": render.list([
5158
macro_tmpl.format(p, "data")
@@ -92,6 +99,9 @@ The wheel map where values are json.encoded strings of the whl_map constructed
9299
in the pip.parse tag class.
93100
""",
94101
),
102+
"_config_template": attr.label(
103+
default = ":config.bzl.tmpl.bzlmod",
104+
),
95105
"_requirements_bzl_template": attr.label(
96106
default = ":requirements.bzl.tmpl.bzlmod",
97107
),

python/private/pypi/pip_repository.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def _pip_repository_impl(rctx):
156156
imports = [
157157
# NOTE: Maintain the order consistent with `buildifier`
158158
'load("@rules_python//python:pip.bzl", "pip_utils")',
159-
'load("@rules_python//python/pip_install:pip_repository.bzl", "group_library", "whl_library")',
159+
'load("@rules_python//python/pip_install:pip_repository.bzl", "group_library", "whl_config_repository", "whl_library")',
160160
]
161161

162162
annotations = {}
@@ -203,6 +203,13 @@ 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%%": """\
207+
whl_config_library(
208+
name = config_repo,
209+
whl_map = {
210+
p: "" for p in all_whl_requirements_by_package
211+
},
212+
)""",
206213
" # %%GROUP_LIBRARY%%": """\
207214
group_repo = "{name}__groups"
208215
group_library(

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ all_whl_requirements = all_whl_requirements_by_package.values()
1313

1414
all_data_requirements = %%ALL_DATA_REQUIREMENTS%%
1515

16-
packages = sorted(all_whl_requirements_by_package)
17-
1816
def requirement(name):
1917
return "%%MACRO_TMPL%%".format(pip_utils.normalize_name(name), "pkg")
2018

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ all_whl_requirements = all_whl_requirements_by_package.values()
1313

1414
all_data_requirements = %%ALL_DATA_REQUIREMENTS%%
1515

16-
packages = sorted(all_whl_requirements_by_package)
17-
1816
_packages = %%PACKAGES%%
1917
_config = %%CONFIG%%
2018
_annotations = %%ANNOTATIONS%%
@@ -56,6 +54,15 @@ def install_deps(**whl_library_kwargs):
5654

5755
# %%GROUP_LIBRARY%%
5856

57+
config_repo = "%%NAME%%__config"
58+
whl_config_repository(
59+
name = config_repo,
60+
whl_map = {
61+
p: ""
62+
for p in all_whl_requirements_by_package
63+
},
64+
)
65+
5966
# Install wheels which may be participants in a group
6067
whl_config = dict(_config)
6168
whl_config.update(whl_library_kwargs)
@@ -70,6 +77,6 @@ def install_deps(**whl_library_kwargs):
7077
group_name = group_name,
7178
group_deps = group_deps,
7279
annotation = _get_annotation(requirement),
73-
packages = packages,
80+
config_load = "@{}//:config.bzl".format(config_repo),
7481
**whl_config
7582
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
""
2+
3+
load("//python/private:text_util.bzl", "render")
4+
5+
def _impl(rctx):
6+
rctx.file("BUILD.bazel", "")
7+
rctx.template(
8+
"config.bzl",
9+
rctx.attr._config_template,
10+
substitutions = {
11+
"%%PACKAGES%%": render.dict(rctx.attr.whl_map, value_repr = lambda x: "None"),
12+
},
13+
)
14+
15+
whl_config_repository = repository_rule(
16+
attrs = {
17+
"whl_map": attr.string_dict(
18+
mandatory = True,
19+
doc = """\
20+
The wheel map where values are json.encoded strings of the whl_map constructed
21+
in the pip.parse tag class.
22+
""",
23+
),
24+
"_config_template": attr.label(
25+
default = ":config.bzl.tmpl.bzlmod",
26+
),
27+
},
28+
doc = """A rule for WORKSPACE to ensure correct whl_repository configuration. PRIVATE USE ONLY.""",
29+
implementation = _impl,
30+
)

0 commit comments

Comments
 (0)