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
|
|
|
from distutils.errors import *
|
2008-05-11 11:00:00 -03:00
|
|
|
from distutils.core import PyPIRCCommand
|
2005-03-22 11:51:14 -04:00
|
|
|
from distutils.spawn import spawn
|
2005-03-21 17:00:59 -04:00
|
|
|
from distutils import log
|
2006-04-30 05:57:35 -03:00
|
|
|
from hashlib import md5
|
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
|
|
|
|
import httplib
|
|
|
|
import base64
|
|
|
|
import urlparse
|
|
|
|
import cStringIO as StringIO
|
2008-05-15 23:06:59 -03:00
|
|
|
try:
|
|
|
|
from configparser import ConfigParser
|
|
|
|
except ImportError:
|
|
|
|
# For backward-compatibility with Python versions < 2.6.
|
|
|
|
from ConfigParser import ConfigParser
|
|
|
|
|
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
|
|
|
|
|
|
|
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):
|
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
|
2005-07-07 12:36:20 -03:00
|
|
|
content = open(filename,'rb').read()
|
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
|
|
|
|
auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip()
|
|
|
|
|
|
|
|
# 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
|
|
|
|
if type(value) != type([]):
|
|
|
|
value = [value]
|
|
|
|
for value in value:
|
|
|
|
if type(value) is tuple:
|
|
|
|
fn = ';filename="%s"' % value[0]
|
|
|
|
value = value[1]
|
|
|
|
else:
|
|
|
|
fn = ""
|
|
|
|
value = str(value)
|
|
|
|
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
|
|
|
|
# We can't use urllib2 since we need to send the Basic
|
|
|
|
# auth right with the first request
|
|
|
|
schema, netloc, url, params, query, fragments = \
|
|
|
|
urlparse.urlparse(self.repository)
|
|
|
|
assert not params and not query and not fragments
|
2005-03-27 21:08:02 -04:00
|
|
|
if schema == 'http':
|
2005-03-21 17:00:59 -04:00
|
|
|
http = httplib.HTTPConnection(netloc)
|
|
|
|
elif schema == 'https':
|
|
|
|
http = httplib.HTTPSConnection(netloc)
|
|
|
|
else:
|
|
|
|
raise AssertionError, "unsupported schema "+schema
|
|
|
|
|
|
|
|
data = ''
|
|
|
|
loglevel = log.INFO
|
|
|
|
try:
|
|
|
|
http.connect()
|
|
|
|
http.putrequest("POST", url)
|
2005-03-27 21:08:02 -04:00
|
|
|
http.putheader('Content-type',
|
2005-03-21 17:00:59 -04:00
|
|
|
'multipart/form-data; boundary=%s'%boundary)
|
|
|
|
http.putheader('Content-length', str(len(body)))
|
|
|
|
http.putheader('Authorization', auth)
|
|
|
|
http.endheaders()
|
|
|
|
http.send(body)
|
|
|
|
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
|
|
|
|
|
|
|
|
r = http.getresponse()
|
|
|
|
if r.status == 200:
|
2005-03-27 21:08:02 -04:00
|
|
|
self.announce('Server response (%s): %s' % (r.status, r.reason),
|
2005-03-21 17:00:59 -04:00
|
|
|
log.INFO)
|
|
|
|
else:
|
2005-03-27 21:08:02 -04:00
|
|
|
self.announce('Upload failed (%s): %s' % (r.status, r.reason),
|
2005-03-22 11:51:14 -04:00
|
|
|
log.ERROR)
|
2005-03-21 17:00:59 -04:00
|
|
|
if self.show_response:
|
|
|
|
print '-'*75, r.read(), '-'*75
|