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