Skip to content

Commit 106789f

Browse files
committed
refactor: store the whl_libraries for each hub in the builder
1 parent 7ee62e7 commit 106789f

File tree

2 files changed

+58
-55
lines changed

2 files changed

+58
-55
lines changed

python/private/pypi/extension.bzl

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ load("//python/private:full_version.bzl", "full_version")
2323
load("//python/private:normalize_name.bzl", "normalize_name")
2424
load("//python/private:repo_utils.bzl", "repo_utils")
2525
load("//python/private:version.bzl", "version")
26-
load("//python/private:version_label.bzl", "version_label")
2726
load(":attrs.bzl", "use_isolated")
2827
load(":evaluate_markers.bzl", "evaluate_markers_py", EVALUATE_MARKERS_SRCS = "SRCS", evaluate_markers_star = "evaluate_markers")
2928
load(":hub_builder.bzl", "hub_builder")
@@ -106,14 +105,6 @@ def _create_whl_repos(
106105
whl_name: {alias: True for alias in aliases}
107106
for whl_name, aliases in pip_attr.extra_hub_aliases.items()
108107
}
109-
whl_libraries = {}
110-
111-
# TODO @aignas 2025-06-29: we should not need the version in the pip_name if
112-
# we are using pipstar and we are downloading the wheel using the downloader
113-
pip_name = "{}_{}".format(
114-
hub.name,
115-
version_label(pip_attr.python_version),
116-
)
117108

118109
whl_modifications = {}
119110
if pip_attr.whl_modifications != None:
@@ -264,47 +255,15 @@ def _create_whl_repos(
264255
is_multiple_versions = whl.is_multiple_versions,
265256
enable_pipstar = hub.config.enable_pipstar,
266257
)
267-
if repo == None:
268-
# NOTE @aignas 2025-07-07: we guard against an edge-case where there
269-
# are more platforms defined than there are wheels for and users
270-
# disallow building from sdist.
271-
continue
272-
273-
repo_name = "{}_{}".format(pip_name, repo.repo_name)
274-
if repo_name in whl_libraries:
275-
fail("attempting to create a duplicate library {} for {}".format(
276-
repo_name,
277-
whl.name,
278-
))
279-
whl_libraries[repo_name] = repo.args
280-
281-
if not hub.config.enable_pipstar and "experimental_target_platforms" in repo.args:
282-
whl_libraries[repo_name] |= {
283-
"experimental_target_platforms": sorted({
284-
# TODO @aignas 2025-07-07: this should be solved in a better way
285-
platforms[candidate].triple.partition("_")[-1]: None
286-
for p in repo.args["experimental_target_platforms"]
287-
for candidate in platforms
288-
if candidate.endswith(p)
289-
}),
290-
}
291-
292-
mapping = hub.whl_map.setdefault(whl.name, {})
293-
if repo.config_setting in mapping and mapping[repo.config_setting] != repo_name:
294-
fail(
295-
"attempting to override an existing repo '{}' for config setting '{}' with a new repo '{}'".format(
296-
mapping[repo.config_setting],
297-
repo.config_setting,
298-
repo_name,
299-
),
300-
)
301-
else:
302-
mapping[repo.config_setting] = repo_name
258+
hub.add_whl_library(
259+
python_version = pip_attr.python_version,
260+
whl = whl,
261+
repo = repo,
262+
)
303263

304264
return struct(
305265
exposed_packages = exposed_packages,
306266
extra_aliases = extra_aliases,
307-
whl_libraries = whl_libraries,
308267
)
309268

