• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import getpass
2from distutils.command import upload as orig
3
4
5class upload(orig.upload):
6    """
7    Override default upload behavior to obtain password
8    in a variety of different ways.
9    """
10
11    def finalize_options(self):
12        orig.upload.finalize_options(self)
13        self.username = (
14            self.username or
15            getpass.getuser()
16        )
17        # Attempt to obtain password. Short circuit evaluation at the first
18        # sign of success.
19        self.password = (
20            self.password or
21            self._load_password_from_keyring() or
22            self._prompt_for_password()
23        )
24
25    def _load_password_from_keyring(self):
26        """
27        Attempt to load password from keyring. Suppress Exceptions.
28        """
29        try:
30            keyring = __import__('keyring')
31            return keyring.get_password(self.repository, self.username)
32        except Exception:
33            pass
34
35    def _prompt_for_password(self):
36        """
37        Prompt for a password on the tty. Suppress Exceptions.
38        """
39        try:
40            return getpass.getpass()
41        except (Exception, KeyboardInterrupt):
42            pass
43