Skip to content

Commit f421474

Browse files
linzhpcopybara-github
authored andcommitted
Expose the logic to read user netrc file
Repository rules that download repositories from internal website often need to read users `.netrc` files. This PR exposes the logic for other repository rules to use. Closes #14588. PiperOrigin-RevId: 423291624
1 parent 43c7b97 commit f421474

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

tools/build_defs/repo/http.bzl

+4-15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ load(
3434
":utils.bzl",
3535
"patch",
3636
"read_netrc",
37+
"read_user_netrc",
3738
"update_attrs",
3839
"use_netrc",
3940
"workspace_and_buildfile",
@@ -77,21 +78,9 @@ def _get_auth(ctx, urls):
7778
"""Given the list of URLs obtain the correct auth dict."""
7879
if ctx.attr.netrc:
7980
netrc = read_netrc(ctx, ctx.attr.netrc)
80-
return use_netrc(netrc, urls, ctx.attr.auth_patterns)
81-
82-
if "HOME" in ctx.os.environ and not ctx.os.name.startswith("windows"):
83-
netrcfile = "%s/.netrc" % (ctx.os.environ["HOME"])
84-
if ctx.execute(["test", "-f", netrcfile]).return_code == 0:
85-
netrc = read_netrc(ctx, netrcfile)
86-
return use_netrc(netrc, urls, ctx.attr.auth_patterns)
87-
88-
if "USERPROFILE" in ctx.os.environ and ctx.os.name.startswith("windows"):
89-
netrcfile = "%s/.netrc" % (ctx.os.environ["USERPROFILE"])
90-
if ctx.path(netrcfile).exists:
91-
netrc = read_netrc(ctx, netrcfile)
92-
return use_netrc(netrc, urls, ctx.attr.auth_patterns)
93-
94-
return {}
81+
else:
82+
netrc = read_user_netrc(ctx)
83+
return use_netrc(netrc, urls, ctx.attr.auth_patterns)
9584

9685
def _http_archive_impl(ctx):
9786
"""Implementation of the http_archive rule."""

tools/build_defs/repo/utils.bzl

+22
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,25 @@ def use_netrc(netrc, urls, patterns):
385385
}
386386

387387
return auth
388+
389+
def read_user_netrc(ctx):
390+
"""Read user's default netrc file.
391+
392+
Args:
393+
ctx: The repository context of the repository rule calling this utility function.
394+
395+
Returns:
396+
dict mapping a machine names to a dict with the information provided about them.
397+
"""
398+
if ctx.os.name.startswith("windows"):
399+
home_dir = ctx.os.environ.get("USERPROFILE", "")
400+
else:
401+
home_dir = ctx.os.environ.get("HOME", "")
402+
403+
if not home_dir:
404+
return {}
405+
406+
netrcfile = "{}/.netrc".format(home_dir)
407+
if not ctx.path(netrcfile).exists:
408+
return {}
409+
return read_netrc(ctx, netrcfile)

0 commit comments

Comments
 (0)