310269
def _whl_repo(
@@ -631,15 +590,6 @@ You cannot use both the additive_build_content and additive_build_content_file a
631590
continue
632591
intersection[pkg] = None
633592
exposed_packages[hub_name] = intersection
634-
whl_libraries.update(out.whl_libraries)
635-
for whl_name, lib in out.whl_libraries.items():
636-
if enable_pipstar:
637-
whl_libraries.setdefault(whl_name, lib)
638-
elif whl_name in lib:
639-
fail("'{}' already in created".format(whl_name))
640-
else:
641-
# replicate whl_libraries.update(out.whl_libraries)
642-
whl_libraries[whl_name] = lib
643593

644594
# TODO @aignas 2024-04-05: how do we support different requirement
645595
# cycles for different abis/oses? For now we will need the users to
@@ -653,6 +603,14 @@ You cannot use both the additive_build_content and additive_build_content_file a
653603
for setting, repo in settings.items():
654604
hub_whl_map[hub.name].setdefault(key, {}).setdefault(repo, []).append(setting)
655605

606+
whl_libraries.update(hub.whl_libraries)
607+
for whl_name, lib in hub.whl_libraries.items():
608+
if whl_name in lib:
609+
fail("'{}' already in created".format(whl_name))
610+
else:
611+
# replicate whl_libraries.update(out.whl_libraries)
612+
whl_libraries[whl_name] = lib
613+
656614
return struct(
657615
# We sort so that the lock-file remains the same no matter the order of how the
658616
# args are manipulated in the code going before.

python/private/pypi/hub_builder.bzl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
load("//python/private:full_version.bzl", "full_version")
44
load("//python/private:normalize_name.bzl", "normalize_name")
55
load("//python/private:version.bzl", "version")
6+
load("//python/private:version_label.bzl", "version_label")
67
load(":pep508_env.bzl", "env")
78
load(":pep508_evaluate.bzl", "evaluate")
89
load(":python_tag.bzl", "python_tag")
@@ -50,6 +51,7 @@ def hub_builder(
5051
get_index_urls = lambda *a, **k: _get_index_urls(self, *a, **k),
5152
detect_interpreter = lambda *a, **k: _detect_interpreter(self, *a, **k),
5253
platforms = lambda version: self.python_versions[version],
54+
add_whl_library = lambda *a, **k: _add_whl_library(self, *a, **k),
5355
)
5456

5557
# buildifier: enable=uninitialized
@@ -175,3 +177,46 @@ def _platforms(*, python_version, minor_mapping, config):
175177
whl_platform_tags = values.whl_platform_tags,
176178
)
177179
return platforms
180+
181+
def _add_whl_library(self, *, python_version, whl, repo):
182+
if repo == None:
183+
# NOTE @aignas 2025-07-07: we guard against an edge-case where there
184+
# are more platforms defined than there are wheels for and users
185+
# disallow building from sdist.
186+
return
187+
188+
platforms = self.python_versions[python_version]
189+
190+
# TODO @aignas 2025-06-29: we should not need the version in the repo_name if
191+
# we are using pipstar and we are downloading the wheel using the downloader
192+
repo_name = "{}_{}_{}".format(self.name, version_label(python_version), repo.repo_name)
193+
194+
if repo_name in self.whl_libraries:
195+
fail("attempting to create a duplicate library {} for {}".format(
196+
repo_name,
197+
whl.name,
198+
))
199+
self.whl_libraries[repo_name] = repo.args
200+
201+
if not self.config.enable_pipstar and "experimental_target_platforms" in repo.args:
202+
self.whl_libraries[repo_name] |= {
203+
"experimental_target_platforms": sorted({
204+
# TODO @aignas 2025-07-07: this should be solved in a better way
205+
platforms[candidate].triple.partition("_")[-1]: None
206+
for p in repo.args["experimental_target_platforms"]
207+
for candidate in platforms
208+
if candidate.endswith(p)
209+
}),
210+
}
211+
212+
mapping = self.whl_map.setdefault(whl.name, {})
213+
if repo.config_setting in mapping and mapping[repo.config_setting] != repo_name:
214+
fail(
215+
"attempting to override an existing repo '{}' for config setting '{}' with a new repo '{}'".format(
216+
mapping[repo.config_setting],
217+
repo.config_setting,
218+
repo_name,
219+
),
220+
)
221+
else:
222+
mapping[repo.config_setting] = repo_name

0 commit comments

Comments
 (0)