@@ -18,7 +18,7 @@ For historic reasons, pip_repositories() is defined in //python:pip.bzl.
1818"""
1919
2020load ("@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" )
2222load ("//python/private:bzlmod_enabled.bzl" , "BZLMOD_ENABLED" )
2323load ("//python/private:coverage_deps.bzl" , "coverage_dep" )
2424load (
@@ -85,6 +85,28 @@ def is_standalone_interpreter(rctx, python_interpreter_path):
8585 ),
8686 ]).return_code == 0
8787
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+
88110def _python_repository_impl (rctx ):
89111 if rctx .attr .distutils and rctx .attr .distutils_content :
90112 fail ("Only one of (distutils, distutils_content) should be set." )
@@ -96,19 +118,22 @@ def _python_repository_impl(rctx):
96118 python_short_version = python_version .rpartition ("." )[0 ]
97119 release_filename = rctx .attr .release_filename
98120 urls = rctx .attr .urls or [rctx .attr .url ]
121+ auth = _get_auth (rctx , urls )
99122
100123 if release_filename .endswith (".zst" ):
101124 rctx .download (
102125 url = urls ,
103126 sha256 = rctx .attr .sha256 ,
104127 output = release_filename ,
128+ auth = auth ,
105129 )
106130 unzstd = rctx .which ("unzstd" )
107131 if not unzstd :
108132 url = rctx .attr .zstd_url .format (version = rctx .attr .zstd_version )
109133 rctx .download_and_extract (
110134 url = url ,
111135 sha256 = rctx .attr .zstd_sha256 ,
136+ auth = auth ,
112137 )
113138 working_directory = "zstd-{version}" .format (version = rctx .attr .zstd_version )
114139
@@ -146,6 +171,7 @@ def _python_repository_impl(rctx):
146171 url = urls ,
147172 sha256 = rctx .attr .sha256 ,
148173 stripPrefix = rctx .attr .strip_prefix ,
174+ auth = auth ,
149175 )
150176
151177 patches = rctx .attr .patches
@@ -348,11 +374,13 @@ py_cc_toolchain(
348374 rctx .file ("BUILD.bazel" , build_content )
349375
350376 attrs = {
377+ "auth_patterns" : rctx .attr .auth_patterns ,
351378 "coverage_tool" : rctx .attr .coverage_tool ,
352379 "distutils" : rctx .attr .distutils ,
353380 "distutils_content" : rctx .attr .distutils_content ,
354381 "ignore_root_user_error" : rctx .attr .ignore_root_user_error ,
355382 "name" : rctx .attr .name ,
383+ "netrc" : rctx .attr .netrc ,
356384 "patches" : rctx .attr .patches ,
357385 "platform" : platform ,
358386 "python_version" : python_version ,
@@ -372,6 +400,9 @@ python_repository = repository_rule(
372400 _python_repository_impl ,
373401 doc = "Fetches the external tools needed for the Python toolchain." ,
374402 attrs = {
403+ "auth_patterns" : attr .string_dict (
404+ doc = "Override mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archive" ,
405+ ),
375406 "coverage_tool" : attr .string (
376407 # Mirrors the definition at
377408 # 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
412443 doc = "Whether the check for root should be ignored or not. This causes cache misses with .pyc files." ,
413444 mandatory = False ,
414445 ),
446+ "netrc" : attr .string (
447+ doc = ".netrc file to use for authentication; mirrors the eponymous attribute from http_archive" ,
448+ ),
415449 "patches" : attr .label_list (
416450 doc = "A list of patch files to apply to the unpacked interpreter" ,
417451 mandatory = False ,
0 commit comments