Skip to content

Commit 044b004

Browse files
committed
refactor: add a method for checking if we should use the downloader
1 parent 76d082e commit 044b004

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

python/private/pypi/extension.bzl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ 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.python_version)
101100
interpreter = hub.detect_interpreter(pip_attr)
102101

103102
# containers to aggregate outputs from this function
@@ -184,15 +183,11 @@ def _create_whl_repos(
184183
),
185184
platforms = platforms,
186185
extra_pip_args = pip_attr.extra_pip_args,
187-
get_index_urls = get_index_urls,
186+
get_index_urls = hub.get_index_urls(pip_attr.python_version),
188187
evaluate_markers = evaluate_markers,
189188
logger = logger,
190189
)
191190

192-
use_downloader = {
193-
normalize_name(s): False
194-
for s in pip_attr.simpleapi_skip
195-
}
196191
exposed_packages = {}
197192
for whl in requirements_by_platform:
198193
if whl.is_exposed:
@@ -246,10 +241,7 @@ def _create_whl_repos(
246241
whl_library_args = whl_library_args,
247242
download_only = pip_attr.download_only,
248243
netrc = hub.config.netrc or pip_attr.netrc,
249-
use_downloader = use_downloader.get(
250-
whl.name,
251-
get_index_urls != None, # defaults to True if the get_index_urls is defined
252-
),
244+
use_downloader = hub.use_downloader(pip_attr.python_version, whl.name),
253245
auth_patterns = hub.config.auth_patterns or pip_attr.auth_patterns,
254246
python_version = _major_minor_version(pip_attr.python_version),
255247
is_multiple_versions = whl.is_multiple_versions,

python/private/pypi/hub_builder.bzl

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,18 @@ def hub_builder(
4747
_simpleapi_download_fn = simpleapi_download_fn,
4848
_simpleapi_cache = simpleapi_cache,
4949
_get_index_urls = {},
50+
_pip_attrs = {},
51+
_use_downloader = {},
5052
# keep sorted
5153
add = lambda *a, **k: _add(self, *a, **k),
5254
detect_interpreter = lambda *a, **k: _detect_interpreter(self, *a, **k),
53-
get_index_urls = lambda version: self._get_index_urls[version],
55+
get_index_urls = lambda version: self._get_index_urls.get(version),
5456
platforms = lambda version: self.python_versions[version],
5557
add_whl_library = lambda *a, **k: _add_whl_library(self, *a, **k),
58+
use_downloader = lambda python_version, whl_name: self._use_downloader.get(python_version, {}).get(
59+
normalize_name(whl_name),
60+
python_version in self._get_index_urls,
61+
),
5662
)
5763

5864
# buildifier: enable=uninitialized
@@ -76,22 +82,32 @@ def _add(self, *, pip_attr):
7682
minor_mapping = self._minor_mapping,
7783
config = self.config,
7884
)
79-
self._get_index_urls[python_version] = _get_index_urls(self, pip_attr)
85+
_set_index_urls(self, pip_attr)
86+
self._pip_attrs[python_version] = pip_attr
8087

81-
def _get_index_urls(self, pip_attr):
88+
def _set_index_urls(self, pip_attr):
8289
if not pip_attr.experimental_index_url:
8390
if pip_attr.experimental_extra_index_urls:
8491
fail("'experimental_extra_index_urls' is a no-op unless 'experimental_index_url' is set")
8592
elif pip_attr.experimental_index_url_overrides:
8693
fail("'experimental_index_url_overrides' is a no-op unless 'experimental_index_url' is set")
94+
elif pip_attr.simpleapi_skip:
95+
fail("'simpleapi_skip' is a no-op unless 'experimental_index_url' is set")
96+
elif pip_attr.netrc:
97+
fail("'netrc' is a no-op unless 'experimental_index_url' is set")
98+
elif pip_attr.auth_patterns:
99+
fail("'auth_patterns' is a no-op unless 'experimental_index_url' is set")
100+
101+
# parallel_download is set to True by default, so we are not checking/validating it
102+
# here
103+
return
87104

88-
return None
89-
90-
skip_sources = [
91-
normalize_name(s)
105+
python_version = pip_attr.python_version
106+
self._use_downloader.setdefault(python_version, {}).update({
107+
normalize_name(s): False
92108
for s in pip_attr.simpleapi_skip
93-
]
94-
return lambda ctx, distributions: self._simpleapi_download_fn(
109+
})
110+
self._get_index_urls[python_version] = lambda ctx, distributions: self._simpleapi_download_fn(
95111
ctx,
96112
attr = struct(
97113
index_url = pip_attr.experimental_index_url,
@@ -100,7 +116,7 @@ def _get_index_urls(self, pip_attr):
100116
sources = [
101117
d
102118
for d in distributions
103-
if normalize_name(d) not in skip_sources
119+
if self.use_downloader(python_version, d)
104120
],
105121
envsubst = pip_attr.envsubst,
106122
# Auth related info

0 commit comments

Comments
 (0)