| 1 | n/a | """Upload HTML documentation to a project index.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import os |
|---|
| 4 | n/a | import base64 |
|---|
| 5 | n/a | import socket |
|---|
| 6 | n/a | import zipfile |
|---|
| 7 | n/a | import logging |
|---|
| 8 | n/a | import http.client |
|---|
| 9 | n/a | import urllib.parse |
|---|
| 10 | n/a | from io import BytesIO |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | from packaging import logger |
|---|
| 13 | n/a | from packaging.util import (read_pypirc, DEFAULT_REPOSITORY, DEFAULT_REALM, |
|---|
| 14 | n/a | encode_multipart) |
|---|
| 15 | n/a | from packaging.errors import PackagingFileError |
|---|
| 16 | n/a | from packaging.command.cmd import Command |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def zip_dir(directory): |
|---|
| 20 | n/a | """Compresses recursively contents of directory into a BytesIO object""" |
|---|
| 21 | n/a | destination = BytesIO() |
|---|
| 22 | n/a | with zipfile.ZipFile(destination, "w") as zip_file: |
|---|
| 23 | n/a | for root, dirs, files in os.walk(directory): |
|---|
| 24 | n/a | for name in files: |
|---|
| 25 | n/a | full = os.path.join(root, name) |
|---|
| 26 | n/a | relative = root[len(directory):].lstrip(os.path.sep) |
|---|
| 27 | n/a | dest = os.path.join(relative, name) |
|---|
| 28 | n/a | zip_file.write(full, dest) |
|---|
| 29 | n/a | return destination |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | class upload_docs(Command): |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | description = "upload HTML documentation to PyPI" |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | user_options = [ |
|---|
| 37 | n/a | ('repository=', 'r', |
|---|
| 38 | n/a | "repository URL [default: %s]" % DEFAULT_REPOSITORY), |
|---|
| 39 | n/a | ('show-response', None, |
|---|
| 40 | n/a | "display full response text from server"), |
|---|
| 41 | n/a | ('upload-dir=', None, |
|---|
| 42 | n/a | "directory to upload"), |
|---|
| 43 | n/a | ] |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | def initialize_options(self): |
|---|
| 46 | n/a | self.repository = None |
|---|
| 47 | n/a | self.realm = None |
|---|
| 48 | n/a | self.show_response = False |
|---|
| 49 | n/a | self.upload_dir = None |
|---|
| 50 | n/a | self.username = '' |
|---|
| 51 | n/a | self.password = '' |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | def finalize_options(self): |
|---|
| 54 | n/a | if self.repository is None: |
|---|
| 55 | n/a | self.repository = DEFAULT_REPOSITORY |
|---|
| 56 | n/a | if self.realm is None: |
|---|
| 57 | n/a | self.realm = DEFAULT_REALM |
|---|
| 58 | n/a | if self.upload_dir is None: |
|---|
| 59 | n/a | build = self.get_finalized_command('build') |
|---|
| 60 | n/a | self.upload_dir = os.path.join(build.build_base, "docs") |
|---|
| 61 | n/a | if not os.path.isdir(self.upload_dir): |
|---|
| 62 | n/a | self.upload_dir = os.path.join(build.build_base, "doc") |
|---|
| 63 | n/a | logger.info('Using upload directory %s', self.upload_dir) |
|---|
| 64 | n/a | self.verify_upload_dir(self.upload_dir) |
|---|
| 65 | n/a | config = read_pypirc(self.repository, self.realm) |
|---|
| 66 | n/a | if config != {}: |
|---|
| 67 | n/a | self.username = config['username'] |
|---|
| 68 | n/a | self.password = config['password'] |
|---|
| 69 | n/a | self.repository = config['repository'] |
|---|
| 70 | n/a | self.realm = config['realm'] |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | def verify_upload_dir(self, upload_dir): |
|---|
| 73 | n/a | self.ensure_dirname('upload_dir') |
|---|
| 74 | n/a | index_location = os.path.join(upload_dir, "index.html") |
|---|
| 75 | n/a | if not os.path.exists(index_location): |
|---|
| 76 | n/a | mesg = "No 'index.html found in docs directory (%s)" |
|---|
| 77 | n/a | raise PackagingFileError(mesg % upload_dir) |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def run(self): |
|---|
| 80 | n/a | name = self.distribution.metadata['Name'] |
|---|
| 81 | n/a | version = self.distribution.metadata['Version'] |
|---|
| 82 | n/a | zip_file = zip_dir(self.upload_dir) |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | fields = [(':action', 'doc_upload'), |
|---|
| 85 | n/a | ('name', name), ('version', version)] |
|---|
| 86 | n/a | files = [('content', name, zip_file.getvalue())] |
|---|
| 87 | n/a | content_type, body = encode_multipart(fields, files) |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | credentials = self.username + ':' + self.password |
|---|
| 90 | n/a | # FIXME should use explicit encoding |
|---|
| 91 | n/a | auth = b"Basic " + base64.encodebytes(credentials.encode()).strip() |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | logger.info("Submitting documentation to %s", self.repository) |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | scheme, netloc, url, params, query, fragments = urllib.parse.urlparse( |
|---|
| 96 | n/a | self.repository) |
|---|
| 97 | n/a | if scheme == "http": |
|---|
| 98 | n/a | conn = http.client.HTTPConnection(netloc) |
|---|
| 99 | n/a | elif scheme == "https": |
|---|
| 100 | n/a | conn = http.client.HTTPSConnection(netloc) |
|---|
| 101 | n/a | else: |
|---|
| 102 | n/a | raise AssertionError("unsupported scheme %r" % scheme) |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | try: |
|---|
| 105 | n/a | conn.connect() |
|---|
| 106 | n/a | conn.putrequest("POST", url) |
|---|
| 107 | n/a | conn.putheader('Content-type', content_type) |
|---|
| 108 | n/a | conn.putheader('Content-length', str(len(body))) |
|---|
| 109 | n/a | conn.putheader('Authorization', auth) |
|---|
| 110 | n/a | conn.endheaders() |
|---|
| 111 | n/a | conn.send(body) |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | except socket.error as e: |
|---|
| 114 | n/a | logger.error(e) |
|---|
| 115 | n/a | return |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | r = conn.getresponse() |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | if r.status == 200: |
|---|
| 120 | n/a | logger.info('Server response (%s): %s', r.status, r.reason) |
|---|
| 121 | n/a | elif r.status == 301: |
|---|
| 122 | n/a | location = r.getheader('Location') |
|---|
| 123 | n/a | if location is None: |
|---|
| 124 | n/a | location = 'http://packages.python.org/%s/' % name |
|---|
| 125 | n/a | logger.info('Upload successful. Visit %s', location) |
|---|
| 126 | n/a | else: |
|---|
| 127 | n/a | logger.error('Upload failed (%s): %s', r.status, r.reason) |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | if self.show_response and logger.isEnabledFor(logging.INFO): |
|---|
| 130 | n/a | sep = '-' * 75 |
|---|
| 131 | n/a | logger.info('%s\n%s\n%s', sep, r.read().decode('utf-8'), sep) |
|---|