@@ -18,7 +18,7 @@ For historic reasons, pip_repositories() is defined in //python:pip.bzl.
18
18
"""
19
19
20
20
load ("@bazel_tools//tools/build_defs/repo:http.bzl" , _http_archive = "http_archive" )
21
- load ("@bazel_tools//tools/build_defs/repo:utils.bzl" , "maybe" )
21
+ load ("@bazel_tools//tools/build_defs/repo:utils.bzl" , "maybe" , "read_netrc" , "read_user_netrc" , "use_netrc" )
22
22
load ("//python/private:bzlmod_enabled.bzl" , "BZLMOD_ENABLED" )
23
23
load ("//python/private:coverage_deps.bzl" , "coverage_dep" )
24
24
load (
@@ -85,6 +85,28 @@ def is_standalone_interpreter(rctx, python_interpreter_path):
85
85
),
86
86
]).return_code == 0
87
87
88
+ def _get_auth (rctx , urls ):
89
+ """Utility for retrieving netrc-based authentication parameters for repository download rules used in python_repository.
90
+
91
+ The implementation below is copied directly from Bazel's implementation of `http_archive`.
92
+ Accordingly, the return value of this function should be used identically as the `auth` parameter of `http_archive`.
93
+ Reference: https://github.com/bazelbuild/bazel/blob/6.3.2/tools/build_defs/repo/http.bzl#L109
94
+
95
+ Args:
96
+ rctx (repository_ctx): The repository rule's context object.
97
+ urls: A list of URLs from which assets will be downloaded.
98
+
99
+ Returns:
100
+ dict: A map of authentication parameters by URL.
101
+ """
102
+ if rctx .attr .netrc :
103
+ netrc = read_netrc (rctx , rctx .attr .netrc )
104
+ elif "NETRC" in rctx .os .environ :
105
+ netrc = read_netrc (rctx , rctx .os .environ ["NETRC" ])
106
+ else :
107
+ netrc = read_user_netrc (rctx )
108
+ return use_netrc (netrc , urls , rctx .attr .auth_patterns )
109
+
88
110
def _python_repository_impl (rctx ):
89
111
if rctx .attr .distutils and rctx .attr .distutils_content :
90
112
fail ("Only one of (distutils, distutils_content) should be set." )
@@ -96,19 +118,22 @@ def _python_repository_impl(rctx):
96
118
python_short_version = python_version .rpartition ("." )[0 ]
97
119
release_filename = rctx .attr .release_filename
98
120
urls = rctx .attr .urls or [rctx .attr .url ]
121
+ auth = _get_auth (rctx , urls )
99
122
100
123
if release_filename .endswith (".zst" ):
101
124
rctx .download (
102
125
url = urls ,
103
126
sha256 = rctx .attr .sha256 ,
104
127
output = release_filename ,
128
+ auth = auth ,
105
129
)
106
130
unzstd = rctx .which ("unzstd" )
107
131
if not unzstd :
108
132
url = rctx .attr .zstd_url .format (version = rctx .attr .zstd_version )
109
133
rctx .download_and_extract (
110
134
url = url ,
111
135
sha256 = rctx .attr .zstd_sha256 ,
136
+ auth = auth ,
112
137
)
113
138
working_directory = "zstd-{version}" .format (version = rctx .attr .zstd_version )
114
139
@@ -146,6 +171,7 @@ def _python_repository_impl(rctx):
146
171
url = urls ,
147
172
sha256 = rctx .attr .sha256 ,
148
173
stripPrefix = rctx .attr .strip_prefix ,
174
+ auth = auth ,
149
175
)
150
176
151
177
patches = rctx .attr .patches
@@ -348,11 +374,13 @@ py_cc_toolchain(
348
374
rctx .file ("BUILD.bazel" , build_content )
349
375
350
376
attrs = {
377
+ "auth_patterns" : rctx .attr .auth_patterns ,
351
378
"coverage_tool" : rctx .attr .coverage_tool ,
352
379
"distutils" : rctx .attr .distutils ,
353
380
"distutils_content" : rctx .attr .distutils_content ,
354
381
"ignore_root_user_error" : rctx .attr .ignore_root_user_error ,
355
382
"name" : rctx .attr .name ,
383
+ "netrc" : rctx .attr .netrc ,
356
384
"patches" : rctx .attr .patches ,
357
385
"platform" : platform ,
358
386
"python_version" : python_version ,
@@ -372,6 +400,9 @@ python_repository = repository_rule(
372
400
_python_repository_impl ,
373
401
doc = "Fetches the external tools needed for the Python toolchain." ,
374
402
attrs = {
403
+ "auth_patterns" : attr .string_dict (
404
+ doc = "Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive" ,
405
+ ),
375
406
"coverage_tool" : attr .string (
376
407
# Mirrors the definition at
377
408
# https://github.com/bazelbuild/bazel/blob/master/src/main/starlark/builtins_bzl/common/python/py_runtime_rule.bzl
@@ -412,6 +443,9 @@ For more information see the official bazel docs
412
443
doc = "Whether the check for root should be ignored or not. This causes cache misses with .pyc files." ,
413
444
mandatory = False ,
414
445
),
446
+ "netrc" : attr .string (
447
+ doc = ".netrc file to use for authentication; mirrors the eponymous attribute from http_archive" ,
448
+ ),
415
449
"patches" : attr .label_list (
416
450
doc = "A list of patch files to apply to the unpacked interpreter" ,
417
451
mandatory = False ,
0 commit comments