From bd0312a4841e9488cf43ae4afb3b58d44eebbbb1 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 19 Sep 2011 10:04:23 -0700 Subject: [PATCH] Support ~/.netrc for HTTP Basic authentication If repo tries to access a URL over HTTP and the user needs to authenticate, offer a match from ~/.netrc. This matches behavior with the Git command line client. Change-Id: I803f3c5d562177ea0330941350cff3cc1e1bef08 Signed-off-by: Shawn O. Pearce --- main.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/main.py b/main.py index 9c545b31..f80bd45e 100755 --- a/main.py +++ b/main.py @@ -22,6 +22,7 @@ if __name__ == '__main__': del sys.argv[-1] del magic +import netrc import optparse import os import re @@ -254,6 +255,17 @@ class _UserAgentHandler(urllib2.BaseHandler): def init_http(): handlers = [_UserAgentHandler()] + mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() + try: + n = netrc.netrc() + for host in n.hosts: + p = n.hosts[host] + mgr.add_password(None, 'http://%s/' % host, p[0], p[2]) + mgr.add_password(None, 'https://%s/' % host, p[0], p[2]) + except netrc.NetrcParseError: + pass + handlers.append(urllib2.HTTPBasicAuthHandler(mgr)) + if 'http_proxy' in os.environ: url = os.environ['http_proxy'] handlers.append(urllib2.ProxyHandler({'http': url, 'https': url}))