+++ /dev/null
-"""distutils.command.register\r
-\r
-Implements the Distutils 'register' command (register with the repository).\r
-"""\r
-\r
-# created 2002/10/21, Richard Jones\r
-\r
-__revision__ = "$Id$"\r
-\r
-import urllib2\r
-import getpass\r
-import urlparse\r
-import StringIO\r
-from warnings import warn\r
-\r
-from distutils.core import PyPIRCCommand\r
-from distutils import log\r
-\r
-class register(PyPIRCCommand):\r
-\r
- description = ("register the distribution with the Python package index")\r
- user_options = PyPIRCCommand.user_options + [\r
- ('list-classifiers', None,\r
- 'list the valid Trove classifiers'),\r
- ('strict', None ,\r
- 'Will stop the registering if the meta-data are not fully compliant')\r
- ]\r
- boolean_options = PyPIRCCommand.boolean_options + [\r
- 'verify', 'list-classifiers', 'strict']\r
-\r
- sub_commands = [('check', lambda self: True)]\r
-\r
- def initialize_options(self):\r
- PyPIRCCommand.initialize_options(self)\r
- self.list_classifiers = 0\r
- self.strict = 0\r
-\r
- def finalize_options(self):\r
- PyPIRCCommand.finalize_options(self)\r
- # setting options for the `check` subcommand\r
- check_options = {'strict': ('register', self.strict),\r
- 'restructuredtext': ('register', 1)}\r
- self.distribution.command_options['check'] = check_options\r
-\r
- def run(self):\r
- self.finalize_options()\r
- self._set_config()\r
-\r
- # Run sub commands\r
- for cmd_name in self.get_sub_commands():\r
- self.run_command(cmd_name)\r
-\r
- if self.dry_run:\r
- self.verify_metadata()\r
- elif self.list_classifiers:\r
- self.classifiers()\r
- else:\r
- self.send_metadata()\r
-\r
- def check_metadata(self):\r
- """Deprecated API."""\r
- warn("distutils.command.register.check_metadata is deprecated, \\r
- use the check command instead", PendingDeprecationWarning)\r
- check = self.distribution.get_command_obj('check')\r
- check.ensure_finalized()\r
- check.strict = self.strict\r
- check.restructuredtext = 1\r
- check.run()\r
-\r
- def _set_config(self):\r
- ''' Reads the configuration file and set attributes.\r
- '''\r
- config = self._read_pypirc()\r
- if config != {}:\r
- self.username = config['username']\r
- self.password = config['password']\r
- self.repository = config['repository']\r
- self.realm = config['realm']\r
- self.has_config = True\r
- else:\r
- if self.repository not in ('pypi', self.DEFAULT_REPOSITORY):\r
- raise ValueError('%s not found in .pypirc' % self.repository)\r
- if self.repository == 'pypi':\r
- self.repository = self.DEFAULT_REPOSITORY\r
- self.has_config = False\r
-\r
- def classifiers(self):\r
- ''' Fetch the list of classifiers from the server.\r
- '''\r
- response = urllib2.urlopen(self.repository+'?:action=list_classifiers')\r
- log.info(response.read())\r
-\r
- def verify_metadata(self):\r
- ''' Send the metadata to the package index server to be checked.\r
- '''\r
- # send the info to the server and report the result\r
- (code, result) = self.post_to_server(self.build_post_data('verify'))\r
- log.info('Server response (%s): %s' % (code, result))\r
-\r
-\r
- def send_metadata(self):\r
- ''' Send the metadata to the package index server.\r
-\r
- Well, do the following:\r
- 1. figure who the user is, and then\r
- 2. send the data as a Basic auth'ed POST.\r
-\r
- First we try to read the username/password from $HOME/.pypirc,\r
- which is a ConfigParser-formatted file with a section\r
- [distutils] containing username and password entries (both\r
- in clear text). Eg:\r
-\r
- [distutils]\r
- index-servers =\r
- pypi\r
-\r
- [pypi]\r
- username: fred\r
- password: sekrit\r
-\r
- Otherwise, to figure who the user is, we offer the user three\r
- choices:\r
-\r
- 1. use existing login,\r
- 2. register as a new user, or\r
- 3. set the password to a random string and email the user.\r
-\r
- '''\r
- # see if we can short-cut and get the username/password from the\r
- # config\r
- if self.has_config:\r
- choice = '1'\r
- username = self.username\r
- password = self.password\r
- else:\r
- choice = 'x'\r
- username = password = ''\r
-\r
- # get the user's login info\r
- choices = '1 2 3 4'.split()\r
- while choice not in choices:\r
- self.announce('''\\r
-We need to know who you are, so please choose either:\r
- 1. use your existing login,\r
- 2. register as a new user,\r
- 3. have the server generate a new password for you (and email it to you), or\r
- 4. quit\r
-Your selection [default 1]: ''', log.INFO)\r
-\r
- choice = raw_input()\r
- if not choice:\r
- choice = '1'\r
- elif choice not in choices:\r
- print 'Please choose one of the four options!'\r
-\r
- if choice == '1':\r
- # get the username and password\r
- while not username:\r
- username = raw_input('Username: ')\r
- while not password:\r
- password = getpass.getpass('Password: ')\r
-\r
- # set up the authentication\r
- auth = urllib2.HTTPPasswordMgr()\r
- host = urlparse.urlparse(self.repository)[1]\r
- auth.add_password(self.realm, host, username, password)\r
- # send the info to the server and report the result\r
- code, result = self.post_to_server(self.build_post_data('submit'),\r
- auth)\r
- self.announce('Server response (%s): %s' % (code, result),\r
- log.INFO)\r
-\r
- # possibly save the login\r
- if code == 200:\r
- if self.has_config:\r
- # sharing the password in the distribution instance\r
- # so the upload command can reuse it\r
- self.distribution.password = password\r
- else:\r
- self.announce(('I can store your PyPI login so future '\r
- 'submissions will be faster.'), log.INFO)\r
- self.announce('(the login will be stored in %s)' % \\r
- self._get_rc_file(), log.INFO)\r
- choice = 'X'\r
- while choice.lower() not in 'yn':\r
- choice = raw_input('Save your login (y/N)?')\r
- if not choice:\r
- choice = 'n'\r
- if choice.lower() == 'y':\r
- self._store_pypirc(username, password)\r
-\r
- elif choice == '2':\r
- data = {':action': 'user'}\r
- data['name'] = data['password'] = data['email'] = ''\r
- data['confirm'] = None\r
- while not data['name']:\r
- data['name'] = raw_input('Username: ')\r
- while data['password'] != data['confirm']:\r
- while not data['password']:\r
- data['password'] = getpass.getpass('Password: ')\r
- while not data['confirm']:\r
- data['confirm'] = getpass.getpass(' Confirm: ')\r
- if data['password'] != data['confirm']:\r
- data['password'] = ''\r
- data['confirm'] = None\r
- print "Password and confirm don't match!"\r
- while not data['email']:\r
- data['email'] = raw_input(' EMail: ')\r
- code, result = self.post_to_server(data)\r
- if code != 200:\r
- log.info('Server response (%s): %s' % (code, result))\r
- else:\r
- log.info('You will receive an email shortly.')\r
- log.info(('Follow the instructions in it to '\r
- 'complete registration.'))\r
- elif choice == '3':\r
- data = {':action': 'password_reset'}\r
- data['email'] = ''\r
- while not data['email']:\r
- data['email'] = raw_input('Your email address: ')\r
- code, result = self.post_to_server(data)\r
- log.info('Server response (%s): %s' % (code, result))\r
-\r
- def build_post_data(self, action):\r
- # figure the data to send - the metadata plus some additional\r
- # information used by the package server\r
- meta = self.distribution.metadata\r
- data = {\r
- ':action': action,\r
- 'metadata_version' : '1.0',\r
- 'name': meta.get_name(),\r
- 'version': meta.get_version(),\r
- 'summary': meta.get_description(),\r
- 'home_page': meta.get_url(),\r
- 'author': meta.get_contact(),\r
- 'author_email': meta.get_contact_email(),\r
- 'license': meta.get_licence(),\r
- 'description': meta.get_long_description(),\r
- 'keywords': meta.get_keywords(),\r
- 'platform': meta.get_platforms(),\r
- 'classifiers': meta.get_classifiers(),\r
- 'download_url': meta.get_download_url(),\r
- # PEP 314\r
- 'provides': meta.get_provides(),\r
- 'requires': meta.get_requires(),\r
- 'obsoletes': meta.get_obsoletes(),\r
- }\r
- if data['provides'] or data['requires'] or data['obsoletes']:\r
- data['metadata_version'] = '1.1'\r
- return data\r
-\r
- def post_to_server(self, data, auth=None):\r
- ''' Post a query to the server, and return a string response.\r
- '''\r
- if 'name' in data:\r
- self.announce('Registering %s to %s' % (data['name'],\r
- self.repository),\r
- log.INFO)\r
- # Build up the MIME payload for the urllib2 POST data\r
- boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'\r
- sep_boundary = '\n--' + boundary\r
- end_boundary = sep_boundary + '--'\r
- body = StringIO.StringIO()\r
- for key, value in data.items():\r
- # handle multiple entries for the same name\r
- if type(value) not in (type([]), type( () )):\r
- value = [value]\r
- for value in value:\r
- body.write(sep_boundary)\r
- body.write('\nContent-Disposition: form-data; name="%s"'%key)\r
- body.write("\n\n")\r
- body.write(value)\r
- if value and value[-1] == '\r':\r
- body.write('\n') # write an extra newline (lurve Macs)\r
- body.write(end_boundary)\r
- body.write("\n")\r
- body = body.getvalue()\r
-\r
- # build the Request\r
- headers = {\r
- 'Content-type': 'multipart/form-data; boundary=%s; charset=utf-8'%boundary,\r
- 'Content-length': str(len(body))\r
- }\r
- req = urllib2.Request(self.repository, body, headers)\r
-\r
- # handle HTTP and include the Basic Auth handler\r
- opener = urllib2.build_opener(\r
- urllib2.HTTPBasicAuthHandler(password_mgr=auth)\r
- )\r
- data = ''\r
- try:\r
- result = opener.open(req)\r
- except urllib2.HTTPError, e:\r
- if self.show_response:\r
- data = e.fp.read()\r
- result = e.code, e.msg\r
- except urllib2.URLError, e:\r
- result = 500, str(e)\r
- else:\r
- if self.show_response:\r
- data = result.read()\r
- result = 200, 'OK'\r
- if self.show_response:\r
- dashes = '-' * 75\r
- self.announce('%s%s%s' % (dashes, data, dashes))\r
-\r
- return result\r