upload.py 1.1 KB

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