Martin v. Löwis | 55f1bb8 | 2005-03-21 20:56:35 +0000 | [diff] [blame] | 1 | """distutils.command.upload |
| 2 | |
| 3 | Implements the Distutils 'upload' subcommand (upload package to PyPI).""" |
| 4 | |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 5 | from distutils.errors import * |
Andrew M. Kuchling | aac5c86 | 2008-05-11 14:00:00 +0000 | [diff] [blame^] | 6 | from distutils.core import PyPIRCCommand |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 7 | from distutils.spawn import spawn |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 8 | from distutils import log |
Georg Brandl | bffb0bc | 2006-04-30 08:57:35 +0000 | [diff] [blame] | 9 | from hashlib import md5 |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 10 | import os |
Martin v. Löwis | ca5d8fe | 2005-03-24 19:40:57 +0000 | [diff] [blame] | 11 | import socket |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 12 | import platform |
| 13 | import ConfigParser |
| 14 | import httplib |
| 15 | import base64 |
| 16 | import urlparse |
| 17 | import cStringIO as StringIO |
| 18 | |
Andrew M. Kuchling | aac5c86 | 2008-05-11 14:00:00 +0000 | [diff] [blame^] | 19 | class upload(PyPIRCCommand): |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 20 | |
| 21 | description = "upload binary package to PyPI" |
| 22 | |
Andrew M. Kuchling | aac5c86 | 2008-05-11 14:00:00 +0000 | [diff] [blame^] | 23 | user_options = PyPIRCCommand.user_options + [ |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 24 | ('sign', 's', |
| 25 | 'sign files to upload using gpg'), |
Phillip J. Eby | 2e550b3 | 2006-03-30 02:12:14 +0000 | [diff] [blame] | 26 | ('identity=', 'i', 'GPG identity used to sign files'), |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 27 | ] |
Andrew M. Kuchling | aac5c86 | 2008-05-11 14:00:00 +0000 | [diff] [blame^] | 28 | |
| 29 | boolean_options = PyPIRCCommand.boolean_options + ['sign'] |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 30 | |
| 31 | def initialize_options(self): |
Andrew M. Kuchling | aac5c86 | 2008-05-11 14:00:00 +0000 | [diff] [blame^] | 32 | PyPIRCCommand.initialize_options(self) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 33 | self.username = '' |
| 34 | self.password = '' |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 35 | self.show_response = 0 |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 36 | self.sign = False |
Phillip J. Eby | 2e550b3 | 2006-03-30 02:12:14 +0000 | [diff] [blame] | 37 | self.identity = None |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 38 | |
| 39 | def finalize_options(self): |
Andrew M. Kuchling | aac5c86 | 2008-05-11 14:00:00 +0000 | [diff] [blame^] | 40 | PyPIRCCommand.finalize_options(self) |
Phillip J. Eby | 2e550b3 | 2006-03-30 02:12:14 +0000 | [diff] [blame] | 41 | if self.identity and not self.sign: |
| 42 | raise DistutilsOptionError( |
| 43 | "Must use --sign for --identity to have meaning" |
| 44 | ) |
Andrew M. Kuchling | aac5c86 | 2008-05-11 14:00:00 +0000 | [diff] [blame^] | 45 | config = self._read_pypirc() |
| 46 | if config != {}: |
| 47 | self.username = config['username'] |
| 48 | self.password = config['password'] |
| 49 | self.repository = config['repository'] |
| 50 | self.realm = config['realm'] |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 51 | |
| 52 | def run(self): |
| 53 | if not self.distribution.dist_files: |
| 54 | raise DistutilsOptionError("No dist file created in earlier command") |
Martin v. Löwis | 98da562 | 2005-03-23 18:54:36 +0000 | [diff] [blame] | 55 | for command, pyversion, filename in self.distribution.dist_files: |
| 56 | self.upload_file(command, pyversion, filename) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 57 | |
Martin v. Löwis | 98da562 | 2005-03-23 18:54:36 +0000 | [diff] [blame] | 58 | def upload_file(self, command, pyversion, filename): |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 59 | # Sign if requested |
| 60 | if self.sign: |
Phillip J. Eby | 2e550b3 | 2006-03-30 02:12:14 +0000 | [diff] [blame] | 61 | gpg_args = ["gpg", "--detach-sign", "-a", filename] |
| 62 | if self.identity: |
| 63 | gpg_args[2:2] = ["--local-user", self.identity] |
| 64 | spawn(gpg_args, |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 65 | dry_run=self.dry_run) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 66 | |
Martin v. Löwis | 6d0c85a | 2006-01-08 10:48:54 +0000 | [diff] [blame] | 67 | # Fill in the data - send all the meta-data in case we need to |
| 68 | # register a new release |
Phillip J. Eby | 5cb7846 | 2005-07-07 15:36:20 +0000 | [diff] [blame] | 69 | content = open(filename,'rb').read() |
Martin v. Löwis | 6d0c85a | 2006-01-08 10:48:54 +0000 | [diff] [blame] | 70 | meta = self.distribution.metadata |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 71 | data = { |
Martin v. Löwis | 6d0c85a | 2006-01-08 10:48:54 +0000 | [diff] [blame] | 72 | # action |
| 73 | ':action': 'file_upload', |
| 74 | 'protcol_version': '1', |
| 75 | |
| 76 | # identify release |
| 77 | 'name': meta.get_name(), |
| 78 | 'version': meta.get_version(), |
| 79 | |
| 80 | # file content |
| 81 | 'content': (os.path.basename(filename),content), |
| 82 | 'filetype': command, |
| 83 | 'pyversion': pyversion, |
| 84 | 'md5_digest': md5(content).hexdigest(), |
| 85 | |
| 86 | # additional meta-data |
| 87 | 'metadata_version' : '1.0', |
| 88 | 'summary': meta.get_description(), |
| 89 | 'home_page': meta.get_url(), |
| 90 | 'author': meta.get_contact(), |
| 91 | 'author_email': meta.get_contact_email(), |
| 92 | 'license': meta.get_licence(), |
| 93 | 'description': meta.get_long_description(), |
| 94 | 'keywords': meta.get_keywords(), |
| 95 | 'platform': meta.get_platforms(), |
| 96 | 'classifiers': meta.get_classifiers(), |
| 97 | 'download_url': meta.get_download_url(), |
| 98 | # PEP 314 |
| 99 | 'provides': meta.get_provides(), |
| 100 | 'requires': meta.get_requires(), |
| 101 | 'obsoletes': meta.get_obsoletes(), |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 102 | } |
| 103 | comment = '' |
| 104 | if command == 'bdist_rpm': |
| 105 | dist, version, id = platform.dist() |
| 106 | if dist: |
| 107 | comment = 'built for %s %s' % (dist, version) |
| 108 | elif command == 'bdist_dumb': |
| 109 | comment = 'built for %s' % platform.platform(terse=1) |
| 110 | data['comment'] = comment |
| 111 | |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 112 | if self.sign: |
| 113 | data['gpg_signature'] = (os.path.basename(filename) + ".asc", |
| 114 | open(filename+".asc").read()) |
| 115 | |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 116 | # set up the authentication |
| 117 | auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip() |
| 118 | |
| 119 | # Build up the MIME payload for the POST data |
| 120 | boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' |
| 121 | sep_boundary = '\n--' + boundary |
| 122 | end_boundary = sep_boundary + '--' |
| 123 | body = StringIO.StringIO() |
| 124 | for key, value in data.items(): |
| 125 | # handle multiple entries for the same name |
| 126 | if type(value) != type([]): |
| 127 | value = [value] |
| 128 | for value in value: |
| 129 | if type(value) is tuple: |
| 130 | fn = ';filename="%s"' % value[0] |
| 131 | value = value[1] |
| 132 | else: |
| 133 | fn = "" |
| 134 | value = str(value) |
| 135 | body.write(sep_boundary) |
| 136 | body.write('\nContent-Disposition: form-data; name="%s"'%key) |
| 137 | body.write(fn) |
| 138 | body.write("\n\n") |
| 139 | body.write(value) |
| 140 | if value and value[-1] == '\r': |
| 141 | body.write('\n') # write an extra newline (lurve Macs) |
| 142 | body.write(end_boundary) |
| 143 | body.write("\n") |
| 144 | body = body.getvalue() |
| 145 | |
| 146 | self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO) |
| 147 | |
| 148 | # build the Request |
| 149 | # We can't use urllib2 since we need to send the Basic |
| 150 | # auth right with the first request |
| 151 | schema, netloc, url, params, query, fragments = \ |
| 152 | urlparse.urlparse(self.repository) |
| 153 | assert not params and not query and not fragments |
Tim Peters | eba28be | 2005-03-28 01:08:02 +0000 | [diff] [blame] | 154 | if schema == 'http': |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 155 | http = httplib.HTTPConnection(netloc) |
| 156 | elif schema == 'https': |
| 157 | http = httplib.HTTPSConnection(netloc) |
| 158 | else: |
| 159 | raise AssertionError, "unsupported schema "+schema |
| 160 | |
| 161 | data = '' |
| 162 | loglevel = log.INFO |
| 163 | try: |
| 164 | http.connect() |
| 165 | http.putrequest("POST", url) |
Tim Peters | eba28be | 2005-03-28 01:08:02 +0000 | [diff] [blame] | 166 | http.putheader('Content-type', |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 167 | 'multipart/form-data; boundary=%s'%boundary) |
| 168 | http.putheader('Content-length', str(len(body))) |
| 169 | http.putheader('Authorization', auth) |
| 170 | http.endheaders() |
| 171 | http.send(body) |
| 172 | except socket.error, e: |
Phillip J. Eby | 137ff79 | 2006-07-10 19:18:35 +0000 | [diff] [blame] | 173 | self.announce(str(e), log.ERROR) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 174 | return |
| 175 | |
| 176 | r = http.getresponse() |
| 177 | if r.status == 200: |
Tim Peters | eba28be | 2005-03-28 01:08:02 +0000 | [diff] [blame] | 178 | self.announce('Server response (%s): %s' % (r.status, r.reason), |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 179 | log.INFO) |
| 180 | else: |
Tim Peters | eba28be | 2005-03-28 01:08:02 +0000 | [diff] [blame] | 181 | self.announce('Upload failed (%s): %s' % (r.status, r.reason), |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 182 | log.ERROR) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 183 | if self.show_response: |
| 184 | print '-'*75, r.read(), '-'*75 |