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).""" |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 4 | |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 5 | from distutils.errors import * |
Tarek Ziadé | 6f6f946 | 2009-06-28 21:10:49 +0000 | [diff] [blame] | 6 | from distutils.core import PyPIRCCommand |
| 7 | from distutils.spawn import spawn |
| 8 | from distutils import log |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 9 | import sys |
| 10 | import os, io |
| 11 | import socket |
| 12 | import platform |
| 13 | import configparser |
| 14 | import http.client as httpclient |
| 15 | from base64 import standard_b64encode |
| 16 | import urllib.parse |
| 17 | |
| 18 | # this keeps compatibility for 2.3 and 2.4 |
| 19 | if sys.version < "2.5": |
| 20 | from md5 import md5 |
| 21 | else: |
| 22 | from hashlib import md5 |
Tarek Ziadé | 38e3d51 | 2009-02-27 12:58:56 +0000 | [diff] [blame] | 23 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 24 | class upload(PyPIRCCommand): |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 25 | |
| 26 | description = "upload binary package to PyPI" |
| 27 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 28 | user_options = PyPIRCCommand.user_options + [ |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 29 | ('sign', 's', |
| 30 | 'sign files to upload using gpg'), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 31 | ('identity=', 'i', 'GPG identity used to sign files'), |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 32 | ] |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 33 | |
| 34 | boolean_options = PyPIRCCommand.boolean_options + ['sign'] |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 35 | |
| 36 | def initialize_options(self): |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 37 | PyPIRCCommand.initialize_options(self) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 38 | self.username = '' |
| 39 | self.password = '' |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 40 | self.show_response = 0 |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 41 | self.sign = False |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | self.identity = None |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 43 | |
| 44 | def finalize_options(self): |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 45 | PyPIRCCommand.finalize_options(self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 46 | if self.identity and not self.sign: |
| 47 | raise DistutilsOptionError( |
| 48 | "Must use --sign for --identity to have meaning" |
| 49 | ) |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 50 | 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öwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 56 | |
Tarek Ziadé | 13f7c3b | 2009-01-09 00:15:45 +0000 | [diff] [blame] | 57 | # getting the password from the distribution |
| 58 | # if previously set by the register command |
| 59 | if not self.password and self.distribution.password: |
| 60 | self.password = self.distribution.password |
| 61 | |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 62 | def run(self): |
| 63 | if not self.distribution.dist_files: |
| 64 | raise DistutilsOptionError("No dist file created in earlier command") |
Martin v. Löwis | 98da562 | 2005-03-23 18:54:36 +0000 | [diff] [blame] | 65 | for command, pyversion, filename in self.distribution.dist_files: |
| 66 | self.upload_file(command, pyversion, filename) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 67 | |
Martin v. Löwis | 98da562 | 2005-03-23 18:54:36 +0000 | [diff] [blame] | 68 | def upload_file(self, command, pyversion, filename): |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 69 | # Sign if requested |
| 70 | if self.sign: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 71 | gpg_args = ["gpg", "--detach-sign", "-a", filename] |
| 72 | if self.identity: |
| 73 | gpg_args[2:2] = ["--local-user", self.identity] |
| 74 | spawn(gpg_args, |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 75 | dry_run=self.dry_run) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 76 | |
Martin v. Löwis | 6d0c85a | 2006-01-08 10:48:54 +0000 | [diff] [blame] | 77 | # Fill in the data - send all the meta-data in case we need to |
| 78 | # register a new release |
Phillip J. Eby | 5cb7846 | 2005-07-07 15:36:20 +0000 | [diff] [blame] | 79 | content = open(filename,'rb').read() |
Martin v. Löwis | 6d0c85a | 2006-01-08 10:48:54 +0000 | [diff] [blame] | 80 | meta = self.distribution.metadata |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 81 | data = { |
Martin v. Löwis | 6d0c85a | 2006-01-08 10:48:54 +0000 | [diff] [blame] | 82 | # action |
| 83 | ':action': 'file_upload', |
| 84 | 'protcol_version': '1', |
| 85 | |
| 86 | # identify release |
| 87 | 'name': meta.get_name(), |
| 88 | 'version': meta.get_version(), |
| 89 | |
| 90 | # file content |
| 91 | 'content': (os.path.basename(filename),content), |
| 92 | 'filetype': command, |
| 93 | 'pyversion': pyversion, |
| 94 | 'md5_digest': md5(content).hexdigest(), |
| 95 | |
| 96 | # additional meta-data |
| 97 | 'metadata_version' : '1.0', |
| 98 | 'summary': meta.get_description(), |
| 99 | 'home_page': meta.get_url(), |
| 100 | 'author': meta.get_contact(), |
| 101 | 'author_email': meta.get_contact_email(), |
| 102 | 'license': meta.get_licence(), |
| 103 | 'description': meta.get_long_description(), |
| 104 | 'keywords': meta.get_keywords(), |
| 105 | 'platform': meta.get_platforms(), |
| 106 | 'classifiers': meta.get_classifiers(), |
| 107 | 'download_url': meta.get_download_url(), |
| 108 | # PEP 314 |
| 109 | 'provides': meta.get_provides(), |
| 110 | 'requires': meta.get_requires(), |
| 111 | 'obsoletes': meta.get_obsoletes(), |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 112 | } |
| 113 | comment = '' |
| 114 | if command == 'bdist_rpm': |
| 115 | dist, version, id = platform.dist() |
| 116 | if dist: |
| 117 | comment = 'built for %s %s' % (dist, version) |
| 118 | elif command == 'bdist_dumb': |
| 119 | comment = 'built for %s' % platform.platform(terse=1) |
| 120 | data['comment'] = comment |
| 121 | |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 122 | if self.sign: |
| 123 | data['gpg_signature'] = (os.path.basename(filename) + ".asc", |
| 124 | open(filename+".asc").read()) |
| 125 | |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 126 | # set up the authentication |
Amaury Forgeot d'Arc | 836b670 | 2008-11-20 23:53:46 +0000 | [diff] [blame] | 127 | user_pass = (self.username + ":" + self.password).encode('ascii') |
| 128 | # The exact encoding of the authentication string is debated. |
| 129 | # Anyway PyPI only accepts ascii for both username or password. |
Tarek Ziadé | 8b9361a | 2009-12-21 00:02:20 +0000 | [diff] [blame] | 130 | auth = "Basic " + standard_b64encode(user_pass).decode('ascii') |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 131 | |
| 132 | # Build up the MIME payload for the POST data |
| 133 | boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' |
Amaury Forgeot d'Arc | 836b670 | 2008-11-20 23:53:46 +0000 | [diff] [blame] | 134 | sep_boundary = b'\n--' + boundary.encode('ascii') |
| 135 | end_boundary = sep_boundary + b'--' |
| 136 | body = io.BytesIO() |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 137 | for key, value in data.items(): |
Amaury Forgeot d'Arc | 836b670 | 2008-11-20 23:53:46 +0000 | [diff] [blame] | 138 | title = '\nContent-Disposition: form-data; name="%s"' % key |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 139 | # handle multiple entries for the same name |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 140 | if type(value) != type([]): |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 141 | value = [value] |
| 142 | for value in value: |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 143 | if type(value) is tuple: |
Amaury Forgeot d'Arc | 836b670 | 2008-11-20 23:53:46 +0000 | [diff] [blame] | 144 | title += '; filename="%s"' % value[0] |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 145 | value = value[1] |
| 146 | else: |
Amaury Forgeot d'Arc | 836b670 | 2008-11-20 23:53:46 +0000 | [diff] [blame] | 147 | value = str(value).encode('utf-8') |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 148 | body.write(sep_boundary) |
Amaury Forgeot d'Arc | 836b670 | 2008-11-20 23:53:46 +0000 | [diff] [blame] | 149 | body.write(title.encode('utf-8')) |
| 150 | body.write(b"\n\n") |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 151 | body.write(value) |
Amaury Forgeot d'Arc | 836b670 | 2008-11-20 23:53:46 +0000 | [diff] [blame] | 152 | if value and value[-1:] == b'\r': |
| 153 | body.write(b'\n') # write an extra newline (lurve Macs) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 154 | body.write(end_boundary) |
Amaury Forgeot d'Arc | 836b670 | 2008-11-20 23:53:46 +0000 | [diff] [blame] | 155 | body.write(b"\n") |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 156 | body = body.getvalue() |
| 157 | |
| 158 | self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO) |
| 159 | |
| 160 | # build the Request |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 161 | # We can't use urllib since we need to send the Basic |
| 162 | # auth right with the first request |
| 163 | # TODO(jhylton): Can we fix urllib? |
| 164 | schema, netloc, url, params, query, fragments = \ |
| 165 | urllib.parse.urlparse(self.repository) |
| 166 | assert not params and not query and not fragments |
| 167 | if schema == 'http': |
| 168 | http = httpclient.HTTPConnection(netloc) |
| 169 | elif schema == 'https': |
| 170 | http = httpclient.HTTPSConnection(netloc) |
| 171 | else: |
| 172 | raise AssertionError("unsupported schema "+schema) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 173 | |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 174 | data = '' |
| 175 | loglevel = log.INFO |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 176 | try: |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 177 | http.connect() |
| 178 | http.putrequest("POST", url) |
| 179 | http.putheader('Content-type', |
| 180 | 'multipart/form-data; boundary=%s'%boundary) |
| 181 | http.putheader('Content-length', str(len(body))) |
| 182 | http.putheader('Authorization', auth) |
| 183 | http.endheaders() |
| 184 | http.send(body) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 185 | except socket.error as e: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 186 | self.announce(str(e), log.ERROR) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 187 | return |
| 188 | |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 189 | r = http.getresponse() |
| 190 | if r.status == 200: |
| 191 | self.announce('Server response (%s): %s' % (r.status, r.reason), |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 192 | log.INFO) |
| 193 | else: |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 194 | self.announce('Upload failed (%s): %s' % (r.status, r.reason), |
Martin v. Löwis | f74b923 | 2005-03-22 15:51:14 +0000 | [diff] [blame] | 195 | log.ERROR) |
Martin v. Löwis | 98858c9 | 2005-03-21 21:00:59 +0000 | [diff] [blame] | 196 | if self.show_response: |
Tarek Ziadé | 3679727 | 2010-07-22 12:50:05 +0000 | [diff] [blame] | 197 | self.announce('-'*75, r.read(), '-'*75) |