Skip to content

Commit 9c810df

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 d547f24 commit 9c810df

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ END_UNRELEASED_TEMPLATE
106106
### Added
107107
* (pypi) To configure the environment for `requirements.txt` evaluation, use the newly added
108108
developer preview of the `pip.default` tag class. Only `rules_python` and root modules can use
109-
this feature. You can also configure custom `config_settings` using `pip.default`.
109+
this feature. You can also configure custom `config_settings` using `pip.default`. It
110+
can also be used to set the global `netrc` or `auth_patterns` variables.
110111
* (pypi) PyPI dependencies now expose an `:extracted_whl_files` filegroup target
111112
of all the files extracted from the wheel. This can be used in lieu of
112113
{obj}`whl_filegroup` to avoid copying/extracting wheel multiple times to

python/private/pypi/extension.bzl

Lines changed: 17 additions & 8 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 = {}, netrc = None, auth_patterns = None, override = False):
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,16 @@ 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 {
409+
"auth_patterns": auth_patterns,
410+
"netrc": netrc,
411+
}.items():
412+
if value and (override or key not in config):
413+
config[key] = value
414+
408415
def _plat(*, name, arch_name, os_name, config_settings = [], env = {}):
409416
return struct(
410417
name = name,
@@ -441,21 +448,23 @@ def build_config(
441448
_configure(
442449
defaults,
443450
arch_name = tag.arch_name,
451+
auth_patterns = tag.auth_patterns,
444452
config_settings = tag.config_settings,
445453
env = tag.env,
454+
netrc = tag.netrc,
446455
os_name = tag.os_name,
447-
platform = platform,
448456
override = mod.is_root,
457+
platform = platform,
449458
# TODO @aignas 2025-05-19: add more attr groups:
450-
# * for AUTH - the default `netrc` usage could be configured through a common
451-
# attribute.
452459
# * for index/downloader config. This includes all of those attributes for
453460
# overrides, etc. Index overrides per platform could be also used here.
454461
# * for whl selection - selecting preferences of which `platform_tag`s we should use
455462
# for what. We could also model the `cp313t` freethreaded as separate platforms.
456463
)
457464

458465
return struct(
466+
auth_patterns = defaults.get("auth_patterns", {}),
467+
netrc = defaults.get("netrc", None),
459468
platforms = {
460469
name: _plat(**values)
461470
for name, values in defaults["platforms"].items()
@@ -858,7 +867,7 @@ This is only used if the {envvar}`RULES_PYTHON_ENABLE_PIPSTAR` is enabled.
858867
""",
859868
),
860869
# The values for PEP508 env marker evaluation during the lock file parsing
861-
}
870+
} | AUTH_ATTRS
862871

863872
_SUPPORTED_PEP508_KEYS = [
864873
"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(
@@ -1225,6 +1231,10 @@ def _test_build_pipstar_platform(env):
12251231
_mod(
12261232
name = "rules_python",
12271233
default = [
1234+
_default(
1235+
netrc = "my_netrc",
1236+
auth_patterns = {"foo": "bar"},
1237+
),
12281238
_default(
12291239
platform = "myplat",
12301240
os_name = "linux",
@@ -1241,6 +1251,8 @@ def _test_build_pipstar_platform(env):
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)