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 <sop@google.com>
This commit is contained in:
Shawn O. Pearce 2011-09-19 10:04:23 -07:00
parent 334851e4b6
commit bd0312a484

12
main.py
View File

@ -22,6 +22,7 @@ if __name__ == '__main__':
del sys.argv[-1] del sys.argv[-1]
del magic del magic
import netrc
import optparse import optparse
import os import os
import re import re
@ -254,6 +255,17 @@ class _UserAgentHandler(urllib2.BaseHandler):
def init_http(): def init_http():
handlers = [_UserAgentHandler()] 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: if 'http_proxy' in os.environ:
url = os.environ['http_proxy'] url = os.environ['http_proxy']
handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) handlers.append(urllib2.ProxyHandler({'http': url, 'https': url}))