check.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. """distutils.command.check
  2. Implements the Distutils 'check' command.
  3. """
  4. import contextlib
  5. from ..core import Command
  6. from ..errors import DistutilsSetupError
  7. with contextlib.suppress(ImportError):
  8. import docutils.utils
  9. import docutils.parsers.rst
  10. import docutils.frontend
  11. import docutils.nodes
  12. class SilentReporter(docutils.utils.Reporter):
  13. def __init__(
  14. self,
  15. source,
  16. report_level,
  17. halt_level,
  18. stream=None,
  19. debug=0,
  20. encoding='ascii',
  21. error_handler='replace',
  22. ):
  23. self.messages = []
  24. super().__init__(
  25. source, report_level, halt_level, stream, debug, encoding, error_handler
  26. )
  27. def system_message(self, level, message, *children, **kwargs):
  28. self.messages.append((level, message, children, kwargs))
  29. return docutils.nodes.system_message(
  30. message, level=level, type=self.levels[level], *children, **kwargs
  31. )
  32. class check(Command):
  33. """This command checks the meta-data of the package."""
  34. description = "perform some checks on the package"
  35. user_options = [
  36. ('metadata', 'm', 'Verify meta-data'),
  37. (
  38. 'restructuredtext',
  39. 'r',
  40. (
  41. 'Checks if long string meta-data syntax '
  42. 'are reStructuredText-compliant'
  43. ),
  44. ),
  45. ('strict', 's', 'Will exit with an error if a check fails'),
  46. ]
  47. boolean_options = ['metadata', 'restructuredtext', 'strict']
  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. def finalize_options(self):
  55. pass
  56. def warn(self, msg):
  57. """Counts the number of warnings that occurs."""
  58. self._warnings += 1
  59. return Command.warn(self, msg)
  60. def run(self):
  61. """Runs the command."""
  62. # perform the various tests
  63. if self.metadata:
  64. self.check_metadata()
  65. if self.restructuredtext:
  66. if 'docutils' in globals():
  67. try:
  68. self.check_restructuredtext()
  69. except TypeError as exc:
  70. raise DistutilsSetupError(str(exc))
  71. elif self.strict:
  72. raise DistutilsSetupError('The docutils package is needed.')
  73. # let's raise an error in strict mode, if we have at least
  74. # one warning
  75. if self.strict and self._warnings > 0:
  76. raise DistutilsSetupError('Please correct your package.')
  77. def check_metadata(self):
  78. """Ensures that all required elements of meta-data are supplied.
  79. Required fields:
  80. name, version
  81. Warns if any are missing.
  82. """
  83. metadata = self.distribution.metadata
  84. missing = []
  85. for attr in 'name', 'version':
  86. if not getattr(metadata, attr, None):
  87. missing.append(attr)
  88. if missing:
  89. self.warn("missing required meta-data: %s" % ', '.join(missing))
  90. def check_restructuredtext(self):
  91. """Checks if the long string fields are reST-compliant."""
  92. data = self.distribution.get_long_description()
  93. for warning in self._check_rst_data(data):
  94. line = warning[-1].get('line')
  95. if line is None:
  96. warning = warning[1]
  97. else:
  98. warning = '{} (line {})'.format(warning[1], line)
  99. self.warn(warning)
  100. def _check_rst_data(self, data):
  101. """Returns warnings when the provided data doesn't compile."""
  102. # the include and csv_table directives need this to be a path
  103. source_path = self.distribution.script_name or 'setup.py'
  104. parser = docutils.parsers.rst.Parser()
  105. settings = docutils.frontend.OptionParser(
  106. components=(docutils.parsers.rst.Parser,)
  107. ).get_default_values()
  108. settings.tab_width = 4
  109. settings.pep_references = None
  110. settings.rfc_references = None
  111. reporter = SilentReporter(
  112. source_path,
  113. settings.report_level,
  114. settings.halt_level,
  115. stream=settings.warning_stream,
  116. debug=settings.debug,
  117. encoding=settings.error_encoding,
  118. error_handler=settings.error_encoding_error_handler,
  119. )
  120. document = docutils.nodes.document(settings, reporter, source=source_path)
  121. document.note_source(source_path, -1)
  122. try:
  123. parser.parse(data, document)
  124. except AttributeError as e:
  125. reporter.messages.append(
  126. (-1, 'Could not finish the parsing: %s.' % e, '', {})
  127. )
  128. return reporter.messages