upload.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. # Attempt to obtain password. Short circuit evaluation at the first
  11. # sign of success.
  12. self.password = (
  13. self.password or
  14. self._load_password_from_keyring() or
  15. self._prompt_for_password()
  16. )
  17. def _load_password_from_keyring(self):
  18. """
  19. Attempt to load password from keyring. Suppress Exceptions.
  20. """
  21. try:
  22. keyring = __import__('keyring')
  23. return keyring.get_password(self.repository, self.username)
  24. except Exception:
  25. pass
  26. def _prompt_for_password(self):
  27. """
  28. Prompt for a password on the tty. Suppress Exceptions.
  29. """
  30. try:
  31. return getpass.getpass()
  32. except (Exception, KeyboardInterrupt):
  33. pass