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