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