2005-03-21 16:56:35 -04:00
|
|
|
"""distutils.command.upload
|
|
|
|
|
|
|
|
Implements the Distutils 'upload' subcommand (upload package to PyPI)."""
|
2005-03-21 17:00:59 -04:00
|
|
|
import os
|
2005-03-24 15:40:57 -04:00
|
|
|
import socket
|
2005-03-21 17:00:59 -04:00
|
|
|
import platform
|
2009-06-15 20:30:13 -03:00
|
|
|
from urllib2 import urlopen, Request, HTTPError
|
2009-12-20 19:23:34 -04:00
|
|
|
from base64 import standard_b64encode
|
2005-03-21 17:00:59 -04:00
|
|
|
import urlparse
|
|
|
|
import cStringIO as StringIO
|
2009-06-15 20:04:29 -03:00
|
|
|
from hashlib import md5
|
2008-05-15 23:06:59 -03:00
|
|
|
|
2009-12-20 21:22:46 -04:00
|
|
|
from distutils.errors import DistutilsOptionError
|
2009-06-15 20:04:29 -03:00
|
|
|
from distutils.core import PyPIRCCommand
|
|
|
|
from distutils.spawn import spawn
|
|
|
|
from distutils import log
|
2005-03-21 17:00:59 -04:00
|
|
|
|
2008-05-11 11:00:00 -03:00
|
|
|
class upload(PyPIRCCommand):
|
2005-03-21 17:00:59 -04:00
|
|
|
|
|
|
|
description = "upload binary package to PyPI"
|
|
|
|
|
2008-05-11 11:00:00 -03:00
|
|
|
user_options = PyPIRCCommand.user_options + [
|
2005-03-22 11:51:14 -04:00
|
|
|
('sign', 's',
|
|
|
|
'sign files to upload using gpg'),
|
2006-03-29 22:12:14 -04:00
|
|
|
('identity=', 'i', 'GPG identity used to sign files'),
|
2005-03-21 17:00:59 -04:00
|
|
|
]
|
2008-05-11 11:00:00 -03:00
|
|
|
|
|
|
|
boolean_options = PyPIRCCommand.boolean_options + ['sign']
|
2005-03-21 17:00:59 -04:00
|
|
|
|
|
|
|
def initialize_options(self):
|
2008-05-11 11:00:00 -03:00
|
|
|
PyPIRCCommand.initialize_options(self)
|
2005-03-21 17:00:59 -04:00
|
|
|
self.username = ''
|
|
|
|
self.password = ''
|
|
|
|
self.show_response = 0
|
2005-03-22 11:51:14 -04:00
|
|
|
self.sign = False
|
2006-03-29 22:12:14 -04:00
|
|
|
self.identity = None
|
2005-03-21 17:00:59 -04:00
|
|
|
|
|
|
|
def finalize_options(self):
|
2008-05-11 11:00:00 -03:00
|
|
|
PyPIRCCommand.finalize_options(self)
|
2006-03-29 22:12:14 -04:00
|
|
|
if self.identity and not self.sign:
|
|
|
|
raise DistutilsOptionError(
|
|
|
|
"Must use --sign for --identity to have meaning"
|
|
|
|
)
|
2008-05-11 11:00:00 -03:00
|
|
|
config = self._read_pypirc()
|
|
|
|
if config != {}:
|
|
|
|
self.username = config['username']
|
|
|
|
self.password = config['password']
|
|
|
|
self.repository = config['repository']
|
|
|
|
self.realm = config['realm']
|
2005-03-21 17:00:59 -04:00
|
|
|
|
2009-01-08 19:56:31 -04:00
|
|
|
# getting the password from the distribution
|
|
|
|
# if previously set by the register command
|
|
|
|
if not self.password and self.distribution.password:
|
|
|
|
self.password = self.distribution.password
|
|
|
|
|
2005-03-21 17:00:59 -04:00
|
|
|
def run(self):
|
|
|
|
if not self.distribution.dist_files:
|
|
|
|
raise DistutilsOptionError("No dist file created in earlier command")
|
2005-03-23 14:54:36 -04:00
|
|
|
for command, pyversion, filename in self.distribution.dist_files:
|
|
|
|
self.upload_file(command, pyversion, filename)
|
2005-03-21 17:00:59 -04:00
|
|
|
|
2005-03-23 14:54:36 -04:00
|
|
|
def upload_file(self, command, pyversion, filename):
|
2009-06-15 20:30:13 -03:00
|
|
|
# Makes sure the repository URL is compliant
|
|
|
|
schema, netloc, url, params, query, fragments = \
|
|
|
|
urlparse.urlparse(self.repository)
|
|
|
|
if params or query or fragments:
|
|
|
|
raise AssertionError("Incompatible url %s" % self.repository)
|
|
|
|
|
|
|
|
if schema not in ('http', 'https'):
|
|
|
|
raise AssertionError("unsupported schema " + schema)
|
|
|
|
|
2005-03-22 11:51:14 -04:00
|
|
|
# Sign if requested
|
|
|
|
if self.sign:
|
2006-03-29 22:12:14 -04:00
|
|
|
gpg_args = ["gpg", "--detach-sign", "-a", filename]
|
|
|
|
if self.identity:
|
|
|
|
gpg_args[2:2] = ["--local-user", self.identity]
|
|
|
|
spawn(gpg_args,
|
2005-03-22 11:51:14 -04:00
|
|
|
dry_run=self.dry_run)
|
2005-03-21 17:00:59 -04:00
|
|
|
|
2006-01-08 06:48:54 -04:00
|
|
|
# Fill in the data - send all the meta-data in case we need to
|
|
|
|
# register a new release
|
2010-11-06 01:06:18 -03:00
|
|
|
f = open(filename,'rb')
|
|
|
|
try:
|
|
|
|
content = f.read()
|
|
|
|
finally:
|
|
|
|
f.close()
|
2006-01-08 06:48:54 -04:00
|
|
|
meta = self.distribution.metadata
|
2005-03-21 17:00:59 -04:00
|
|
|
data = {
|
2006-01-08 06:48:54 -04:00
|
|
|
# action
|
|
|
|
':action': 'file_upload',
|
|
|
|
'protcol_version': '1',
|
|
|
|
|
|
|
|
# identify release
|
|
|
|
'name': meta.get_name(),
|
|
|
|
'version': meta.get_version(),
|
|
|
|
|
|
|
|
# file content
|
|
|
|
'content': (os.path.basename(filename),content),
|
|
|
|
'filetype': command,
|
|
|
|
'pyversion': pyversion,
|
|
|
|
'md5_digest': md5(content).hexdigest(),
|
|
|
|
|
|
|
|
# additional meta-data
|
|
|
|
'metadata_version' : '1.0',
|
|
|
|
'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(),
|
|
|
|
'classifiers': meta.get_classifiers(),
|
|
|
|
'download_url': meta.get_download_url(),
|
|
|
|
# PEP 314
|
|
|
|
'provides': meta.get_provides(),
|
|
|
|
'requires': meta.get_requires(),
|
|
|
|
'obsoletes': meta.get_obsoletes(),
|
2005-03-21 17:00:59 -04:00
|
|
|
}
|
|
|
|
comment = ''
|
|
|
|
if command == 'bdist_rpm':
|
|
|
|
dist, version, id = platform.dist()
|
|
|
|
if dist:
|
|
|
|
comment = 'built for %s %s' % (dist, version)
|
|
|
|
elif command == 'bdist_dumb':
|
|
|
|
comment = 'built for %s' % platform.platform(terse=1)
|
|
|
|
data['comment'] = comment
|
|
|
|
|
2005-03-22 11:51:14 -04:00
|
|
|
if self.sign:
|
|
|
|
data['gpg_signature'] = (os.path.basename(filename) + ".asc",
|
|
|
|
open(filename+".asc").read())
|
|
|
|
|
2005-03-21 17:00:59 -04:00
|
|
|
# set up the authentication
|
2009-12-20 19:23:34 -04:00
|
|
|
auth = "Basic " + standard_b64encode(self.username + ":" +
|
|
|
|
self.password)
|
2005-03-21 17:00:59 -04:00
|
|
|
|
|
|
|
# Build up the MIME payload for the 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
|
2009-06-15 20:04:29 -03:00
|
|
|
if not isinstance(value, list):
|
2005-03-21 17:00:59 -04:00
|
|
|
value = [value]
|
|
|
|
for value in value:
|
2009-06-15 20:04:29 -03:00
|
|
|
if isinstance(value, tuple):
|
2005-03-21 17:00:59 -04:00
|
|
|
fn = ';filename="%s"' % value[0]
|
|
|
|
value = value[1]
|
|
|
|
else:
|
|
|
|
fn = ""
|
2010-01-23 20:33:32 -04:00
|
|
|
|
2005-03-21 17:00:59 -04:00
|
|
|
body.write(sep_boundary)
|
|
|
|
body.write('\nContent-Disposition: form-data; name="%s"'%key)
|
|
|
|
body.write(fn)
|
|
|
|
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()
|
|
|
|
|
|
|
|
self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
|
|
|
|
|
|
|
|
# build the Request
|
2009-06-15 20:30:13 -03:00
|
|
|
headers = {'Content-type':
|
|
|
|
'multipart/form-data; boundary=%s' % boundary,
|
|
|
|
'Content-length': str(len(body)),
|
|
|
|
'Authorization': auth}
|
|
|
|
|
|
|
|
request = Request(self.repository, data=body,
|
|
|
|
headers=headers)
|
|
|
|
# send the data
|
2005-03-21 17:00:59 -04:00
|
|
|
try:
|
2009-06-15 20:30:13 -03:00
|
|
|
result = urlopen(request)
|
|
|
|
status = result.getcode()
|
|
|
|
reason = result.msg
|
2010-12-03 13:00:40 -04:00
|
|
|
if self.show_response:
|
|
|
|
msg = '\n'.join(('-' * 75, r.read(), '-' * 75))
|
|
|
|
self.announce(msg, log.INFO)
|
2005-03-21 17:00:59 -04:00
|
|
|
except socket.error, e:
|
2006-07-10 16:18:35 -03:00
|
|
|
self.announce(str(e), log.ERROR)
|
2005-03-21 17:00:59 -04:00
|
|
|
return
|
2009-06-15 20:30:13 -03:00
|
|
|
except HTTPError, e:
|
|
|
|
status = e.code
|
|
|
|
reason = e.msg
|
2005-03-21 17:00:59 -04:00
|
|
|
|
2009-06-15 20:30:13 -03:00
|
|
|
if status == 200:
|
|
|
|
self.announce('Server response (%s): %s' % (status, reason),
|
2005-03-21 17:00:59 -04:00
|
|
|
log.INFO)
|
|
|
|
else:
|
2009-06-15 20:30:13 -03:00
|
|
|
self.announce('Upload failed (%s): %s' % (status, reason),
|
2005-03-22 11:51:14 -04:00
|
|
|
log.ERROR)
|