Skip to content

Commit e2295ab

Browse files
authored
feat(pypi): builder for netrc and auth_patterns (#3136)
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 8c33aa6 commit e2295ab

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
@@ -128,7 +128,8 @@ END_UNRELEASED_TEMPLATE
128128
([#3114](https://github.com/bazel-contrib/rules_python/pull/3114)).
129129
* (pypi) To configure the environment for `requirements.txt` evaluation, use the newly added
130130
developer preview of the `pip.default` tag class. Only `rules_python` and root modules can use
131-
this feature. You can also configure custom `config_settings` using `pip.default`.
131+
this feature. You can also configure custom `config_settings` using `pip.default`. It
132+
can also be used to set the global `netrc` or `auth_patterns` variables.
132133
* (pypi) PyPI dependencies now expose an `:extracted_whl_files` filegroup target
133134
of all the files extracted from the wheel. This can be used in lieu of
134135
{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
@@ -322,12 +322,12 @@ def _create_whl_repos(
322322
src = src,
323323
whl_library_args = whl_library_args,
324324
download_only = pip_attr.download_only,
325-
netrc = pip_attr.netrc,
325+
netrc = config.netrc or pip_attr.netrc,
326326
use_downloader = use_downloader.get(
327327
whl.name,
328328
get_index_urls != None, # defaults to True if the get_index_urls is defined
329329
),
330-
auth_patterns = pip_attr.auth_patterns,
330+
auth_patterns = config.auth_patterns or pip_attr.auth_patterns,
331331
python_version = major_minor,
332332
is_multiple_versions = whl.is_multiple_versions,
333333
enable_pipstar = config.enable_pipstar,
@@ -502,13 +502,20 @@ def build_config(
502502
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):
503503
defaults["platforms"].pop(platform)
504504

505-
# TODO @aignas 2025-05-19: add more attr groups:
506-
# * for AUTH - the default `netrc` usage could be configured through a common
507-
# attribute.
508-
# * for index/downloader config. This includes all of those attributes for
509-
# overrides, etc. Index overrides per platform could be also used here.
505+
_configure(
506+
defaults,
507+
override = mod.is_root,
508+
# extra values that we just add
509+
auth_patterns = tag.auth_patterns,
510+
netrc = tag.netrc,
511+
# TODO @aignas 2025-05-19: add more attr groups:
512+
# * for index/downloader config. This includes all of those attributes for
513+
# overrides, etc. Index overrides per platform could be also used here.
514+
)
510515

511516
return struct(
517+
auth_patterns = defaults.get("auth_patterns", {}),
518+
netrc = defaults.get("netrc", None),
512519
platforms = {
513520
name: _plat(**values)
514521
for name, values in defaults["platforms"].items()
@@ -975,7 +982,7 @@ See official [docs](https://packaging.python.org/en/latest/specifications/platfo
975982
:::
976983
""",
977984
),
978-
}
985+
} | AUTH_ATTRS
979986

980987
_SUPPORTED_PEP508_KEYS = [
981988
"implementation_name",

tests/pypi/extension/extension_tests.bzl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,34 @@ def _build_config(env, enable_pipstar = 0, **kwargs):
100100
**kwargs
101101
),
102102
attrs = dict(
103-
platforms = subjects.dict,
103+
auth_patterns = subjects.dict,
104104
enable_pipstar = subjects.bool,
105+
netrc = subjects.str,
106+
platforms = subjects.dict,
105107
),
106108
)
107109

108110
def _default(
109111
*,
110112
arch_name = None,
113+
auth_patterns = None,
111114
config_settings = None,
115+
env = None,
116+
netrc = None,
112117
os_name = None,
113118
platform = None,
114119
whl_platform_tags = None,
115-
env = None,
116120
whl_abi_tags = None):
117121
return struct(
118122
arch_name = arch_name,
119-
os_name = os_name,
120-
platform = platform,
121-
whl_platform_tags = whl_platform_tags or [],
123+
auth_patterns = auth_patterns or {},
122124
config_settings = config_settings,
123125
env = env or {},
126+
netrc = netrc,
127+
os_name = os_name,
128+
platform = platform,
124129
whl_abi_tags = whl_abi_tags or [],
130+
whl_platform_tags = whl_platform_tags or [],
125131
)
126132

127133
def _parse(
@@ -1224,11 +1230,17 @@ def _test_build_pipstar_platform(env):
12241230
],
12251231
),
12261232
_default(platform = "myplat2"),
1233+
_default(
1234+
netrc = "my_netrc",
1235+
auth_patterns = {"foo": "bar"},
1236+
),
12271237
],
12281238
),
12291239
),
12301240
enable_pipstar = True,
12311241
)
1242+
config.auth_patterns().contains_exactly({"foo": "bar"})
1243+
config.netrc().equals("my_netrc")
12321244
config.enable_pipstar().equals(True)
12331245
config.platforms().contains_exactly({
12341246
"myplat": struct(

0 commit comments

Comments
 (0)