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