blob: 92c4bf204e1365afcfb41afb6c2a801c0e149e25 [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
Georg Brandlbffb0bc2006-04-30 08:57:35 +00009from hashlib import md5
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
Alexandre Vassalottieb8cef22008-05-16 02:06:59 +000017try:
18 from configparser import ConfigParser
19except ImportError:
20 # For backward-compatibility with Python versions < 2.6.
21 from ConfigParser import ConfigParser
22
Martin v. Löwis98858c92005-03-21 21:00:59 +000023
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000024class upload(PyPIRCCommand):
Martin v. Löwis98858c92005-03-21 21:00:59 +000025
26 description = "upload binary package to PyPI"
27
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000028 user_options = PyPIRCCommand.user_options + [
Martin v. Löwisf74b9232005-03-22 15:51:14 +000029 ('sign', 's',
30 'sign files to upload using gpg'),
Phillip J. Eby2e550b32006-03-30 02:12:14 +000031 ('identity=', 'i', 'GPG identity used to sign files'),
Martin v. Löwis98858c92005-03-21 21:00:59 +000032 ]
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000033
34 boolean_options = PyPIRCCommand.boolean_options + ['sign']
Martin v. Löwis98858c92005-03-21 21:00:59 +000035
36 def initialize_options(self):
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000037 PyPIRCCommand.initialize_options(self)
Martin v. Löwis98858c92005-03-21 21:00:59 +000038 self.username = ''
39 self.password = ''
Martin v. Löwis98858c92005-03-21 21:00:59 +000040 self.show_response = 0
Martin v. Löwisf74b9232005-03-22 15:51:14 +000041 self.sign = False
Phillip J. Eby2e550b32006-03-30 02:12:14 +000042 self.identity = None
Martin v. Löwis98858c92005-03-21 21:00:59 +000043
44 def finalize_options(self):
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000045 PyPIRCCommand.finalize_options(self)
Phillip J. Eby2e550b32006-03-30 02:12:14 +000046 if self.identity and not self.sign:
47 raise DistutilsOptionError(
48 "Must use --sign for --identity to have meaning"
49 )
Andrew M. Kuchlingaac5c862008-05-11 14:00:00 +000050 config = self._read_pypirc()
51 if config != {}:
52 self.username = config['username']
53 self.password = config['password']
54 self.repository = config['repository']
55 self.realm = config['realm']
Martin v. Löwis98858c92005-03-21 21:00:59 +000056
57 def run(self):
58 if not self.distribution.dist_files:
59 raise DistutilsOptionError("No dist file created in earlier command")
Martin v. Löwis98da5622005-03-23 18:54:36 +000060 for command, pyversion, filename in self.distribution.dist_files:
61 self.upload_file(command, pyversion, filename)
Martin v. Löwis98858c92005-03-21 21:00:59 +000062
Martin v. Löwis98da5622005-03-23 18:54:36 +000063 def upload_file(self, command, pyversion, filename):
Martin v. Löwisf74b9232005-03-22 15:51:14 +000064 # Sign if requested
65 if self.sign:
Phillip J. Eby2e550b32006-03-30 02:12:14 +000066 gpg_args = ["gpg", "--detach-sign", "-a", filename]
67 if self.identity:
68 gpg_args[2:2] = ["--local-user", self.identity]
69 spawn(gpg_args,
Martin v. Löwisf74b9232005-03-22 15:51:14 +000070 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000071
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000072 # Fill in the data - send all the meta-data in case we need to
73 # register a new release
Phillip J. Eby5cb78462005-07-07 15:36:20 +000074 content = open(filename,'rb').read()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000075 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000076 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000077 # action
78 ':action': 'file_upload',
79 'protcol_version': '1',
80
81 # identify release
82 'name': meta.get_name(),
83 'version': meta.get_version(),
84
85 # file content
86 'content': (os.path.basename(filename),content),
87 'filetype': command,
88 'pyversion': pyversion,
89 'md5_digest': md5(content).hexdigest(),
90
91 # additional meta-data
92 'metadata_version' : '1.0',
93 'summary': meta.get_description(),
94 'home_page': meta.get_url(),
95 'author': meta.get_contact(),
96 'author_email': meta.get_contact_email(),
97 'license': meta.get_licence(),
98 'description': meta.get_long_description(),
99 'keywords': meta.get_keywords(),
100 'platform': meta.get_platforms(),
101 'classifiers': meta.get_classifiers(),
102 'download_url': meta.get_download_url(),
103 # PEP 314
104 'provides': meta.get_provides(),
105 'requires': meta.get_requires(),
106 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000107 }
108 comment = ''
109 if command == 'bdist_rpm':
110 dist, version, id = platform.dist()
111 if dist:
112 comment = 'built for %s %s' % (dist, version)
113 elif command == 'bdist_dumb':
114 comment = 'built for %s' % platform.platform(terse=1)
115 data['comment'] = comment
116
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000117 if self.sign:
118 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
119 open(filename+".asc").read())
120
Martin v. Löwis98858c92005-03-21 21:00:59 +0000121 # set up the authentication
122 auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip()
123
124 # Build up the MIME payload for the POST data
125 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
126 sep_boundary = '\n--' + boundary
127 end_boundary = sep_boundary + '--'
128 body = StringIO.StringIO()
129 for key, value in data.items():
130 # handle multiple entries for the same name
131 if type(value) != type([]):
132 value = [value]
133 for value in value:
134 if type(value) is tuple:
135 fn = ';filename="%s"' % value[0]
136 value = value[1]
137 else:
138 fn = ""
139 value = str(value)
140 body.write(sep_boundary)
141 body.write('\nContent-Disposition: form-data; name="%s"'%key)
142 body.write(fn)
143 body.write("\n\n")
144 body.write(value)
145 if value and value[-1] == '\r':
146 body.write('\n') # write an extra newline (lurve Macs)
147 body.write(end_boundary)
148 body.write("\n")
149 body = body.getvalue()
150
151 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
152
153 # build the Request
154 # We can't use urllib2 since we need to send the Basic
155 # auth right with the first request
156 schema, netloc, url, params, query, fragments = \
157 urlparse.urlparse(self.repository)
158 assert not params and not query and not fragments
Tim Peterseba28be2005-03-28 01:08:02 +0000159 if schema == 'http':
Martin v. Löwis98858c92005-03-21 21:00:59 +0000160 http = httplib.HTTPConnection(netloc)
161 elif schema == 'https':
162 http = httplib.HTTPSConnection(netloc)
163 else:
164 raise AssertionError, "unsupported schema "+schema
165
166 data = ''
167 loglevel = log.INFO
168 try:
169 http.connect()
170 http.putrequest("POST", url)
Tim Peterseba28be2005-03-28 01:08:02 +0000171 http.putheader('Content-type',
Martin v. Löwis98858c92005-03-21 21:00:59 +0000172 'multipart/form-data; boundary=%s'%boundary)
173 http.putheader('Content-length', str(len(body)))
174 http.putheader('Authorization', auth)
175 http.endheaders()
176 http.send(body)
177 except socket.error, e:
Phillip J. Eby137ff792006-07-10 19:18:35 +0000178 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000179 return
180
181 r = http.getresponse()
182 if r.status == 200:
Tim Peterseba28be2005-03-28 01:08:02 +0000183 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000184 log.INFO)
185 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000186 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000187 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000188 if self.show_response:
189 print '-'*75, r.read(), '-'*75