Skip to content

Commit c283aa3

Browse files
committed
documnet methods on the builder
1 parent 4628688 commit c283aa3

File tree

1 file changed

+65
-52
lines changed

1 file changed

+65
-52
lines changed

python/private/pypi/hub_builder.bzl

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -50,49 +50,55 @@ def hub_builder(
5050
self = struct(
5151
name = name,
5252
module_name = module_name,
53+
54+
# public methods, keep sorted and to minimum
55+
build = lambda: _build(self),
56+
pip_parse = lambda *a, **k: _pip_parse(self, *a, **k),
57+
58+
# build output
59+
_exposed_packages = {}, # modified by _add_exposed_packages
60+
_extra_aliases = {}, # modified by _add_extra_aliases
61+
_group_map = {}, # modified by _add_group_map
62+
_whl_libraries = {}, # modified by _add_whl_library
63+
_whl_map = {}, # modified by _add_whl_library
64+
# internal
5365
_platforms = {},
54-
config = config,
55-
whl_map = {},
56-
exposed_packages = {},
57-
extra_aliases = {},
58-
whl_libraries = {},
59-
group_map = {},
66+
_get_index_urls = {},
67+
_use_downloader = {},
68+
_simpleapi_cache = simpleapi_cache,
69+
# instance constants
70+
_config = config,
6071
_evaluate_markers_fn = evaluate_markers_fn,
6172
_logger = logger,
6273
_minor_mapping = minor_mapping,
6374
_available_interpreters = available_interpreters,
6475
_simpleapi_download_fn = simpleapi_download_fn,
65-
_simpleapi_cache = simpleapi_cache,
66-
_get_index_urls = {},
67-
_use_downloader = {},
68-
# keep sorted
69-
get_index_urls = lambda version: self._get_index_urls.get(version),
70-
use_downloader = lambda python_version, whl_name: self._use_downloader.get(python_version, {}).get(
71-
normalize_name(whl_name),
72-
self.get_index_urls(python_version) != None,
73-
),
74-
build = lambda: _build(self),
75-
pip_parse = lambda *a, **k: _pip_parse(self, *a, **k),
7676
)
7777

7878
# buildifier: enable=uninitialized
7979
return self
8080

81+
def _use_downloader(self, python_version, whl_name):
82+
return self._use_downloader.get(python_version, {}).get(
83+
normalize_name(whl_name),
84+
self._get_index_urls.get(python_version) != None,
85+
)
86+
8187
def _build(self):
8288
whl_map = {}
83-
for key, settings in self.whl_map.items():
89+
for key, settings in self._whl_map.items():
8490
for setting, repo in settings.items():
8591
whl_map.setdefault(key, {}).setdefault(repo, []).append(setting)
8692

8793
return struct(
8894
whl_map = whl_map,
89-
group_map = self.group_map,
95+
group_map = self._group_map,
9096
extra_aliases = {
9197
whl: sorted(aliases)
92-
for whl, aliases in self.extra_aliases.items()
98+
for whl, aliases in self._extra_aliases.items()
9399
},
94-
exposed_packages = sorted(self.exposed_packages),
95-
whl_libraries = self.whl_libraries,
100+
exposed_packages = sorted(self._exposed_packages),
101+
whl_libraries = self._whl_libraries,
96102
)
97103

98104
def _set_index_urls(self, pip_attr):
@@ -126,7 +132,7 @@ def _set_index_urls(self, pip_attr):
126132
sources = [
127133
d
128134
for d in distributions
129-
if self.use_downloader(python_version, d)
135+
if _use_downloader(self, python_version, d)
130136
],
131137
envsubst = pip_attr.envsubst,
132138
# Auth related info
@@ -220,15 +226,15 @@ def _add_whl_library(self, *, python_version, whl, repo):
220226
# we are using pipstar and we are downloading the wheel using the downloader
221227
repo_name = "{}_{}_{}".format(self.name, version_label(python_version), repo.repo_name)
222228

223-
if repo_name in self.whl_libraries:
229+
if repo_name in self._whl_libraries:
224230
fail("attempting to create a duplicate library {} for {}".format(
225231
repo_name,
226232
whl.name,
227233
))
228-
self.whl_libraries[repo_name] = repo.args
234+
self._whl_libraries[repo_name] = repo.args
229235

230-
if not self.config.enable_pipstar and "experimental_target_platforms" in repo.args:
231-
self.whl_libraries[repo_name] |= {
236+
if not self._config.enable_pipstar and "experimental_target_platforms" in repo.args:
237+
self._whl_libraries[repo_name] |= {
232238
"experimental_target_platforms": sorted({
233239
# TODO @aignas 2025-07-07: this should be solved in a better way
234240
platforms[candidate].triple.partition("_")[-1]: None
@@ -238,7 +244,7 @@ def _add_whl_library(self, *, python_version, whl, repo):
238244
}),
239245
}
240246

241-
mapping = self.whl_map.setdefault(whl.name, {})
247+
mapping = self._whl_map.setdefault(whl.name, {})
242248
if repo.config_setting in mapping and mapping[repo.config_setting] != repo_name:
243249
fail(
244250
"attempting to override an existing repo '{}' for config setting '{}' with a new repo '{}'".format(
@@ -254,7 +260,7 @@ def _evaluate_markers(self, pip_attr):
254260
if self._evaluate_markers_fn:
255261
return self._evaluate_markers_fn
256262

257-
if self.config.enable_pipstar:
263+
if self._config.enable_pipstar:
258264
return lambda _, requirements: evaluate_markers_star(
259265
requirements = requirements,
260266
platforms = self._platforms[pip_attr.python_version],
@@ -325,7 +331,7 @@ def _create_whl_repos(
325331
),
326332
platforms = platforms,
327333
extra_pip_args = pip_attr.extra_pip_args,
328-
get_index_urls = self.get_index_urls(pip_attr.python_version),
334+
get_index_urls = self._get_index_urls.get(pip_attr.python_version),
329335
evaluate_markers = _evaluate_markers(self, pip_attr),
330336
logger = logger,
331337
)
@@ -382,7 +388,7 @@ def _create_whl_repos(
382388
for p, args in whl_overrides.get(whl.name, {}).items()
383389
},
384390
)
385-
if not self.config.enable_pipstar:
391+
if not self._config.enable_pipstar:
386392
maybe_args["experimental_target_platforms"] = pip_attr.experimental_target_platforms
387393

388394
whl_library_args.update({k: v for k, v in maybe_args.items() if v})
@@ -403,12 +409,12 @@ def _create_whl_repos(
403409
src = src,
404410
whl_library_args = whl_library_args,
405411
download_only = pip_attr.download_only,
406-
netrc = self.config.netrc or pip_attr.netrc,
407-
use_downloader = self.use_downloader(pip_attr.python_version, whl.name),
408-
auth_patterns = self.config.auth_patterns or pip_attr.auth_patterns,
412+
netrc = self._config.netrc or pip_attr.netrc,
413+
use_downloader = _use_downloader(self, pip_attr.python_version, whl.name),
414+
auth_patterns = self._config.auth_patterns or pip_attr.auth_patterns,
409415
python_version = _major_minor_version(pip_attr.python_version),
410416
is_multiple_versions = whl.is_multiple_versions,
411-
enable_pipstar = self.config.enable_pipstar,
417+
enable_pipstar = self._config.enable_pipstar,
412418
)
413419
_add_whl_library(
414420
self,
@@ -417,16 +423,19 @@ def _create_whl_repos(
417423
repo = repo,
418424
)
419425

420-
if self.exposed_packages:
426+
_add_exposed_packages(self, exposed_packages)
427+
428+
def _add_exposed_packages(self, exposed_packages):
429+
if self._exposed_packages:
421430
intersection = {}
422431
for pkg in exposed_packages:
423-
if pkg not in self.exposed_packages:
432+
if pkg not in self._exposed_packages:
424433
continue
425434
intersection[pkg] = None
426-
self.exposed_packages.clear()
435+
self._exposed_packages.clear()
427436
exposed_packages = intersection
428437

429-
self.exposed_packages.update(exposed_packages)
438+
self._exposed_packages.update(exposed_packages)
430439

431440
def _whl_repo(
432441
*,
@@ -499,6 +508,20 @@ def _whl_repo(
499508
),
500509
)
501510

511+
def _add_group_map(self, group_map):
512+
# TODO @aignas 2024-04-05: how do we support different requirement
513+
# cycles for different abis/oses? For now we will need the users to
514+
# assume the same groups across all versions/platforms until we start
515+
# using an alternative cycle resolution strategy.
516+
self._group_map.clear()
517+
self._group_map.update(group_map)
518+
519+
def _add_extra_aliases(self, extra_hub_aliases):
520+
for whl_name, aliases in extra_hub_aliases.items():
521+
self._extra_aliases.setdefault(whl_name, {}).update(
522+
{alias: True for alias in aliases},
523+
)
524+
502525
def _pip_parse(self, module_ctx, pip_attr, whl_overrides):
503526
python_version = pip_attr.python_version
504527
if python_version in self._platforms:
@@ -515,21 +538,11 @@ def _pip_parse(self, module_ctx, pip_attr, whl_overrides):
515538
self._platforms[python_version] = _platforms(
516539
python_version = python_version,
517540
minor_mapping = self._minor_mapping,
518-
config = self.config,
541+
config = self._config,
519542
)
520543
_set_index_urls(self, pip_attr)
521-
522-
# TODO @aignas 2024-04-05: how do we support different requirement
523-
# cycles for different abis/oses? For now we will need the users to
524-
# assume the same groups across all versions/platforms until we start
525-
# using an alternative cycle resolution strategy.
526-
self.group_map.clear()
527-
self.group_map.update(pip_attr.experimental_requirement_cycles)
528-
529-
for whl_name, aliases in pip_attr.extra_hub_aliases.items():
530-
self.extra_aliases.setdefault(whl_name, {}).update(
531-
{alias: True for alias in aliases},
532-
)
544+
_add_group_map(self, pip_attr.experimental_requirement_cycles)
545+
_add_extra_aliases(self, pip_attr.extra_hub_aliases)
533546
_create_whl_repos(
534547
self,
535548
module_ctx,

0 commit comments

Comments
 (0)