2018-12-15 00:08:54 +00:00
|
|
|
import getpass
|
|
|
|
from distutils import log
|
2016-02-23 06:06:55 +00:00
|
|
|
from distutils.command import upload as orig
|
|
|
|
|
|
|
|
|
|
|
|
class upload(orig.upload):
|
|
|
|
"""
|
2018-12-15 00:08:54 +00:00
|
|
|
Override default upload behavior to obtain password
|
|
|
|
in a variety of different ways.
|
2016-02-23 06:06:55 +00:00
|
|
|
"""
|
|
|
|
|
2018-12-15 00:08:54 +00:00
|
|
|
def run(self):
|
|
|
|
try:
|
|
|
|
orig.upload.run(self)
|
|
|
|
finally:
|
|
|
|
self.announce(
|
|
|
|
"WARNING: Uploading via this command is deprecated, use twine "
|
|
|
|
"to upload instead (https://pypi.org/p/twine/)",
|
|
|
|
log.WARN
|
|
|
|
)
|
|
|
|
|
2016-02-23 06:06:55 +00:00
|
|
|
def finalize_options(self):
|
|
|
|
orig.upload.finalize_options(self)
|
2018-12-15 00:08:54 +00:00
|
|
|
self.username = (
|
|
|
|
self.username or
|
|
|
|
getpass.getuser()
|
|
|
|
)
|
|
|
|
# Attempt to obtain password. Short circuit evaluation at the first
|
|
|
|
# sign of success.
|
|
|
|
self.password = (
|
|
|
|
self.password or
|
|
|
|
self._load_password_from_keyring() or
|
|
|
|
self._prompt_for_password()
|
|
|
|
)
|
2016-02-23 06:06:55 +00:00
|
|
|
|
|
|
|
def _load_password_from_keyring(self):
|
|
|
|
"""
|
|
|
|
Attempt to load password from keyring. Suppress Exceptions.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
keyring = __import__('keyring')
|
2018-12-15 00:08:54 +00:00
|
|
|
return keyring.get_password(self.repository, self.username)
|
2016-02-23 06:06:55 +00:00
|
|
|
except Exception:
|
|
|
|
pass
|
2018-12-15 00:08:54 +00:00
|
|
|
|
|
|
|
def _prompt_for_password(self):
|
|
|
|
"""
|
|
|
|
Prompt for a password on the tty. Suppress Exceptions.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return getpass.getpass()
|
|
|
|
except (Exception, KeyboardInterrupt):
|
|
|
|
pass
|