check.py 5.5 KB

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