瀏覽代碼

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>
Shawn O. Pearce 14 年之前
父節點
當前提交
bd0312a484
共有 1 個文件被更改,包括 12 次插入0 次删除
  1. 12 0
      main.py

+ 12 - 0
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}))