Skip to content

Commit eff36be

Browse files
committed
feat(pypi): builder for netrc and auth_patterns
With this we move closer towards starting playing with the API to fully replace `pip.parse` with `pip.configure` builder pattern for better expressiveness. Work towards #2747
1 parent 040f77e commit eff36be

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ END_UNRELEASED_TEMPLATE
123123
([#3114](https://github.com/bazel-contrib/rules_python/pull/3114)).
124124
* (pypi) To configure the environment for `requirements.txt` evaluation, use the newly added
125125
developer preview of the `pip.default` tag class. Only `rules_python` and root modules can use
126-
this feature. You can also configure custom `config_settings` using `pip.default`.
126+
this feature. You can also configure custom `config_settings` using `pip.default`. It
127+
can also be used to set the global `netrc` or `auth_patterns` variables.
127128
* (pypi) PyPI dependencies now expose an `:extracted_whl_files` filegroup target
128129
of all the files extracted from the wheel. This can be used in lieu of
129130
{obj}`whl_filegroup` to avoid copying/extracting wheel multiple times to

python/private/pypi/extension.bzl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,12 @@ def _create_whl_repos(
333333
src = src,
334334
whl_library_args = whl_library_args,
335335
download_only = pip_attr.download_only,
336-
netrc = pip_attr.netrc,
336+
netrc = config.netrc or pip_attr.netrc,
337337
use_downloader = use_downloader.get(
338338
whl.name,
339339
get_index_urls != None, # defaults to True if the get_index_urls is defined
340340
),
341-
auth_patterns = pip_attr.auth_patterns,
341+
auth_patterns = config.auth_patterns or pip_attr.auth_patterns,
342342
python_version = major_minor,
343343
is_multiple_versions = whl.is_multiple_versions,
344344
enable_pipstar = config.enable_pipstar,
@@ -525,13 +525,20 @@ def build_config(
525525
if platform and not (tag.arch_name or tag.config_settings or tag.env or tag.os_name or tag.whl_abi_tags or tag.whl_platform_tags or tag.marker):
526526
defaults["platforms"].pop(platform)
527527

528-
# TODO @aignas 2025-05-19: add more attr groups:
529-
# * for AUTH - the default `netrc` usage could be configured through a common
530-
# attribute.
531-
# * for index/downloader config. This includes all of those attributes for
532-
# overrides, etc. Index overrides per platform could be also used here.
528+
_configure(
529+
defaults,
530+
override = mod.is_root,
531+
# extra values that we just add
532+
auth_patterns = tag.auth_patterns,
533+
netrc = tag.netrc,
534+
# TODO @aignas 2025-05-19: add more attr groups:
535+
# * for index/downloader config. This includes all of those attributes for
536+
# overrides, etc. Index overrides per platform could be also used here.
537+
)
533538

534539
return struct(
540+
auth_patterns = defaults.get("auth_patterns", {}),
541+
netrc = defaults.get("netrc", None),
535542
platforms = {
536543
name: _plat(**values)
537544
for name, values in defaults["platforms"].items()
@@ -1004,7 +1011,7 @@ See official [docs](https://packaging.python.org/en/latest/specifications/platfo
10041011
:::
10051012
""",
10061013
),
1007-
}
1014+
} | AUTH_ATTRS
10081015

10091016
_SUPPORTED_PEP508_KEYS = [
10101017
"implementation_name",

tests/pypi/extension/extension_tests.bzl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,36 @@ def _build_config(env, enable_pipstar = 0, **kwargs):
102102
**kwargs
103103
),
104104
attrs = dict(
105-
platforms = subjects.dict,
105+
auth_patterns = subjects.dict,
106106
enable_pipstar = subjects.bool,
107+
netrc = subjects.str,
108+
platforms = subjects.dict,
107109
),
108110
)
109111

110112
def _default(
111113
*,
112114
arch_name = None,
115+
auth_patterns = None,
113116
config_settings = None,
117+
env = None,
118+
netrc = None,
114119
os_name = None,
115120
platform = None,
116121
whl_platform_tags = None,
117-
env = None,
118122
marker = None,
119123
whl_abi_tags = None):
120124
return struct(
121125
arch_name = arch_name,
122-
os_name = os_name,
123-
platform = platform,
124-
whl_platform_tags = whl_platform_tags or [],
126+
auth_patterns = auth_patterns or {},
125127
config_settings = config_settings,
126128
env = env or {},
127129
marker = marker or "",
130+
netrc = netrc,
131+
os_name = os_name,
132+
platform = platform,
128133
whl_abi_tags = whl_abi_tags or [],
134+
whl_platform_tags = whl_platform_tags or [],
129135
)
130136

131137
def _parse(
@@ -1236,11 +1242,17 @@ def _test_build_pipstar_platform(env):
12361242
],
12371243
),
12381244
_default(platform = "myplat2"),
1245+
_default(
1246+
netrc = "my_netrc",
1247+
auth_patterns = {"foo": "bar"},
1248+
),
12391249
],
12401250
),
12411251
),
12421252
enable_pipstar = True,
12431253
)
1254+
config.auth_patterns().contains_exactly({"foo": "bar"})
1255+
config.netrc().equals("my_netrc")
12441256
config.enable_pipstar().equals(True)
12451257
config.platforms().contains_exactly({
12461258
"myplat": struct(

0 commit comments

Comments
 (0)