| 1 | n/a | """distutils.command.check |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Implements the Distutils 'check' command. |
|---|
| 4 | n/a | """ |
|---|
| 5 | n/a | from distutils.core import Command |
|---|
| 6 | n/a | from distutils.errors import DistutilsSetupError |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | try: |
|---|
| 9 | n/a | # docutils is installed |
|---|
| 10 | n/a | from docutils.utils import Reporter |
|---|
| 11 | n/a | from docutils.parsers.rst import Parser |
|---|
| 12 | n/a | from docutils import frontend |
|---|
| 13 | n/a | from docutils import nodes |
|---|
| 14 | n/a | from io import StringIO |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | class SilentReporter(Reporter): |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | def __init__(self, source, report_level, halt_level, stream=None, |
|---|
| 19 | n/a | debug=0, encoding='ascii', error_handler='replace'): |
|---|
| 20 | n/a | self.messages = [] |
|---|
| 21 | n/a | Reporter.__init__(self, source, report_level, halt_level, stream, |
|---|
| 22 | n/a | debug, encoding, error_handler) |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | def system_message(self, level, message, *children, **kwargs): |
|---|
| 25 | n/a | self.messages.append((level, message, children, kwargs)) |
|---|
| 26 | n/a | return nodes.system_message(message, level=level, |
|---|
| 27 | n/a | type=self.levels[level], |
|---|
| 28 | n/a | *children, **kwargs) |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | HAS_DOCUTILS = True |
|---|
| 31 | n/a | except Exception: |
|---|
| 32 | n/a | # Catch all exceptions because exceptions besides ImportError probably |
|---|
| 33 | n/a | # indicate that docutils is not ported to Py3k. |
|---|
| 34 | n/a | HAS_DOCUTILS = False |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | class check(Command): |
|---|
| 37 | n/a | """This command checks the meta-data of the package. |
|---|
| 38 | n/a | """ |
|---|
| 39 | n/a | description = ("perform some checks on the package") |
|---|
| 40 | n/a | user_options = [('metadata', 'm', 'Verify meta-data'), |
|---|
| 41 | n/a | ('restructuredtext', 'r', |
|---|
| 42 | n/a | ('Checks if long string meta-data syntax ' |
|---|
| 43 | n/a | 'are reStructuredText-compliant')), |
|---|
| 44 | n/a | ('strict', 's', |
|---|
| 45 | n/a | 'Will exit with an error if a check fails')] |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | boolean_options = ['metadata', 'restructuredtext', 'strict'] |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | def initialize_options(self): |
|---|
| 50 | n/a | """Sets default values for options.""" |
|---|
| 51 | n/a | self.restructuredtext = 0 |
|---|
| 52 | n/a | self.metadata = 1 |
|---|
| 53 | n/a | self.strict = 0 |
|---|
| 54 | n/a | self._warnings = 0 |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | def finalize_options(self): |
|---|
| 57 | n/a | pass |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | def warn(self, msg): |
|---|
| 60 | n/a | """Counts the number of warnings that occurs.""" |
|---|
| 61 | n/a | self._warnings += 1 |
|---|
| 62 | n/a | return Command.warn(self, msg) |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | def run(self): |
|---|
| 65 | n/a | """Runs the command.""" |
|---|
| 66 | n/a | # perform the various tests |
|---|
| 67 | n/a | if self.metadata: |
|---|
| 68 | n/a | self.check_metadata() |
|---|
| 69 | n/a | if self.restructuredtext: |
|---|
| 70 | n/a | if HAS_DOCUTILS: |
|---|
| 71 | n/a | self.check_restructuredtext() |
|---|
| 72 | n/a | elif self.strict: |
|---|
| 73 | n/a | raise DistutilsSetupError('The docutils package is needed.') |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | # let's raise an error in strict mode, if we have at least |
|---|
| 76 | n/a | # one warning |
|---|
| 77 | n/a | if self.strict and self._warnings > 0: |
|---|
| 78 | n/a | raise DistutilsSetupError('Please correct your package.') |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | def check_metadata(self): |
|---|
| 81 | n/a | """Ensures that all required elements of meta-data are supplied. |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | name, version, URL, (author and author_email) or |
|---|
| 84 | n/a | (maintainer and maintainer_email)). |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | Warns if any are missing. |
|---|
| 87 | n/a | """ |
|---|
| 88 | n/a | metadata = self.distribution.metadata |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | missing = [] |
|---|
| 91 | n/a | for attr in ('name', 'version', 'url'): |
|---|
| 92 | n/a | if not (hasattr(metadata, attr) and getattr(metadata, attr)): |
|---|
| 93 | n/a | missing.append(attr) |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | if missing: |
|---|
| 96 | n/a | self.warn("missing required meta-data: %s" % ', '.join(missing)) |
|---|
| 97 | n/a | if metadata.author: |
|---|
| 98 | n/a | if not metadata.author_email: |
|---|
| 99 | n/a | self.warn("missing meta-data: if 'author' supplied, " + |
|---|
| 100 | n/a | "'author_email' must be supplied too") |
|---|
| 101 | n/a | elif metadata.maintainer: |
|---|
| 102 | n/a | if not metadata.maintainer_email: |
|---|
| 103 | n/a | self.warn("missing meta-data: if 'maintainer' supplied, " + |
|---|
| 104 | n/a | "'maintainer_email' must be supplied too") |
|---|
| 105 | n/a | else: |
|---|
| 106 | n/a | self.warn("missing meta-data: either (author and author_email) " + |
|---|
| 107 | n/a | "or (maintainer and maintainer_email) " + |
|---|
| 108 | n/a | "must be supplied") |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | def check_restructuredtext(self): |
|---|
| 111 | n/a | """Checks if the long string fields are reST-compliant.""" |
|---|
| 112 | n/a | data = self.distribution.get_long_description() |
|---|
| 113 | n/a | for warning in self._check_rst_data(data): |
|---|
| 114 | n/a | line = warning[-1].get('line') |
|---|
| 115 | n/a | if line is None: |
|---|
| 116 | n/a | warning = warning[1] |
|---|
| 117 | n/a | else: |
|---|
| 118 | n/a | warning = '%s (line %s)' % (warning[1], line) |
|---|
| 119 | n/a | self.warn(warning) |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | def _check_rst_data(self, data): |
|---|
| 122 | n/a | """Returns warnings when the provided data doesn't compile.""" |
|---|
| 123 | n/a | source_path = StringIO() |
|---|
| 124 | n/a | parser = Parser() |
|---|
| 125 | n/a | settings = frontend.OptionParser(components=(Parser,)).get_default_values() |
|---|
| 126 | n/a | settings.tab_width = 4 |
|---|
| 127 | n/a | settings.pep_references = None |
|---|
| 128 | n/a | settings.rfc_references = None |
|---|
| 129 | n/a | reporter = SilentReporter(source_path, |
|---|
| 130 | n/a | settings.report_level, |
|---|
| 131 | n/a | settings.halt_level, |
|---|
| 132 | n/a | stream=settings.warning_stream, |
|---|
| 133 | n/a | debug=settings.debug, |
|---|
| 134 | n/a | encoding=settings.error_encoding, |
|---|
| 135 | n/a | error_handler=settings.error_encoding_error_handler) |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | document = nodes.document(settings, reporter, source=source_path) |
|---|
| 138 | n/a | document.note_source(source_path, -1) |
|---|
| 139 | n/a | try: |
|---|
| 140 | n/a | parser.parse(data, document) |
|---|
| 141 | n/a | except AttributeError as e: |
|---|
| 142 | n/a | reporter.messages.append( |
|---|
| 143 | n/a | (-1, 'Could not finish the parsing: %s.' % e, '', {})) |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | return reporter.messages |
|---|