Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the logic to read user netrc file #14588

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions tools/build_defs/repo/http.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ load(
":utils.bzl",
"patch",
"read_netrc",
"read_user_netrc",
"update_attrs",
"use_netrc",
"workspace_and_buildfile",
Expand Down Expand Up @@ -77,21 +78,9 @@ def _get_auth(ctx, urls):
"""Given the list of URLs obtain the correct auth dict."""
if ctx.attr.netrc:
netrc = read_netrc(ctx, ctx.attr.netrc)
return use_netrc(netrc, urls, ctx.attr.auth_patterns)

if "HOME" in ctx.os.environ and not ctx.os.name.startswith("windows"):
netrcfile = "%s/.netrc" % (ctx.os.environ["HOME"])
if ctx.execute(["test", "-f", netrcfile]).return_code == 0:
netrc = read_netrc(ctx, netrcfile)
return use_netrc(netrc, urls, ctx.attr.auth_patterns)

if "USERPROFILE" in ctx.os.environ and ctx.os.name.startswith("windows"):
netrcfile = "%s/.netrc" % (ctx.os.environ["USERPROFILE"])
if ctx.path(netrcfile).exists:
netrc = read_netrc(ctx, netrcfile)
return use_netrc(netrc, urls, ctx.attr.auth_patterns)

return {}
else:
netrc = read_user_netrc(ctx)
return use_netrc(netrc, urls, ctx.attr.auth_patterns)

def _http_archive_impl(ctx):
"""Implementation of the http_archive rule."""
Expand Down
22 changes: 22 additions & 0 deletions tools/build_defs/repo/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,25 @@ def use_netrc(netrc, urls, patterns):
}

return auth

def read_user_netrc(ctx):
"""Read user's default netrc file.
Args:
ctx: The repository context of the repository rule calling this utility function.
Returns:
dict mapping a machine names to a dict with the information provided about them.
"""
if ctx.os.name.startswith("windows"):
home_dir = ctx.os.environ.get("USERPROFILE", "")
else:
home_dir = ctx.os.environ.get("HOME", "")

if not home_dir:
return {}

netrcfile = "{}/.netrc".format(home_dir)
if not ctx.path(netrcfile).exists:
return {}
return read_netrc(ctx, netrcfile)