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