| 1 | n/a | """Upload a distribution to a project index.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import os |
|---|
| 4 | n/a | import socket |
|---|
| 5 | n/a | import logging |
|---|
| 6 | n/a | import platform |
|---|
| 7 | n/a | import urllib.parse |
|---|
| 8 | n/a | from base64 import standard_b64encode |
|---|
| 9 | n/a | from hashlib import md5 |
|---|
| 10 | n/a | from urllib.error import HTTPError |
|---|
| 11 | n/a | from urllib.request import urlopen, Request |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | from packaging import logger |
|---|
| 14 | n/a | from packaging.errors import PackagingOptionError |
|---|
| 15 | n/a | from packaging.util import (spawn, read_pypirc, DEFAULT_REPOSITORY, |
|---|
| 16 | n/a | DEFAULT_REALM, encode_multipart) |
|---|
| 17 | n/a | from packaging.command.cmd import Command |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | class upload(Command): |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | description = "upload distribution to PyPI" |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | user_options = [ |
|---|
| 25 | n/a | ('repository=', 'r', |
|---|
| 26 | n/a | "repository URL [default: %s]" % DEFAULT_REPOSITORY), |
|---|
| 27 | n/a | ('show-response', None, |
|---|
| 28 | n/a | "display full response text from server"), |
|---|
| 29 | n/a | ('sign', 's', |
|---|
| 30 | n/a | "sign files to upload using gpg"), |
|---|
| 31 | n/a | ('identity=', 'i', |
|---|
| 32 | n/a | "GPG identity used to sign files"), |
|---|
| 33 | n/a | ('upload-docs', None, |
|---|
| 34 | n/a | "upload documentation too"), |
|---|
| 35 | n/a | ] |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | boolean_options = ['show-response', 'sign'] |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | def initialize_options(self): |
|---|
| 40 | n/a | self.repository = None |
|---|
| 41 | n/a | self.realm = None |
|---|
| 42 | n/a | self.show_response = False |
|---|
| 43 | n/a | self.username = '' |
|---|
| 44 | n/a | self.password = '' |
|---|
| 45 | n/a | self.show_response = False |
|---|
| 46 | n/a | self.sign = False |
|---|
| 47 | n/a | self.identity = None |
|---|
| 48 | n/a | self.upload_docs = False |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | def finalize_options(self): |
|---|
| 51 | n/a | if self.repository is None: |
|---|
| 52 | n/a | self.repository = DEFAULT_REPOSITORY |
|---|
| 53 | n/a | if self.realm is None: |
|---|
| 54 | n/a | self.realm = DEFAULT_REALM |
|---|
| 55 | n/a | if self.identity and not self.sign: |
|---|
| 56 | n/a | raise PackagingOptionError( |
|---|
| 57 | n/a | "Must use --sign for --identity to have meaning") |
|---|
| 58 | n/a | config = read_pypirc(self.repository, self.realm) |
|---|
| 59 | n/a | if config != {}: |
|---|
| 60 | n/a | self.username = config['username'] |
|---|
| 61 | n/a | self.password = config['password'] |
|---|
| 62 | n/a | self.repository = config['repository'] |
|---|
| 63 | n/a | self.realm = config['realm'] |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | # getting the password from the distribution |
|---|
| 66 | n/a | # if previously set by the register command |
|---|
| 67 | n/a | if not self.password and self.distribution.password: |
|---|
| 68 | n/a | self.password = self.distribution.password |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | def run(self): |
|---|
| 71 | n/a | if not self.distribution.dist_files: |
|---|
| 72 | n/a | raise PackagingOptionError( |
|---|
| 73 | n/a | "No dist file created in earlier command") |
|---|
| 74 | n/a | for command, pyversion, filename in self.distribution.dist_files: |
|---|
| 75 | n/a | self.upload_file(command, pyversion, filename) |
|---|
| 76 | n/a | if self.upload_docs: |
|---|
| 77 | n/a | upload_docs = self.get_finalized_command("upload_docs") |
|---|
| 78 | n/a | upload_docs.repository = self.repository |
|---|
| 79 | n/a | upload_docs.username = self.username |
|---|
| 80 | n/a | upload_docs.password = self.password |
|---|
| 81 | n/a | upload_docs.run() |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | # XXX to be refactored with register.post_to_server |
|---|
| 84 | n/a | def upload_file(self, command, pyversion, filename): |
|---|
| 85 | n/a | # Makes sure the repository URL is compliant |
|---|
| 86 | n/a | scheme, netloc, url, params, query, fragments = \ |
|---|
| 87 | n/a | urllib.parse.urlparse(self.repository) |
|---|
| 88 | n/a | if params or query or fragments: |
|---|
| 89 | n/a | raise AssertionError("Incompatible url %s" % self.repository) |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | if scheme not in ('http', 'https'): |
|---|
| 92 | n/a | raise AssertionError("unsupported scheme " + scheme) |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | # Sign if requested |
|---|
| 95 | n/a | if self.sign: |
|---|
| 96 | n/a | gpg_args = ["gpg", "--detach-sign", "-a", filename] |
|---|
| 97 | n/a | if self.identity: |
|---|
| 98 | n/a | gpg_args[2:2] = ["--local-user", self.identity] |
|---|
| 99 | n/a | spawn(gpg_args, |
|---|
| 100 | n/a | dry_run=self.dry_run) |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | # Fill in the data - send all the metadata in case we need to |
|---|
| 103 | n/a | # register a new release |
|---|
| 104 | n/a | with open(filename, 'rb') as f: |
|---|
| 105 | n/a | content = f.read() |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | data = self.distribution.metadata.todict() |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | # extra upload infos |
|---|
| 110 | n/a | data[':action'] = 'file_upload' |
|---|
| 111 | n/a | data['protcol_version'] = '1' |
|---|
| 112 | n/a | data['content'] = (os.path.basename(filename), content) |
|---|
| 113 | n/a | data['filetype'] = command |
|---|
| 114 | n/a | data['pyversion'] = pyversion |
|---|
| 115 | n/a | data['md5_digest'] = md5(content).hexdigest() |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | if command == 'bdist_dumb': |
|---|
| 118 | n/a | data['comment'] = 'built for %s' % platform.platform(terse=True) |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | if self.sign: |
|---|
| 121 | n/a | with open(filename + '.asc') as fp: |
|---|
| 122 | n/a | sig = fp.read() |
|---|
| 123 | n/a | data['gpg_signature'] = [ |
|---|
| 124 | n/a | (os.path.basename(filename) + ".asc", sig)] |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | # set up the authentication |
|---|
| 127 | n/a | # The exact encoding of the authentication string is debated. |
|---|
| 128 | n/a | # Anyway PyPI only accepts ascii for both username or password. |
|---|
| 129 | n/a | user_pass = (self.username + ":" + self.password).encode('ascii') |
|---|
| 130 | n/a | auth = b"Basic " + standard_b64encode(user_pass) |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | # Build up the MIME payload for the POST data |
|---|
| 133 | n/a | files = [] |
|---|
| 134 | n/a | for key in ('content', 'gpg_signature'): |
|---|
| 135 | n/a | if key in data: |
|---|
| 136 | n/a | filename_, value = data.pop(key) |
|---|
| 137 | n/a | files.append((key, filename_, value)) |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | content_type, body = encode_multipart(data.items(), files) |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | logger.info("Submitting %s to %s", filename, self.repository) |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | # build the Request |
|---|
| 144 | n/a | headers = {'Content-type': content_type, |
|---|
| 145 | n/a | 'Content-length': str(len(body)), |
|---|
| 146 | n/a | 'Authorization': auth} |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | request = Request(self.repository, body, headers) |
|---|
| 149 | n/a | # send the data |
|---|
| 150 | n/a | try: |
|---|
| 151 | n/a | result = urlopen(request) |
|---|
| 152 | n/a | status = result.code |
|---|
| 153 | n/a | reason = result.msg |
|---|
| 154 | n/a | except socket.error as e: |
|---|
| 155 | n/a | logger.error(e) |
|---|
| 156 | n/a | return |
|---|
| 157 | n/a | except HTTPError as e: |
|---|
| 158 | n/a | status = e.code |
|---|
| 159 | n/a | reason = e.msg |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | if status == 200: |
|---|
| 162 | n/a | logger.info('Server response (%s): %s', status, reason) |
|---|
| 163 | n/a | else: |
|---|
| 164 | n/a | logger.error('Upload failed (%s): %s', status, reason) |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | if self.show_response and logger.isEnabledFor(logging.INFO): |
|---|
| 167 | n/a | sep = '-' * 75 |
|---|
| 168 | n/a | logger.info('%s\n%s\n%s', sep, result.read().decode(), sep) |
|---|