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