Skip to content

Commit

Permalink
feat: add ability to override hub name
Browse files Browse the repository at this point in the history
  • Loading branch information
Finn Ball committed Mar 31, 2024
1 parent 5631f05 commit 59f00a6
Showing 1 changed file with 44 additions and 29 deletions.
73 changes: 44 additions & 29 deletions python/private/bzlmod/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides):
group_deps = requirement_cycles.get(group_name, [])

repo_name = "{}_{}".format(pip_name, whl_name)
# print(repo_name)
# print(pip_name)
# print(dir(module_ctx))
whl_library(
name = repo_name,
requirement = requirement_line,
Expand Down Expand Up @@ -332,39 +335,45 @@ def _pip_impl(module_ctx):
for mod in module_ctx.modules:
for pip_attr in mod.tags.parse:
hub_name = pip_attr.hub_name
if hub_name not in pip_hub_map:
# only override if the module is root.
override = mod.is_root and pip_attr.override
if hub_name not in pip_hub_map or override:
pip_hub_map[pip_attr.hub_name] = struct(
module_name = mod.name,
python_versions = [pip_attr.python_version],
pip_attr = pip_attr,
override = override
)
elif pip_hub_map[hub_name].module_name != mod.name:
# We cannot have two hubs with the same name in different
# modules.
fail((
"Duplicate cross-module pip hub named '{hub}': pip hub " +
"names must be unique across modules. First defined " +
"by module '{first_module}', second attempted by " +
"module '{second_module}'"
).format(
hub = hub_name,
first_module = pip_hub_map[hub_name].module_name,
second_module = mod.name,
))

elif pip_attr.python_version in pip_hub_map[hub_name].python_versions:
fail((
"Duplicate pip python version '{version}' for hub " +
"'{hub}' in module '{module}': the Python versions " +
"used for a hub must be unique"
).format(
hub = hub_name,
module = mod.name,
version = pip_attr.python_version,
))
else:
pip_hub_map[pip_attr.hub_name].python_versions.append(pip_attr.python_version)

_create_whl_repos(module_ctx, pip_attr, hub_whl_map, whl_overrides)
elif not pip_hub_map[hub_name].pip_attr.override:
if pip_hub_map[hub_name].module_name != mod.name:
# We cannot have two hubs with the same name in different
# modules.
fail((
"Duplicate cross-module pip hub named '{hub}': pip hub " +
"names must be unique across modules. First defined " +
"by module '{first_module}', second attempted by " +
"module '{second_module}'"
).format(
hub = hub_name,
first_module = pip_hub_map[hub_name].module_name,
second_module = mod.name,
))

elif pip_attr.python_version in pip_hub_map[hub_name].python_versions:
fail((
"Duplicate pip python version '{version}' for hub " +
"'{hub}' in module '{module}': the Python versions " +
"used for a hub must be unique"
).format(
hub = hub_name,
module = mod.name,
version = pip_attr.python_version,
))
else:
pip_hub_map[pip_attr.hub_name].python_versions.append(pip_attr.python_version)

for value in pip_hub_map.values():
_create_whl_repos(module_ctx, value.pip_attr, hub_whl_map, whl_overrides)

for hub_name, whl_map in hub_whl_map.items():
pip_repository(
Expand Down Expand Up @@ -421,6 +430,12 @@ A dict of labels to wheel names that is typically generated by the whl_modificat
The labels are JSON config files describing the modifications.
""",
),
"override": attr.bool(
default = False,
doc = """
If the module is root, this may override any hub_name's listed in the dependencies.
"""
),
}, **pip_repository_attrs)

# Like the pip_repository rule, we end up setting this manually so
Expand Down

0 comments on commit 59f00a6

Please sign in to comment.