1. Ronny Pfannschmidt
  2. pip-globalopts

Commits

Jannis Leidel  committed 77f20f9

Added option to disable prompting for input. Also added ability to interactively ask for username and/or password in case a request requires authentication.

  • Participants
  • Parent commits 9627015
  • Branches default

Comments (0)

Files changed (3)

File pip/basecommand.py

View file
  • Ignore whitespace
         # Make sure we have all global options carried over
         for attr in ['log', 'venv', 'proxy', 'venv_base', 'require_venv',
                      'respect_venv', 'log_explicit_levels', 'log_file',
-                     'timeout', 'default_vcs', 'skip_requirements_regex']:
+                     'timeout', 'default_vcs', 'skip_requirements_regex',
+                     'no_input']:
             setattr(options, attr, getattr(initial_options, attr) or getattr(options, attr))
         options.quiet += initial_options.quiet
         options.verbose += initial_options.verbose
 
         socket.setdefaulttimeout(options.timeout or None)
 
-        urlopen.setup(proxystr=options.proxy)
+        urlopen.setup(proxystr=options.proxy, prompting=not options.no_input)
 
         exit = 0
         try:

File pip/baseparser.py

View file
  • Ignore whitespace
     metavar='FILENAME',
     default=default_log_file,
     help=optparse.SUPPRESS_HELP)
+parser.add_option(
+    # Don't ask for input
+    '--no-input',
+    dest='no_input',
+    action='store_true',
+    default=False,
+    help=optparse.SUPPRESS_HELP)
 
 parser.add_option(
     '--proxy',

File pip/download.py

View file
  • Ignore whitespace
         """
         url, username, password = self.extract_credentials(url)
         if username is None:
-            return urllib2.urlopen(self.get_request(url))
-        return self.get_response(url, username, password)
+            try:
+                response = urllib2.urlopen(self.get_request(url))
+            except urllib2.HTTPError, e:
+                if e.code != 401:
+                    raise
+                response = self.get_response(url)
+        else:
+            response = self.get_response(url, username, password)
+        return response
 
     def get_request(self, url):
         """
         stored_username, stored_password = self.passman.find_user_password(None, netloc)
         # see if we have a password stored
         if stored_username is None:
+            if username is None and self.prompting:
+                username = urllib.quote(raw_input('User for %s: ' % netloc))
+                password = urllib.quote(getpass.getpass('Password: '))
             if username and password:
                 self.passman.add_password(None, netloc, username, password)
             stored_username, stored_password = self.passman.find_user_password(None, netloc)
         # FIXME: should catch a 401 and offer to let the user reenter credentials
         return opener.open(req)
 
-    def setup(self, proxystr=''):
+    def setup(self, proxystr='', prompting=True):
         """
         Sets the proxy handler given the option passed on the command
         line.  If an empty string is passed it looks at the HTTP_PROXY
         environment variable.
         """
+        self.prompting = prompting
         proxy = self.get_proxy(proxystr)
         if proxy:
             proxy_support = urllib2.ProxyHandler({"http": proxy, "ftp": proxy})
             opener = urllib2.build_opener(proxy_support, urllib2.CacheFTPHandler)
             urllib2.install_opener(opener)
 
+    def parse_credentials(self, netloc):
+        if "@" in netloc:
+            userinfo = netloc.rsplit("@", 1)[0]
+            if ":" in userinfo:
+                return userinfo.split(":", 1)
+            return userinfo, None
+        return None, None
+
     def extract_credentials(self, url):
         """
         Extracts user/password from a url.
             result = urlparse.urlsplit(url)
         scheme, netloc, path, query, frag = result
 
-        if result.username is None:
+        username, password = self.parse_credentials(netloc)
+        if username is None:
             return url, None, None
-        elif result.password is None:
+        elif password is None and self.prompting:
             # remove the auth credentials from the url part
-            netloc = netloc.replace('%s@' % result.username, '', 1)
+            netloc = netloc.replace('%s@' % username, '', 1)
             # prompt for the password
-            prompt = 'Password for %s@%s: ' % (result.username, netloc)
-            result.password = urllib.quote(getpass.getpass(prompt))
+            prompt = 'Password for %s@%s: ' % (username, netloc)
+            password = urllib.quote(getpass.getpass(prompt))
         else:
             # remove the auth credentials from the url part
-            netloc = netloc.replace('%s:%s@' % (result.username, result.password), '', 1)
+            netloc = netloc.replace('%s:%s@' % (username, password), '', 1)
 
         target_url = urlparse.urlunsplit((scheme, netloc, path, query, frag))
-        return target_url, result.username, result.password
+        return target_url, username, password
 
     def get_proxy(self, proxystr=''):
         """