Path: blob/main/test/lib/python3.9/site-packages/setuptools/_distutils/command/check.py
4804 views
"""distutils.command.check12Implements the Distutils 'check' command.3"""4from email.utils import getaddresses56from distutils.core import Command7from distutils.errors import DistutilsSetupError89try:10# docutils is installed11from docutils.utils import Reporter12from docutils.parsers.rst import Parser13from docutils import frontend14from docutils import nodes1516class SilentReporter(Reporter):1718def __init__(self, source, report_level, halt_level, stream=None,19debug=0, encoding='ascii', error_handler='replace'):20self.messages = []21super().__init__(source, report_level, halt_level, stream,22debug, encoding, error_handler)2324def system_message(self, level, message, *children, **kwargs):25self.messages.append((level, message, children, kwargs))26return nodes.system_message(message, level=level,27type=self.levels[level],28*children, **kwargs)2930HAS_DOCUTILS = True31except Exception:32# Catch all exceptions because exceptions besides ImportError probably33# indicate that docutils is not ported to Py3k.34HAS_DOCUTILS = False3536class check(Command):37"""This command checks the meta-data of the package.38"""39description = ("perform some checks on the package")40user_options = [('metadata', 'm', 'Verify meta-data'),41('restructuredtext', 'r',42('Checks if long string meta-data syntax '43'are reStructuredText-compliant')),44('strict', 's',45'Will exit with an error if a check fails')]4647boolean_options = ['metadata', 'restructuredtext', 'strict']4849def initialize_options(self):50"""Sets default values for options."""51self.restructuredtext = 052self.metadata = 153self.strict = 054self._warnings = 05556def finalize_options(self):57pass5859def warn(self, msg):60"""Counts the number of warnings that occurs."""61self._warnings += 162return Command.warn(self, msg)6364def run(self):65"""Runs the command."""66# perform the various tests67if self.metadata:68self.check_metadata()69if self.restructuredtext:70if HAS_DOCUTILS:71self.check_restructuredtext()72elif self.strict:73raise DistutilsSetupError('The docutils package is needed.')7475# let's raise an error in strict mode, if we have at least76# one warning77if self.strict and self._warnings > 0:78raise DistutilsSetupError('Please correct your package.')7980def check_metadata(self):81"""Ensures that all required elements of meta-data are supplied.8283Required fields:84name, version8586Warns if any are missing.87"""88metadata = self.distribution.metadata8990missing = []91for attr in 'name', 'version':92if not getattr(metadata, attr, None):93missing.append(attr)9495if missing:96self.warn("missing required meta-data: %s" % ', '.join(missing))9798def check_restructuredtext(self):99"""Checks if the long string fields are reST-compliant."""100data = self.distribution.get_long_description()101for warning in self._check_rst_data(data):102line = warning[-1].get('line')103if line is None:104warning = warning[1]105else:106warning = '%s (line %s)' % (warning[1], line)107self.warn(warning)108109def _check_rst_data(self, data):110"""Returns warnings when the provided data doesn't compile."""111# the include and csv_table directives need this to be a path112source_path = self.distribution.script_name or 'setup.py'113parser = Parser()114settings = frontend.OptionParser(components=(Parser,)).get_default_values()115settings.tab_width = 4116settings.pep_references = None117settings.rfc_references = None118reporter = SilentReporter(source_path,119settings.report_level,120settings.halt_level,121stream=settings.warning_stream,122debug=settings.debug,123encoding=settings.error_encoding,124error_handler=settings.error_encoding_error_handler)125126document = nodes.document(settings, reporter, source=source_path)127document.note_source(source_path, -1)128try:129parser.parse(data, document)130except AttributeError as e:131reporter.messages.append(132(-1, 'Could not finish the parsing: %s.' % e, '', {}))133134return reporter.messages135136137