blob: 8805d41da0e62fa5658a34422600e26b8b73bfa3 [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
Georg Brandl392c6fc2008-05-25 07:25:25 +000017from ConfigParser import ConfigParser
Alexandre Vassalottieb8cef22008-05-16 02:06:59 +000018
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
53 def run(self):
54 if not self.distribution.dist_files:
55 raise DistutilsOptionError("No dist file created in earlier command")
Martin v. Löwis98da5622005-03-23 18:54:36 +000056 for command, pyversion, filename in self.distribution.dist_files:
57 self.upload_file(command, pyversion, filename)
Martin v. Löwis98858c92005-03-21 21:00:59 +000058
Martin v. Löwis98da5622005-03-23 18:54:36 +000059 def upload_file(self, command, pyversion, filename):
Martin v. Löwisf74b9232005-03-22 15:51:14 +000060 # Sign if requested
61 if self.sign:
Phillip J. Eby2e550b32006-03-30 02:12:14 +000062 gpg_args = ["gpg", "--detach-sign", "-a", filename]
63 if self.identity:
64 gpg_args[2:2] = ["--local-user", self.identity]
65 spawn(gpg_args,
Martin v. Löwisf74b9232005-03-22 15:51:14 +000066 dry_run=self.dry_run)
Martin v. Löwis98858c92005-03-21 21:00:59 +000067
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000068 # Fill in the data - send all the meta-data in case we need to
69 # register a new release
Phillip J. Eby5cb78462005-07-07 15:36:20 +000070 content = open(filename,'rb').read()
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000071 meta = self.distribution.metadata
Martin v. Löwis98858c92005-03-21 21:00:59 +000072 data = {
Martin v. Löwis6d0c85a2006-01-08 10:48:54 +000073 # action
74 ':action': 'file_upload',
75 'protcol_version': '1',
76
77 # identify release
78 'name': meta.get_name(),
79 'version': meta.get_version(),
80
81 # file content
82 'content': (os.path.basename(filename),content),
83 'filetype': command,
84 'pyversion': pyversion,
85 'md5_digest': md5(content).hexdigest(),
86
87 # additional meta-data
88 'metadata_version' : '1.0',
89 'summary': meta.get_description(),
90 'home_page': meta.get_url(),
91 'author': meta.get_contact(),
92 'author_email': meta.get_contact_email(),
93 'license': meta.get_licence(),
94 'description': meta.get_long_description(),
95 'keywords': meta.get_keywords(),
96 'platform': meta.get_platforms(),
97 'classifiers': meta.get_classifiers(),
98 'download_url': meta.get_download_url(),
99 # PEP 314
100 'provides': meta.get_provides(),
101 'requires': meta.get_requires(),
102 'obsoletes': meta.get_obsoletes(),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000103 }
104 comment = ''
105 if command == 'bdist_rpm':
106 dist, version, id = platform.dist()
107 if dist:
108 comment = 'built for %s %s' % (dist, version)
109 elif command == 'bdist_dumb':
110 comment = 'built for %s' % platform.platform(terse=1)
111 data['comment'] = comment
112
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000113 if self.sign:
114 data['gpg_signature'] = (os.path.basename(filename) + ".asc",
115 open(filename+".asc").read())
116
Martin v. Löwis98858c92005-03-21 21:00:59 +0000117 # set up the authentication
118 auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip()
119
120 # Build up the MIME payload for the POST data
121 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'
122 sep_boundary = '\n--' + boundary
123 end_boundary = sep_boundary + '--'
124 body = StringIO.StringIO()
125 for key, value in data.items():
126 # handle multiple entries for the same name
127 if type(value) != type([]):
128 value = [value]
129 for value in value:
130 if type(value) is tuple:
131 fn = ';filename="%s"' % value[0]
132 value = value[1]
133 else:
134 fn = ""
135 value = str(value)
136 body.write(sep_boundary)
137 body.write('\nContent-Disposition: form-data; name="%s"'%key)
138 body.write(fn)
139 body.write("\n\n")
140 body.write(value)
141 if value and value[-1] == '\r':
142 body.write('\n') # write an extra newline (lurve Macs)
143 body.write(end_boundary)
144 body.write("\n")
145 body = body.getvalue()
146
147 self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO)
148
149 # build the Request
150 # We can't use urllib2 since we need to send the Basic
151 # auth right with the first request
152 schema, netloc, url, params, query, fragments = \
153 urlparse.urlparse(self.repository)
154 assert not params and not query and not fragments
Tim Peterseba28be2005-03-28 01:08:02 +0000155 if schema == 'http':
Martin v. Löwis98858c92005-03-21 21:00:59 +0000156 http = httplib.HTTPConnection(netloc)
157 elif schema == 'https':
158 http = httplib.HTTPSConnection(netloc)
159 else:
160 raise AssertionError, "unsupported schema "+schema
161
162 data = ''
163 loglevel = log.INFO
164 try:
165 http.connect()
166 http.putrequest("POST", url)
Tim Peterseba28be2005-03-28 01:08:02 +0000167 http.putheader('Content-type',
Martin v. Löwis98858c92005-03-21 21:00:59 +0000168 'multipart/form-data; boundary=%s'%boundary)
169 http.putheader('Content-length', str(len(body)))
170 http.putheader('Authorization', auth)
171 http.endheaders()
172 http.send(body)
173 except socket.error, e:
Phillip J. Eby137ff792006-07-10 19:18:35 +0000174 self.announce(str(e), log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000175 return
176
177 r = http.getresponse()
178 if r.status == 200:
Tim Peterseba28be2005-03-28 01:08:02 +0000179 self.announce('Server response (%s): %s' % (r.status, r.reason),
Martin v. Löwis98858c92005-03-21 21:00:59 +0000180 log.INFO)
181 else:
Tim Peterseba28be2005-03-28 01:08:02 +0000182 self.announce('Upload failed (%s): %s' % (r.status, r.reason),
Martin v. Löwisf74b9232005-03-22 15:51:14 +0000183 log.ERROR)
Martin v. Löwis98858c92005-03-21 21:00:59 +0000184 if self.show_response:
185 print '-'*75, r.read(), '-'*75