def upload_file(self, command, pyversion, filename): # Makes sure the repository URL is compliant scheme, netloc, url, params, query, fragments = \ urllib.parse.urlparse(self.repository) if params or query or fragments: raise AssertionError("Incompatible url %s" % self.repository) if scheme not in ('http', 'https'): raise AssertionError("unsupported scheme " + scheme) # Sign if requested if self.sign: gpg_args = ["gpg", "--detach-sign", "-a", filename] if self.identity: gpg_args[2:2] = ["--local-user", self.identity] spawn(gpg_args, dry_run=self.dry_run) # Fill in the data - send all the metadata in case we need to # register a new release with open(filename, 'rb') as f: content = f.read() data = self.distribution.metadata.todict() # extra upload infos data[':action'] = 'file_upload' data['protcol_version'] = '1' data['content'] = (os.path.basename(filename), content) data['filetype'] = command data['pyversion'] = pyversion data['md5_digest'] = md5(content).hexdigest() if command == 'bdist_dumb': data['comment'] = 'built for %s' % platform.platform(terse=True) if self.sign: with open(filename + '.asc') as fp: sig = fp.read() data['gpg_signature'] = [ (os.path.basename(filename) + ".asc", sig)] # set up the authentication # The exact encoding of the authentication string is debated. # Anyway PyPI only accepts ascii for both username or password. user_pass = (self.username + ":" + self.password).encode('ascii') auth = b"Basic " + standard_b64encode(user_pass) # Build up the MIME payload for the POST data files = [] for key in ('content', 'gpg_signature'): if key in data: filename_, value = data.pop(key) files.append((key, filename_, value)) content_type, body = encode_multipart(data.items(), files) logger.info("Submitting %s to %s", filename, self.repository) # build the Request headers = {'Content-type': content_type, 'Content-length': str(len(body)), 'Authorization': auth} request = Request(self.repository, body, headers) # send the data try: result = urlopen(request) status = result.code reason = result.msg except socket.error as e: logger.error(e) return except HTTPError as e: status = e.code reason = e.msg if status == 200: logger.info('Server response (%s): %s', status, reason) else: logger.error('Upload failed (%s): %s', status, reason) if self.show_response and logger.isEnabledFor(logging.INFO): sep = '-' * 75 logger.info('%s\n%s\n%s', sep, result.read().decode(), sep)
def spawn(self, cmd): spawn(cmd, dry_run=self.dry_run)
def spawn(self, cmd, search_path=True, level=1): """Spawn an external command respecting dry-run flag.""" from packaging.util import spawn spawn(cmd, search_path, dry_run=self.dry_run)