blob: df82e4ca562c2048d8f4b52e73a8f31caa4b2229 [file] [log] [blame]
Martin v. Löwis55f1bb82005-03-21 20:56:35 +00001"""distutils.command.upload
2
3Implements the Distutils 'upload' subcommand (upload package to PyPI)."""
4
Martin v. Löwis98858c92005-03-21 21:00:59 +00005from distutils.errors import *
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +00006from distutils.core import PyPIRCCommand
Martin v. Löwisf74b9232005-03-22 15:51:14 +00007from distutils.spawn import spawn
Martin v. Löwis98858c92005-03-21 21:00:59 +00008from distutils import log
Tarek Ziadédda92f72009-02-27 12:53:34 +00009import sys
Martin v. Löwis98858c92005-03-21 21:00:59 +000010import os
Martin v. Löwisca5d8fe2005-03-24 19:40:57 +000011import socket
Martin v. Löwis98858c92005-03-21 21:00:59 +000012import platform
Martin v. Löwis98858c92005-03-21 21:00:59 +000013import httplib
14import base64
15import urlparse
16import cStringIO as StringIO
Georg Brandl392c6fc2008-05-25 07:25:25 +000017from ConfigParser import ConfigParser
Alexandre Vassalottieb8cef22008-05-16 02:06:59 +000018
Tarek Ziadédda92f72009-02-27 12:53:34 +000019# this keeps compatibility for 2.3 and 2.4
20if sys.version < "2.5":
21 from md5 import md5
22else:
23 from hashlib import md5
Martin v. Löwis98858c92005-03-21 21:00:59 +000024
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000025class upload(PyPIRCCommand):
Martin v. Löwis98858c92005-03-21 21:00:59 +000026
27 description = "upload binary package to PyPI"
28
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000029 user_options = PyPIRCCommand.user_options + [
Martin v. Löwisf74b9232005-03-22 15:51:14 +000030 ('sign', 's',
31 'sign files to upload using gpg'),
Phillip J. Eby2e550b32006-03-30 02:12:14 +000032 ('identity=', 'i', 'GPG identity used to sign files'),
Martin v. Löwis98858c92005-03-21 21:00:59 +000033 ]
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000034
35 boolean_options = PyPIRCCommand.boolean_options + ['sign']
Martin v. Löwis98858c92005-03-21 21:00:59 +000036
37 def initialize_options(self):
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000038 PyPIRCCommand.initialize_options(self)
Martin v. Löwis98858c92005-03-21 21:00:59 +000039 self.username = ''
40 self.password = ''
Martin v. Löwis98858c92005-03-21 21:00:59 +000041 self.show_response = 0
Martin v. Löwisf74b9232005-03-22 15:51:14 +000042 self.sign = False
Phillip J. Eby2e550b32006-03-30 02:12:14 +000043 self.identity = None
Martin v. Löwis98858c92005-03-21 21:00:59 +000044
45 def finalize_options(self):
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000046 PyPIRCCommand.finalize_options(self)
Phillip J. Eby2e550b32006-03-30 02:12:14 +000047 if self.identity and not self.sign:
48 raise DistutilsOptionError(
49 "Must use --sign for --identity to have meaning"
50 )
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000051 config = self._read_pypirc()
52 if config != {}:
53 self.username = config['username']
54 self.password = config['password']
55 self.repository = config['repository']
56 self.realm = config['realm']
Martin v. Löwis98858c92005-03-21 21:00:59 +000057
Tarek Ziadé1a240fb2009-01-08 23:56:31 +000058 # getting the password from the distribution
59 # if previously set by the register command
60 if not self.password and self.distribution.password:
61 self.password = self.distribution.password
62
Martin v. Löwis98858c92005-03-21 21:00:59 +000063 def run(self):
64 if not self.distribution.dist_files:
65 raise DistutilsOptionError("No dist file created in earlier command")
Martin v. Löwis98da5622005-03-23 18:54:36 +000066 for command, pyversion, filename in self.distribution.dist_files:
67 self.upload_file(command, pyversion, filename)
Martin v. Löwis98858c92005-03-21 21:00:59 +000068
Martin v. Löwis98da5622005-03-23 18:54:36 +000069 def upload_file(self, command, pyversion, filename):
Martin v. Löwisf74b9232005-03-22 15:51:14 +000070 # Sign if requested
71 if self.sign:
Phillip J. Eby2e550b32006-03-30 02:12:14 +000072 gpg_args = ["gpg", "--detach-sign", "-a", filename]
73 if self.identity:
74 gpg_args[2:2] = ["--local-user", self.identity]
75 spawn(gpg_args,
Martin v. Löwisf74b9232005-03-22 15:51:14 +000076 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000077
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000078 # Fill in the data - send all the meta-data in case we need to
79 # register a new release
Phillip J. Eby5cb78462005-07-07 15:36:20 +000080 content = open(filename,'rb').read()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000081 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000082 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000083 # action
84 ':action': 'file_upload',
85 'protcol_version': '1',
86
87 # identify release
88 'name': meta.get_name(),
89 'version': meta.get_version(),
90
91 # file content
92 'content': (os.path.basename(filename),content),
93 'filetype': command,
94 'pyversion': pyversion,
95 'md5_digest': md5(content).hexdigest(),
96
97 # additional meta-data
98 'metadata_version' : '1.0',
99 'summary': meta.get_description(),
100 'home_page': meta.get_url(),
101 'author': meta.get_contact(),
102 'author_email': meta.get_contact_email(),
103 'license': meta.get_licence(),
104 'description': meta.get_long_description(),
105 'keywords': meta.get_keywords(),
106 'platform': meta.get_platforms(),
107 'classifiers': meta.get_classifiers(),
108 'download_url': meta.get_download_url(),
109 # PEP 314
110 'provides': meta.get_provides(),
111 'requires': meta.get_requires(),
112 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000113 }
114 comment = ''
115 if command == 'bdist_rpm':
116 dist, version, id = platform.dist()
117 if dist:
118 comment = 'built for %s %s' % (dist, version)
119 elif command == 'bdist_dumb':
120 comment = 'built for %s' % platform.platform(terse=1)
121 data['comment'] = comment
122
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000123 if self.sign:
124 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
125 open(filename+".asc").read())
126
Martin v. Löwis98858c92005-03-21 21:00:59 +0000127 # set up the authentication
128 auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip()
129
130 # Build up the MIME payload for the POST data
131 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
132 sep_boundary = '\n--' + boundary
133 end_boundary = sep_boundary + '--'
134 body = StringIO.StringIO()
135 for key, value in data.items():
136 # handle multiple entries for the same name
137 if type(value) != type([]):
138 value = [value]
139 for value in value:
140 if type(value) is tuple:
141 fn = ';filename="%s"' % value[0]
142 value = value[1]
143 else:
144 fn = ""
145 value = str(value)
146 body.write(sep_boundary)
147 body.write('\nContent-Disposition: form-data; name="%s"'%key)
148 body.write(fn)
149 body.write("\n\n")
150 body.write(value)
151 if value and value[-1] == '\r':
152 body.write('\n') # write an extra newline (lurve Macs)
153 body.write(end_boundary)
154 body.write("\n")
155 body = body.getvalue()
156
157 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
158
159 # build the Request
160 # We can't use urllib2 since we need to send the Basic
161 # auth right with the first request
162 schema, netloc, url, params, query, fragments = \
163 urlparse.urlparse(self.repository)
164 assert not params and not query and not fragments
Tim Peterseba28be2005-03-28 01:08:02 +0000165 if schema == 'http':
Martin v. Löwis98858c92005-03-21 21:00:59 +0000166 http = httplib.HTTPConnection(netloc)
167 elif schema == 'https':
168 http = httplib.HTTPSConnection(netloc)
169 else:
170 raise AssertionError, "unsupported schema "+schema
171
172 data = ''
173 loglevel = log.INFO
174 try:
175 http.connect()
176 http.putrequest("POST", url)
Tim Peterseba28be2005-03-28 01:08:02 +0000177 http.putheader('Content-type',
Martin v. Löwis98858c92005-03-21 21:00:59 +0000178 'multipart/form-data; boundary=%s'%boundary)
179 http.putheader('Content-length', str(len(body)))
180 http.putheader('Authorization', auth)
181 http.endheaders()
182 http.send(body)
183 except socket.error, e:
Phillip J. Eby137ff792006-07-10 19:18:35 +0000184 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000185 return
186
187 r = http.getresponse()
188 if r.status == 200:
Tim Peterseba28be2005-03-28 01:08:02 +0000189 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000190 log.INFO)
191 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000192 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000193 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000194 if self.show_response:
195 print '-'*75, r.read(), '-'*75