Skip to content

Commit 76d082e

Browse files
committed
refactor: get_index_urls early return
1 parent 106789f commit 76d082e

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

python/private/pypi/extension.bzl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def _create_whl_repos(
9797
rule.
9898
"""
9999
logger = repo_utils.logger(module_ctx, "pypi:create_whl_repos")
100-
get_index_urls = hub.get_index_urls(pip_attr)
100+
get_index_urls = hub.get_index_urls(pip_attr.python_version)
101101
interpreter = hub.detect_interpreter(pip_attr)
102102

103103
# containers to aggregate outputs from this function
@@ -564,9 +564,7 @@ You cannot use both the additive_build_content and additive_build_content_file a
564564
else:
565565
builder = pip_hub_map[pip_attr.hub_name]
566566

567-
builder.add(
568-
python_version = pip_attr.python_version,
569-
)
567+
builder.add(pip_attr = pip_attr)
570568

571569
# TODO @aignas 2025-05-19: express pip.parse as a series of configure calls
572570
out = _create_whl_repos(

python/private/pypi/hub_builder.bzl

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,20 @@ def hub_builder(
4646
_available_interpreters = available_interpreters,
4747
_simpleapi_download_fn = simpleapi_download_fn,
4848
_simpleapi_cache = simpleapi_cache,
49+
_get_index_urls = {},
4950
# keep sorted
5051
add = lambda *a, **k: _add(self, *a, **k),
51-
get_index_urls = lambda *a, **k: _get_index_urls(self, *a, **k),
5252
detect_interpreter = lambda *a, **k: _detect_interpreter(self, *a, **k),
53+
get_index_urls = lambda version: self._get_index_urls[version],
5354
platforms = lambda version: self.python_versions[version],
5455
add_whl_library = lambda *a, **k: _add_whl_library(self, *a, **k),
5556
)
5657

5758
# buildifier: enable=uninitialized
5859
return self
5960

60-
def _add(self, *, python_version):
61+
def _add(self, *, pip_attr):
62+
python_version = pip_attr.python_version
6163
if python_version in self.python_versions:
6264
fail((
6365
"Duplicate pip python version '{version}' for hub " +
@@ -74,39 +76,40 @@ def _add(self, *, python_version):
7476
minor_mapping = self._minor_mapping,
7577
config = self.config,
7678
)
79+
self._get_index_urls[python_version] = _get_index_urls(self, pip_attr)
7780

7881
def _get_index_urls(self, pip_attr):
79-
get_index_urls = None
80-
if pip_attr.experimental_index_url:
81-
skip_sources = [
82-
normalize_name(s)
83-
for s in pip_attr.simpleapi_skip
84-
]
85-
get_index_urls = lambda ctx, distributions: self._simpleapi_download_fn(
86-
ctx,
87-
attr = struct(
88-
index_url = pip_attr.experimental_index_url,
89-
extra_index_urls = pip_attr.experimental_extra_index_urls or [],
90-
index_url_overrides = pip_attr.experimental_index_url_overrides or {},
91-
sources = [
92-
d
93-
for d in distributions
94-
if normalize_name(d) not in skip_sources
95-
],
96-
envsubst = pip_attr.envsubst,
97-
# Auth related info
98-
netrc = pip_attr.netrc,
99-
auth_patterns = pip_attr.auth_patterns,
100-
),
101-
cache = self._simpleapi_cache,
102-
parallel_download = pip_attr.parallel_download,
103-
)
104-
elif pip_attr.experimental_extra_index_urls:
105-
fail("'experimental_extra_index_urls' is a no-op unless 'experimental_index_url' is set")
106-
elif pip_attr.experimental_index_url_overrides:
107-
fail("'experimental_index_url_overrides' is a no-op unless 'experimental_index_url' is set")
108-
109-
return get_index_urls
82+
if not pip_attr.experimental_index_url:
83+
if pip_attr.experimental_extra_index_urls:
84+
fail("'experimental_extra_index_urls' is a no-op unless 'experimental_index_url' is set")
85+
elif pip_attr.experimental_index_url_overrides:
86+
fail("'experimental_index_url_overrides' is a no-op unless 'experimental_index_url' is set")
87+
88+
return None
89+
90+
skip_sources = [
91+
normalize_name(s)
92+
for s in pip_attr.simpleapi_skip
93+
]
94+
return lambda ctx, distributions: self._simpleapi_download_fn(
95+
ctx,
96+
attr = struct(
97+
index_url = pip_attr.experimental_index_url,
98+
extra_index_urls = pip_attr.experimental_extra_index_urls or [],
99+
index_url_overrides = pip_attr.experimental_index_url_overrides or {},
100+
sources = [
101+
d
102+
for d in distributions
103+
if normalize_name(d) not in skip_sources
104+
],
105+
envsubst = pip_attr.envsubst,
106+
# Auth related info
107+
netrc = pip_attr.netrc,
108+
auth_patterns = pip_attr.auth_patterns,
109+
),
110+
cache = self._simpleapi_cache,
111+
parallel_download = pip_attr.parallel_download,
112+
)
110113

111114
def _detect_interpreter(self, pip_attr):
112115
python_interpreter_target = pip_attr.python_interpreter_target

0 commit comments

Comments
 (0)