Path: blob/master/venv/Lib/site-packages/setuptools/_distutils/command/check.py
811 views
"""distutils.command.check12Implements the Distutils 'check' command.3"""4from distutils.core import Command5from distutils.errors import DistutilsSetupError67try:8# docutils is installed9from docutils.utils import Reporter10from docutils.parsers.rst import Parser11from docutils import frontend12from docutils import nodes1314class SilentReporter(Reporter):1516def __init__(self, source, report_level, halt_level, stream=None,17debug=0, encoding='ascii', error_handler='replace'):18self.messages = []19Reporter.__init__(self, source, report_level, halt_level, stream,20debug, encoding, error_handler)2122def system_message(self, level, message, *children, **kwargs):23self.messages.append((level, message, children, kwargs))24return nodes.system_message(message, level=level,25type=self.levels[level],26*children, **kwargs)2728HAS_DOCUTILS = True29except Exception:30# Catch all exceptions because exceptions besides ImportError probably31# indicate that docutils is not ported to Py3k.32HAS_DOCUTILS = False3334class check(Command):35"""This command checks the meta-data of the package.36"""37description = ("perform some checks on the package")38user_options = [('metadata', 'm', 'Verify meta-data'),39('restructuredtext', 'r',40('Checks if long string meta-data syntax '41'are reStructuredText-compliant')),42('strict', 's',43'Will exit with an error if a check fails')]4445boolean_options = ['metadata', 'restructuredtext', 'strict']4647def initialize_options(self):48"""Sets default values for options."""49self.restructuredtext = 050self.metadata = 151self.strict = 052self._warnings = 05354def finalize_options(self):55pass5657def warn(self, msg):58"""Counts the number of warnings that occurs."""59self._warnings += 160return Command.warn(self, msg)6162def run(self):63"""Runs the command."""64# perform the various tests65if self.metadata:66self.check_metadata()67if self.restructuredtext:68if HAS_DOCUTILS:69self.check_restructuredtext()70elif self.strict:71raise DistutilsSetupError('The docutils package is needed.')7273# let's raise an error in strict mode, if we have at least74# one warning75if self.strict and self._warnings > 0:76raise DistutilsSetupError('Please correct your package.')7778def check_metadata(self):79"""Ensures that all required elements of meta-data are supplied.8081Required fields:82name, version, URL8384Recommended fields:85(author and author_email) or (maintainer and maintainer_email))8687Warns if any are missing.88"""89metadata = self.distribution.metadata9091missing = []92for attr in ('name', 'version', 'url'):93if not (hasattr(metadata, attr) and getattr(metadata, attr)):94missing.append(attr)9596if missing:97self.warn("missing required meta-data: %s" % ', '.join(missing))98if metadata.author:99if not metadata.author_email:100self.warn("missing meta-data: if 'author' supplied, " +101"'author_email' should be supplied too")102elif metadata.maintainer:103if not metadata.maintainer_email:104self.warn("missing meta-data: if 'maintainer' supplied, " +105"'maintainer_email' should be supplied too")106else:107self.warn("missing meta-data: either (author and author_email) " +108"or (maintainer and maintainer_email) " +109"should be supplied")110111def check_restructuredtext(self):112"""Checks if the long string fields are reST-compliant."""113data = self.distribution.get_long_description()114for warning in self._check_rst_data(data):115line = warning[-1].get('line')116if line is None:117warning = warning[1]118else:119warning = '%s (line %s)' % (warning[1], line)120self.warn(warning)121122def _check_rst_data(self, data):123"""Returns warnings when the provided data doesn't compile."""124# the include and csv_table directives need this to be a path125source_path = self.distribution.script_name or 'setup.py'126parser = Parser()127settings = frontend.OptionParser(components=(Parser,)).get_default_values()128settings.tab_width = 4129settings.pep_references = None130settings.rfc_references = None131reporter = SilentReporter(source_path,132settings.report_level,133settings.halt_level,134stream=settings.warning_stream,135debug=settings.debug,136encoding=settings.error_encoding,137error_handler=settings.error_encoding_error_handler)138139document = nodes.document(settings, reporter, source=source_path)140document.note_source(source_path, -1)141try:142parser.parse(data, document)143except AttributeError as e:144reporter.messages.append(145(-1, 'Could not finish the parsing: %s.' % e, '', {}))146147return reporter.messages148149150