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 * |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +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 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +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 |
Alexandre Vassalotti | 1d1eaa4 | 2008-05-14 22:59:42 +0000 | [diff] [blame] | 13 | import configparser |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 14 | import http.client |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 15 | import base64 |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame^] | 16 | import urllib.parse |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 17 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 18 | class upload(PyPIRCCommand): |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 19 | |
| 20 | description = "upload binary package to PyPI" |
| 21 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 22 | user_options = PyPIRCCommand.user_options + [ |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 23 | ('sign', 's', |
| 24 | 'sign files to upload using gpg'), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 25 | ('identity=', 'i', 'GPG identity used to sign files'), |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 26 | ] |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 27 | |
| 28 | boolean_options = PyPIRCCommand.boolean_options + ['sign'] |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 29 | |
| 30 | def initialize_options(self): |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 31 | PyPIRCCommand.initialize_options(self) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 32 | self.username = '' |
| 33 | self.password = '' |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 34 | self.show_response = 0 |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 35 | self.sign = False |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 36 | self.identity = None |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 37 | |
| 38 | def finalize_options(self): |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 39 | PyPIRCCommand.finalize_options(self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 40 | if self.identity and not self.sign: |
| 41 | raise DistutilsOptionError( |
| 42 | "Must use --sign for --identity to have meaning" |
| 43 | ) |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 44 | config = self._read_pypirc() |
| 45 | if config != {}: |
| 46 | self.username = config['username'] |
| 47 | self.password = config['password'] |
| 48 | self.repository = config['repository'] |
| 49 | self.realm = config['realm'] |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 50 | |
| 51 | def run(self): |
| 52 | if not self.distribution.dist_files: |
| 53 | raise DistutilsOptionError("No dist file created in earlier command") |
Martin v. Löwis | 98da562 | 2005-03-23 18:54:36 +0000 | [diff] [blame] | 54 | for command, pyversion, filename in self.distribution.dist_files: |
| 55 | self.upload_file(command, pyversion, filename) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 56 | |
Martin v. Löwis | 98da562 | 2005-03-23 18:54:36 +0000 | [diff] [blame] | 57 | def upload_file(self, command, pyversion, filename): |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 58 | # Sign if requested |
| 59 | if self.sign: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 60 | gpg_args = ["gpg", "--detach-sign", "-a", filename] |
| 61 | if self.identity: |
| 62 | gpg_args[2:2] = ["--local-user", self.identity] |
| 63 | spawn(gpg_args, |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 64 | dry_run=self.dry_run) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 65 | |
Martin v. Löwis | 6d0c85a | 2006-01-08 10:48:54 +0000 | [diff] [blame] | 66 | # Fill in the data - send all the meta-data in case we need to |
| 67 | # register a new release |
Phillip J. Eby | 5cb7846 | 2005-07-07 15:36:20 +0000 | [diff] [blame] | 68 | content = open(filename,'rb').read() |
Martin v. Löwis | 6d0c85a | 2006-01-08 10:48:54 +0000 | [diff] [blame] | 69 | meta = self.distribution.metadata |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 70 | data = { |
Martin v. Löwis | 6d0c85a | 2006-01-08 10:48:54 +0000 | [diff] [blame] | 71 | # action |
| 72 | ':action': 'file_upload', |
| 73 | 'protcol_version': '1', |
| 74 | |
| 75 | # identify release |
| 76 | 'name': meta.get_name(), |
| 77 | 'version': meta.get_version(), |
| 78 | |
| 79 | # file content |
| 80 | 'content': (os.path.basename(filename),content), |
| 81 | 'filetype': command, |
| 82 | 'pyversion': pyversion, |
| 83 | 'md5_digest': md5(content).hexdigest(), |
| 84 | |
| 85 | # additional meta-data |
| 86 | 'metadata_version' : '1.0', |
| 87 | 'summary': meta.get_description(), |
| 88 | 'home_page': meta.get_url(), |
| 89 | 'author': meta.get_contact(), |
| 90 | 'author_email': meta.get_contact_email(), |
| 91 | 'license': meta.get_licence(), |
| 92 | 'description': meta.get_long_description(), |
| 93 | 'keywords': meta.get_keywords(), |
| 94 | 'platform': meta.get_platforms(), |
| 95 | 'classifiers': meta.get_classifiers(), |
| 96 | 'download_url': meta.get_download_url(), |
| 97 | # PEP 314 |
| 98 | 'provides': meta.get_provides(), |
| 99 | 'requires': meta.get_requires(), |
| 100 | 'obsoletes': meta.get_obsoletes(), |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 101 | } |
| 102 | comment = '' |
| 103 | if command == 'bdist_rpm': |
| 104 | dist, version, id = platform.dist() |
| 105 | if dist: |
| 106 | comment = 'built for %s %s' % (dist, version) |
| 107 | elif command == 'bdist_dumb': |
| 108 | comment = 'built for %s' % platform.platform(terse=1) |
| 109 | data['comment'] = comment |
| 110 | |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 111 | if self.sign: |
| 112 | data['gpg_signature'] = (os.path.basename(filename) + ".asc", |
| 113 | open(filename+".asc").read()) |
| 114 | |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 115 | # set up the authentication |
| 116 | auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip() |
| 117 | |
| 118 | # Build up the MIME payload for the POST data |
| 119 | boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' |
| 120 | sep_boundary = '\n--' + boundary |
| 121 | end_boundary = sep_boundary + '--' |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 122 | body = io.StringIO() |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 123 | for key, value in data.items(): |
| 124 | # handle multiple entries for the same name |
| 125 | if type(value) != type([]): |
| 126 | value = [value] |
| 127 | for value in value: |
| 128 | if type(value) is tuple: |
| 129 | fn = ';filename="%s"' % value[0] |
| 130 | value = value[1] |
| 131 | else: |
| 132 | fn = "" |
| 133 | value = str(value) |
| 134 | body.write(sep_boundary) |
| 135 | body.write('\nContent-Disposition: form-data; name="%s"'%key) |
| 136 | body.write(fn) |
| 137 | body.write("\n\n") |
| 138 | body.write(value) |
| 139 | if value and value[-1] == '\r': |
| 140 | body.write('\n') # write an extra newline (lurve Macs) |
| 141 | body.write(end_boundary) |
| 142 | body.write("\n") |
| 143 | body = body.getvalue() |
| 144 | |
| 145 | self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO) |
| 146 | |
| 147 | # build the Request |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame^] | 148 | # We can't use urllib since we need to send the Basic |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 149 | # auth right with the first request |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame^] | 150 | # TODO(jhylton): Can we fix urllib? |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 151 | schema, netloc, url, params, query, fragments = \ |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame^] | 152 | urllib.parse.urlparse(self.repository) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 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': |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 155 | http = http.client.HTTPConnection(netloc) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 156 | elif schema == 'https': |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 157 | http = http.client.HTTPSConnection(netloc) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 158 | else: |
Collin Winter | 5b7e9d7 | 2007-08-30 03:52:21 +0000 | [diff] [blame] | 159 | raise AssertionError("unsupported schema "+schema) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 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) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 172 | except socket.error as e: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +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: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 184 | print('-'*75, r.read(), '-'*75) |