diff --git a/main.py b/main.py index 665a655b..34dd27dc 100755 --- a/main.py +++ b/main.py @@ -22,6 +22,7 @@ if __name__ == '__main__': del sys.argv[-1] del magic +import getpass import netrc import optparse import os @@ -276,7 +277,25 @@ class _UserAgentHandler(urllib2.BaseHandler): req.add_header('User-Agent', _UserAgent()) return req +def _AddPasswordFromUserInput(handler, msg, req): + # If repo could not find auth info from netrc, try to get it from user input + url = req.get_full_url() + user, password = handler.passwd.find_user_password(None, url) + if user is None: + print msg + try: + user = raw_input('User: ') + password = getpass.getpass() + except KeyboardInterrupt: + return + handler.passwd.add_password(None, url, user, password) + class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler): + def http_error_401(self, req, fp, code, msg, headers): + _AddPasswordFromUserInput(self, msg, req) + return urllib2.HTTPBasicAuthHandler.http_error_401( + self, req, fp, code, msg, headers) + def http_error_auth_reqed(self, authreq, host, req, headers): try: old_add_header = req.add_header @@ -295,6 +314,11 @@ class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler): raise class _DigestAuthHandler(urllib2.HTTPDigestAuthHandler): + def http_error_401(self, req, fp, code, msg, headers): + _AddPasswordFromUserInput(self, msg, req) + return urllib2.HTTPDigestAuthHandler.http_error_401( + self, req, fp, code, msg, headers) + def http_error_auth_reqed(self, auth_header, host, req, headers): try: old_add_header = req.add_header