diff --git a/Lib/distutils/command/upload.py b/Lib/distutils/command/upload.py --- a/Lib/distutils/command/upload.py +++ b/Lib/distutils/command/upload.py @@ -2,6 +2,7 @@ Implements the Distutils 'upload' subcommand (upload package to PyPI).""" import os +import sys import socket import platform from urllib2 import urlopen, Request, HTTPError @@ -9,6 +10,7 @@ import urlparse import cStringIO as StringIO from hashlib import md5 +import getpass from distutils.errors import DistutilsError, DistutilsOptionError from distutils.core import PyPIRCCommand @@ -41,18 +43,33 @@ raise DistutilsOptionError( "Must use --sign for --identity to have meaning" ) + config = self._read_pypirc() - if config != {}: - self.username = config['username'] - self.password = config['password'] - self.repository = config['repository'] - self.realm = config['realm'] + # Empty config dict indicates non-existing or invalid .pypirc file. + # In this case, proceed using default settings (no authentication). + if not config: + return - # getting the password from the distribution - # if previously set by the register command - if not self.password and self.distribution.password: + # Use values provided via .pypirc file. If password was not + # specified in there, config['password'] is None. + self.username = config['username'] + self.password = config['password'] + self.repository = config['repository'] + self.realm = config['realm'] + + # Use password if previously set by the register command. + if self.password is None and self.distribution.password: self.password = self.distribution.password + # If the password is still undefined, prompt for it (if stdin is + # attached to a terminal) or read directly from stdin. + if self.password is None: + if sys.stdin.isatty(): + self.password = getpass.getpass('Enter password: ') + else: + self.announce("Reading password from stdin", log.INFO) + self.password = sys.stdin.readline().rstrip() + def run(self): if not self.distribution.dist_files: raise DistutilsOptionError("No dist file created in earlier command")