diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/distutils/command/register.py | 97 | ||||
-rw-r--r-- | Lib/distutils/command/upload.py | 41 | ||||
-rw-r--r-- | Lib/distutils/core.py | 1 | ||||
-rw-r--r-- | Lib/distutils/dist.py | 7 | ||||
-rw-r--r-- | Lib/distutils/tests/test_dist.py | 47 |
5 files changed, 80 insertions, 113 deletions
diff --git a/Lib/distutils/command/register.py b/Lib/distutils/command/register.py index a9ab361..5c588ef 100644 --- a/Lib/distutils/command/register.py +++ b/Lib/distutils/command/register.py @@ -8,29 +8,37 @@ Implements the Distutils 'register' command (register with the repository). __revision__ = "$Id$" import os, string, urllib2, getpass, urlparse -import StringIO +import StringIO, ConfigParser -from distutils.core import PyPIRCCommand +from distutils.core import Command from distutils.errors import * -from distutils import log -class register(PyPIRCCommand): +class register(Command): description = ("register the distribution with the Python package index") - user_options = PyPIRCCommand.user_options + [ + + DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi' + + user_options = [ + ('repository=', 'r', + "url of repository [default: %s]"%DEFAULT_REPOSITORY), ('list-classifiers', None, 'list the valid Trove classifiers'), + ('show-response', None, + 'display full response text from server'), ] - boolean_options = PyPIRCCommand.boolean_options + [ - 'verify', 'list-classifiers'] + boolean_options = ['verify', 'show-response', 'list-classifiers'] def initialize_options(self): - PyPIRCCommand.initialize_options(self) + self.repository = None + self.show_response = 0 self.list_classifiers = 0 + def finalize_options(self): + if self.repository is None: + self.repository = self.DEFAULT_REPOSITORY + def run(self): - self.finalize_options() - self._set_config() self.check_metadata() if self.dry_run: self.verify_metadata() @@ -69,23 +77,6 @@ class register(PyPIRCCommand): "or (maintainer and maintainer_email) " + "must be supplied") - def _set_config(self): - ''' Reads the configuration file and set attributes. - ''' - config = self._read_pypirc() - if config != {}: - self.username = config['username'] - self.password = config['password'] - self.repository = config['repository'] - self.realm = config['realm'] - self.has_config = True - else: - if self.repository not in ('pypi', self.DEFAULT_REPOSITORY): - raise ValueError('%s not found in .pypirc' % self.repository) - if self.repository == 'pypi': - self.repository = self.DEFAULT_REPOSITORY - self.has_config = False - def classifiers(self): ''' Fetch the list of classifiers from the server. ''' @@ -99,7 +90,6 @@ class register(PyPIRCCommand): (code, result) = self.post_to_server(self.build_post_data('verify')) print 'Server response (%s): %s'%(code, result) - def send_metadata(self): ''' Send the metadata to the package index server. @@ -109,14 +99,10 @@ class register(PyPIRCCommand): First we try to read the username/password from $HOME/.pypirc, which is a ConfigParser-formatted file with a section - [distutils] containing username and password entries (both + [server-login] containing username and password entries (both in clear text). Eg: - [distutils] - index-servers = - pypi - - [pypi] + [server-login] username: fred password: sekrit @@ -128,15 +114,21 @@ class register(PyPIRCCommand): 3. set the password to a random string and email the user. ''' + choice = 'x' + username = password = '' + # see if we can short-cut and get the username/password from the # config - if self.has_config: - choice = '1' - username = self.username - password = self.password - else: - choice = 'x' - username = password = '' + config = None + if 'HOME' in os.environ: + rc = os.path.join(os.environ['HOME'], '.pypirc') + if os.path.exists(rc): + print 'Using PyPI login from %s'%rc + config = ConfigParser.ConfigParser() + config.read(rc) + username = config.get('server-login', 'username') + password = config.get('server-login', 'password') + choice = '1' # get the user's login info choices = '1 2 3 4'.split() @@ -163,24 +155,32 @@ Your selection [default 1]: ''', # set up the authentication auth = urllib2.HTTPPasswordMgr() host = urlparse.urlparse(self.repository)[1] - auth.add_password(self.realm, host, username, password) + auth.add_password('pypi', host, username, password) + # send the info to the server and report the result code, result = self.post_to_server(self.build_post_data('submit'), auth) - print 'Server response (%s): %s' % (code, result) + print 'Server response (%s): %s'%(code, result) # possibly save the login - if not self.has_config and code == 200: + if 'HOME' in os.environ and config is None and code == 200: + rc = os.path.join(os.environ['HOME'], '.pypirc') print 'I can store your PyPI login so future submissions will be faster.' - print '(the login will be stored in %s)' % self._get_rc_file() + print '(the login will be stored in %s)'%rc choice = 'X' while choice.lower() not in 'yn': choice = raw_input('Save your login (y/N)?') if not choice: choice = 'n' if choice.lower() == 'y': - self._store_pypirc(username, password) - + f = open(rc, 'w') + f.write('[server-login]\nusername:%s\npassword:%s\n'%( + username, password)) + f.close() + try: + os.chmod(rc, 0600) + except: + pass elif choice == '2': data = {':action': 'user'} data['name'] = data['password'] = data['email'] = '' @@ -243,8 +243,7 @@ Your selection [default 1]: ''', def post_to_server(self, data, auth=None): ''' Post a query to the server, and return a string response. ''' - self.announce('Registering %s to %s' % (data['name'], - self.repository), log.INFO) + # Build up the MIME payload for the urllib2 POST data boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' sep_boundary = '\n--' + boundary diff --git a/Lib/distutils/command/upload.py b/Lib/distutils/command/upload.py index daf6811..301a159 100644 --- a/Lib/distutils/command/upload.py +++ b/Lib/distutils/command/upload.py @@ -3,7 +3,7 @@ Implements the Distutils 'upload' subcommand (upload package to PyPI).""" from distutils.errors import * -from distutils.core import PyPIRCCommand +from distutils.core import Command from distutils.spawn import spawn from distutils import log from hashlib import md5 @@ -16,38 +16,53 @@ import base64 import urlparse import cStringIO as StringIO -class upload(PyPIRCCommand): +class upload(Command): description = "upload binary package to PyPI" - user_options = PyPIRCCommand.user_options + [ + DEFAULT_REPOSITORY = 'http://pypi.python.org/pypi' + + user_options = [ + ('repository=', 'r', + "url of repository [default: %s]" % DEFAULT_REPOSITORY), + ('show-response', None, + 'display full response text from server'), ('sign', 's', 'sign files to upload using gpg'), ('identity=', 'i', 'GPG identity used to sign files'), ] - - boolean_options = PyPIRCCommand.boolean_options + ['sign'] + boolean_options = ['show-response', 'sign'] def initialize_options(self): - PyPIRCCommand.initialize_options(self) self.username = '' self.password = '' + self.repository = '' self.show_response = 0 self.sign = False self.identity = None def finalize_options(self): - PyPIRCCommand.finalize_options(self) if self.identity and not self.sign: 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'] + if 'HOME' in os.environ: + rc = os.path.join(os.environ['HOME'], '.pypirc') + if os.path.exists(rc): + self.announce('Using PyPI login from %s' % rc) + config = ConfigParser.ConfigParser({ + 'username':'', + 'password':'', + 'repository':''}) + config.read(rc) + if not self.repository: + self.repository = config.get('server-login', 'repository') + if not self.username: + self.username = config.get('server-login', 'username') + if not self.password: + self.password = config.get('server-login', 'password') + if not self.repository: + self.repository = self.DEFAULT_REPOSITORY def run(self): if not self.distribution.dist_files: diff --git a/Lib/distutils/core.py b/Lib/distutils/core.py index de9ce7d..32b9026 100644 --- a/Lib/distutils/core.py +++ b/Lib/distutils/core.py @@ -20,7 +20,6 @@ from distutils.util import grok_environment_error # Mainly import these so setup scripts can "from distutils.core import" them. from distutils.dist import Distribution from distutils.cmd import Command -from distutils.config import PyPIRCCommand from distutils.extension import Extension # This is a barebones help message generated displayed when the user diff --git a/Lib/distutils/dist.py b/Lib/distutils/dist.py index 0b13c1e..d098cb9 100644 --- a/Lib/distutils/dist.py +++ b/Lib/distutils/dist.py @@ -343,9 +343,10 @@ Common commands: (see '--help-commands' for more) user_filename = "pydistutils.cfg" # And look for the user config file - user_file = os.path.join(os.path.expanduser('~'), user_filename) - if os.path.isfile(user_file): - files.append(user_file) + if 'HOME' in os.environ: + user_file = os.path.join(os.environ.get('HOME'), user_filename) + if os.path.isfile(user_file): + files.append(user_file) # All platforms support local setup.cfg local_file = "setup.cfg" diff --git a/Lib/distutils/tests/test_dist.py b/Lib/distutils/tests/test_dist.py index 2857263..5ae7933 100644 --- a/Lib/distutils/tests/test_dist.py +++ b/Lib/distutils/tests/test_dist.py @@ -55,7 +55,6 @@ class DistributionTestCase(unittest.TestCase): self.assertEqual(d.get_command_packages(), ["distutils.command"]) def test_command_packages_cmdline(self): - from distutils.tests.test_dist import test_dist sys.argv.extend(["--command-packages", "foo.bar,distutils.tests", "test_dist", @@ -66,7 +65,6 @@ class DistributionTestCase(unittest.TestCase): self.assertEqual(d.get_command_packages(), ["distutils.command", "foo.bar", "distutils.tests"]) cmd = d.get_command_obj("test_dist") - print cmd.__class__, test_dist self.assert_(isinstance(cmd, test_dist)) self.assertEqual(cmd.sample_option, "sometext") @@ -181,54 +179,9 @@ class MetadataTestCase(unittest.TestCase): dist.metadata.write_pkg_file(sio) return sio.getvalue() - def test_custom_pydistutils(self): - # fixes #2166 - # make sure pydistutils.cfg is found - old = {} - for env in ('HOME', 'HOMEPATH', 'HOMEDRIVE'): - value = os.environ.get(env) - old[env] = value - if value is not None: - del os.environ[env] - - if os.name == 'posix': - user_filename = ".pydistutils.cfg" - else: - user_filename = "pydistutils.cfg" - - curdir = os.path.dirname(__file__) - user_filename = os.path.join(curdir, user_filename) - f = open(user_filename, 'w') - f.write('.') - f.close() - - try: - dist = distutils.dist.Distribution() - - # linux-style - if sys.platform in ('linux', 'darwin'): - os.environ['HOME'] = curdir - files = dist.find_config_files() - self.assert_(user_filename in files) - - # win32-style - if sys.platform == 'win32': - # home drive should be found - os.environ['HOMEPATH'] = curdir - files = dist.find_config_files() - self.assert_(user_filename in files) - finally: - for key, value in old.items(): - if value is None: - continue - os.environ[key] = value - os.remove(user_filename) def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(DistributionTestCase)) suite.addTest(unittest.makeSuite(MetadataTestCase)) return suite - -if __name__ == "__main__": - unittest.main(defaultTest="test_suite") |