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