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 Apr 8, 2024
1 parent 24a910d commit b3bb708
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions python/private/bzlmod/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -427,39 +427,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, simpleapi_cache)
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, simpleapi_cache)

for hub_name, whl_map in hub_whl_map.items():
pip_repository(
Expand Down Expand Up @@ -557,6 +563,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)
attrs.update(AUTH_ATTRS)

Expand Down

0 comments on commit b3bb708

Please sign in to comment.