blob: d3066816b4f55aba68438fbbe14309ec9ffb304c [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)."""
Tarek Ziadédda92f72009-02-27 12:53:34 +00004import sys
Martin v. Löwis98858c92005-03-21 21:00:59 +00005import os
Martin v. Löwisca5d8fe2005-03-24 19:40:57 +00006import socket
Martin v. Löwis98858c92005-03-21 21:00:59 +00007import platform
Martin v. Löwis98858c92005-03-21 21:00:59 +00008import httplib
9import base64
10import urlparse
11import cStringIO as StringIO
Georg Brandl392c6fc2008-05-25 07:25:25 +000012from ConfigParser import ConfigParser
Tarek Ziadéd7c5cee2009-06-15 23:04:29 +000013from hashlib import md5
Alexandre Vassalottieb8cef22008-05-16 02:06:59 +000014
Tarek Ziadéd7c5cee2009-06-15 23:04:29 +000015from distutils.errors import *
16from distutils.core import PyPIRCCommand
17from distutils.spawn import spawn
18from distutils import log
Martin v. Löwis98858c92005-03-21 21:00:59 +000019
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000020class upload(PyPIRCCommand):
Martin v. Löwis98858c92005-03-21 21:00:59 +000021
22 description = "upload binary package to PyPI"
23
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000024 user_options = PyPIRCCommand.user_options + [
Martin v. Löwisf74b9232005-03-22 15:51:14 +000025 ('sign', 's',
26 'sign files to upload using gpg'),
Phillip J. Eby2e550b32006-03-30 02:12:14 +000027 ('identity=', 'i', 'GPG identity used to sign files'),
Martin v. Löwis98858c92005-03-21 21:00:59 +000028 ]
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000029
30 boolean_options = PyPIRCCommand.boolean_options + ['sign']
Martin v. Löwis98858c92005-03-21 21:00:59 +000031
32 def initialize_options(self):
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000033 PyPIRCCommand.initialize_options(self)
Martin v. Löwis98858c92005-03-21 21:00:59 +000034 self.username = ''
35 self.password = ''
Martin v. Löwis98858c92005-03-21 21:00:59 +000036 self.show_response = 0
Martin v. Löwisf74b9232005-03-22 15:51:14 +000037 self.sign = False
Phillip J. Eby2e550b32006-03-30 02:12:14 +000038 self.identity = None
Martin v. Löwis98858c92005-03-21 21:00:59 +000039
40 def finalize_options(self):
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000041 PyPIRCCommand.finalize_options(self)
Phillip J. Eby2e550b32006-03-30 02:12:14 +000042 if self.identity and not self.sign:
43 raise DistutilsOptionError(
44 "Must use --sign for --identity to have meaning"
45 )
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000046 config = self._read_pypirc()
47 if config != {}:
48 self.username = config['username']
49 self.password = config['password']
50 self.repository = config['repository']
51 self.realm = config['realm']
Martin v. Löwis98858c92005-03-21 21:00:59 +000052
Tarek Ziadé1a240fb2009-01-08 23:56:31 +000053 # getting the password from the distribution
54 # if previously set by the register command
55 if not self.password and self.distribution.password:
56 self.password = self.distribution.password
57
Martin v. Löwis98858c92005-03-21 21:00:59 +000058 def run(self):
59 if not self.distribution.dist_files:
60 raise DistutilsOptionError("No dist file created in earlier command")
Martin v. Löwis98da5622005-03-23 18:54:36 +000061 for command, pyversion, filename in self.distribution.dist_files:
62 self.upload_file(command, pyversion, filename)
Martin v. Löwis98858c92005-03-21 21:00:59 +000063
Martin v. Löwis98da5622005-03-23 18:54:36 +000064 def upload_file(self, command, pyversion, filename):
Martin v. Löwisf74b9232005-03-22 15:51:14 +000065 # Sign if requested
66 if self.sign:
Phillip J. Eby2e550b32006-03-30 02:12:14 +000067 gpg_args = ["gpg", "--detach-sign", "-a", filename]
68 if self.identity:
69 gpg_args[2:2] = ["--local-user", self.identity]
70 spawn(gpg_args,
Martin v. Löwisf74b9232005-03-22 15:51:14 +000071 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000072
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000073 # Fill in the data - send all the meta-data in case we need to
74 # register a new release
Phillip J. Eby5cb78462005-07-07 15:36:20 +000075 content = open(filename,'rb').read()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000076 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000077 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000078 # action
79 ':action': 'file_upload',
80 'protcol_version': '1',
81
82 # identify release
83 'name': meta.get_name(),
84 'version': meta.get_version(),
85
86 # file content
87 'content': (os.path.basename(filename),content),
88 'filetype': command,
89 'pyversion': pyversion,
90 'md5_digest': md5(content).hexdigest(),
91
92 # additional meta-data
93 'metadata_version' : '1.0',
94 'summary': meta.get_description(),
95 'home_page': meta.get_url(),
96 'author': meta.get_contact(),
97 'author_email': meta.get_contact_email(),
98 'license': meta.get_licence(),
99 'description': meta.get_long_description(),
100 'keywords': meta.get_keywords(),
101 'platform': meta.get_platforms(),
102 'classifiers': meta.get_classifiers(),
103 'download_url': meta.get_download_url(),
104 # PEP 314
105 'provides': meta.get_provides(),
106 'requires': meta.get_requires(),
107 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000108 }
109 comment = ''
110 if command == 'bdist_rpm':
111 dist, version, id = platform.dist()
112 if dist:
113 comment = 'built for %s %s' % (dist, version)
114 elif command == 'bdist_dumb':
115 comment = 'built for %s' % platform.platform(terse=1)
116 data['comment'] = comment
117
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000118 if self.sign:
119 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
120 open(filename+".asc").read())
121
Martin v. Löwis98858c92005-03-21 21:00:59 +0000122 # set up the authentication
Tarek Ziadéd7c5cee2009-06-15 23:04:29 +0000123 auth = "Basic " + base64.encodestring(self.username + ":" +
124 self.password).strip()
Martin v. Löwis98858c92005-03-21 21:00:59 +0000125
126 # Build up the MIME payload for the POST data
127 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
128 sep_boundary = '\n--' + boundary
129 end_boundary = sep_boundary + '--'
130 body = StringIO.StringIO()
131 for key, value in data.items():
132 # handle multiple entries for the same name
Tarek Ziadéd7c5cee2009-06-15 23:04:29 +0000133 if not isinstance(value, list):
Martin v. Löwis98858c92005-03-21 21:00:59 +0000134 value = [value]
135 for value in value:
Tarek Ziadéd7c5cee2009-06-15 23:04:29 +0000136 if isinstance(value, tuple):
Martin v. Löwis98858c92005-03-21 21:00:59 +0000137 fn = ';filename="%s"' % value[0]
138 value = value[1]
139 else:
140 fn = ""
141 value = str(value)
142 body.write(sep_boundary)
143 body.write('\nContent-Disposition: form-data; name="%s"'%key)
144 body.write(fn)
145 body.write("\n\n")
146 body.write(value)
147 if value and value[-1] == '\r':
148 body.write('\n') # write an extra newline (lurve Macs)
149 body.write(end_boundary)
150 body.write("\n")
151 body = body.getvalue()
152
153 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
154
155 # build the Request
156 # We can't use urllib2 since we need to send the Basic
157 # auth right with the first request
158 schema, netloc, url, params, query, fragments = \
159 urlparse.urlparse(self.repository)
160 assert not params and not query and not fragments
Tim Peterseba28be2005-03-28 01:08:02 +0000161 if schema == 'http':
Martin v. Löwis98858c92005-03-21 21:00:59 +0000162 http = httplib.HTTPConnection(netloc)
163 elif schema == 'https':
164 http = httplib.HTTPSConnection(netloc)
165 else:
166 raise AssertionError, "unsupported schema "+schema
167
168 data = ''
169 loglevel = log.INFO
170 try:
171 http.connect()
172 http.putrequest("POST", url)
Tim Peterseba28be2005-03-28 01:08:02 +0000173 http.putheader('Content-type',
Martin v. Löwis98858c92005-03-21 21:00:59 +0000174 'multipart/form-data; boundary=%s'%boundary)
175 http.putheader('Content-length', str(len(body)))
176 http.putheader('Authorization', auth)
177 http.endheaders()
178 http.send(body)
179 except socket.error, e:
Phillip J. Eby137ff792006-07-10 19:18:35 +0000180 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000181 return
182
183 r = http.getresponse()
184 if r.status == 200:
Tim Peterseba28be2005-03-28 01:08:02 +0000185 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000186 log.INFO)
187 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000188 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000189 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000190 if self.show_response:
Tarek Ziadée899b712009-03-31 20:53:55 +0000191 self.announce('-'*75, r.read(), '-'*75)