Skip to content

Commit 17ce70a

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 7816c21 commit 17ce70a

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

CHANGELOG.md

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

python/private/pypi/extension.bzl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ def _create_whl_repos(
288288
src = src,
289289
whl_library_args = whl_library_args,
290290
download_only = pip_attr.download_only,
291-
netrc = pip_attr.netrc,
292-
auth_patterns = pip_attr.auth_patterns,
291+
netrc = config.netrc or pip_attr.netrc,
292+
auth_patterns = config.auth_patterns or pip_attr.auth_patterns,
293293
python_version = major_minor,
294294
is_multiple_versions = whl.is_multiple_versions,
295295
enable_pipstar = config.enable_pipstar,
@@ -376,7 +376,7 @@ def _whl_repo(*, src, whl_library_args, is_multiple_versions, download_only, net
376376
),
377377
)
378378

379-
def _configure(config, *, platform, os_name, arch_name, config_settings, env = {}, override = False):
379+
def _configure(config, *, platform, os_name, arch_name, config_settings, env = {}, override = False, **kwargs):
380380
"""Set the value in the config if the value is provided"""
381381
config.setdefault("platforms", {})
382382
if platform and (os_name or arch_name or config_settings or env):
@@ -402,9 +402,13 @@ def _configure(config, *, platform, os_name, arch_name, config_settings, env = {
402402
continue
403403

404404
config["platforms"][platform][key] = value
405-
else:
405+
elif platform and override:
406406
config["platforms"].pop(platform)
407407

408+
for key, value in kwargs.items():
409+
if value and (override or key not in config):
410+
config[key] = value
411+
408412
def _plat(*, name, arch_name, os_name, config_settings = [], env = {}):
409413
return struct(
410414
name = name,
@@ -444,16 +448,19 @@ def build_config(
444448
os_name = tag.os_name,
445449
platform = tag.platform,
446450
override = mod.is_root,
451+
# extra values that we just add
452+
auth_patterns = tag.auth_patterns,
453+
netrc = tag.netrc,
447454
# TODO @aignas 2025-05-19: add more attr groups:
448-
# * for AUTH - the default `netrc` usage could be configured through a common
449-
# attribute.
450455
# * for index/downloader config. This includes all of those attributes for
451456
# overrides, etc. Index overrides per platform could be also used here.
452457
# * for whl selection - selecting preferences of which `platform_tag`s we should use
453458
# for what. We could also model the `cp313t` freethreaded as separate platforms.
454459
)
455460

456461
return struct(
462+
auth_patterns = defaults.get("auth_patterns", {}),
463+
netrc = defaults.get("netrc", None),
457464
platforms = {
458465
name: _plat(**values)
459466
for name, values in defaults["platforms"].items()
@@ -856,7 +863,7 @@ This is only used if the {envvar}`RULES_PYTHON_ENABLE_PIPSTAR` is enabled.
856863
""",
857864
),
858865
# The values for PEP508 env marker evaluation during the lock file parsing
859-
}
866+
} | AUTH_ATTRS
860867

861868
_SUPPORTED_PEP508_KEYS = [
862869
"implementation_name",

tests/pypi/extension/extension_tests.bzl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,33 @@ def _build_config(env, enable_pipstar = 0, **kwargs):
9999
**kwargs
100100
),
101101
attrs = dict(
102-
platforms = subjects.dict,
102+
auth_patterns = subjects.dict,
103103
enable_pipstar = subjects.bool,
104+
netrc = subjects.str,
105+
platforms = subjects.dict,
104106
),
105107
)
106108

107109
def _default(
108110
arch_name = None,
111+
auth_patterns = None,
109112
config_settings = None,
113+
env = None,
114+
netrc = None,
110115
os_name = None,
111116
platform = None,
112-
env = None,
113117
whl_limit = None,
114118
whl_platforms = None):
115119
return struct(
116120
arch_name = arch_name,
117-
os_name = os_name,
118-
platform = platform,
121+
auth_patterns = auth_patterns or {},
119122
config_settings = config_settings,
120123
env = env or {},
121-
whl_platforms = whl_platforms,
124+
netrc = netrc,
125+
os_name = os_name,
126+
platform = platform,
122127
whl_limit = whl_limit,
128+
whl_platforms = whl_platforms,
123129
)
124130

125131
def _parse(
@@ -1234,11 +1240,17 @@ def _test_build_pipstar_platform(env):
12341240
"@platforms//cpu:x86_64",
12351241
],
12361242
),
1243+
_default(
1244+
netrc = "my_netrc",
1245+
auth_patterns = {"foo": "bar"},
1246+
),
12371247
],
12381248
),
12391249
),
12401250
enable_pipstar = True,
12411251
)
1252+
config.auth_patterns().contains_exactly({"foo": "bar"})
1253+
config.netrc().equals("my_netrc")
12421254
config.enable_pipstar().equals(True)
12431255
config.platforms().contains_exactly({
12441256
"myplat": struct(

0 commit comments

Comments
 (0)