ยปCore Development>Code coverage>Doc/tools/rstlint.py

Python code coverage for Doc/tools/rstlint.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a# -*- coding: utf-8 -*-
3n/a
4n/a# Check for stylistic and formal issues in .rst and .py
5n/a# files included in the documentation.
6n/a#
7n/a# 01/2009, Georg Brandl
8n/a
9n/a# TODO: - wrong versions in versionadded/changed
10n/a# - wrong markup after versionchanged directive
11n/a
12n/aimport os
13n/aimport re
14n/aimport sys
15n/aimport getopt
16n/afrom os.path import join, splitext, abspath, exists
17n/afrom collections import defaultdict
18n/a
19n/adirectives = [
20n/a # standard docutils ones
21n/a 'admonition', 'attention', 'caution', 'class', 'compound', 'container',
22n/a 'contents', 'csv-table', 'danger', 'date', 'default-role', 'epigraph',
23n/a 'error', 'figure', 'footer', 'header', 'highlights', 'hint', 'image',
24n/a 'important', 'include', 'line-block', 'list-table', 'meta', 'note',
25n/a 'parsed-literal', 'pull-quote', 'raw', 'replace',
26n/a 'restructuredtext-test-directive', 'role', 'rubric', 'sectnum', 'sidebar',
27n/a 'table', 'target-notes', 'tip', 'title', 'topic', 'unicode', 'warning',
28n/a # Sphinx and Python docs custom ones
29n/a 'acks', 'attribute', 'autoattribute', 'autoclass', 'autodata',
30n/a 'autoexception', 'autofunction', 'automethod', 'automodule', 'centered',
31n/a 'cfunction', 'class', 'classmethod', 'cmacro', 'cmdoption', 'cmember',
32n/a 'code-block', 'confval', 'cssclass', 'ctype', 'currentmodule', 'cvar',
33n/a 'data', 'decorator', 'decoratormethod', 'deprecated-removed',
34n/a 'deprecated(?!-removed)', 'describe', 'directive', 'doctest', 'envvar',
35n/a 'event', 'exception', 'function', 'glossary', 'highlight', 'highlightlang',
36n/a 'impl-detail', 'index', 'literalinclude', 'method', 'miscnews', 'module',
37n/a 'moduleauthor', 'opcode', 'pdbcommand', 'productionlist',
38n/a 'program', 'role', 'sectionauthor', 'seealso', 'sourcecode', 'staticmethod',
39n/a 'tabularcolumns', 'testcode', 'testoutput', 'testsetup', 'toctree', 'todo',
40n/a 'todolist', 'versionadded', 'versionchanged'
41n/a]
42n/a
43n/aall_directives = '(' + '|'.join(directives) + ')'
44n/aseems_directive_re = re.compile(r'(?<!\.)\.\. %s([^a-z:]|:(?!:))' % all_directives)
45n/adefault_role_re = re.compile(r'(^| )`\w([^`]*?\w)?`($| )')
46n/aleaked_markup_re = re.compile(r'[a-z]::\s|`|\.\.\s*\w+:')
47n/a
48n/a
49n/acheckers = {}
50n/a
51n/achecker_props = {'severity': 1, 'falsepositives': False}
52n/a
53n/a
54n/adef checker(*suffixes, **kwds):
55n/a """Decorator to register a function as a checker."""
56n/a def deco(func):
57n/a for suffix in suffixes:
58n/a checkers.setdefault(suffix, []).append(func)
59n/a for prop in checker_props:
60n/a setattr(func, prop, kwds.get(prop, checker_props[prop]))
61n/a return func
62n/a return deco
63n/a
64n/a
65n/a@checker('.py', severity=4)
66n/adef check_syntax(fn, lines):
67n/a """Check Python examples for valid syntax."""
68n/a code = ''.join(lines)
69n/a if '\r' in code:
70n/a if os.name != 'nt':
71n/a yield 0, '\\r in code file'
72n/a code = code.replace('\r', '')
73n/a try:
74n/a compile(code, fn, 'exec')
75n/a except SyntaxError as err:
76n/a yield err.lineno, 'not compilable: %s' % err
77n/a
78n/a
79n/a@checker('.rst', severity=2)
80n/adef check_suspicious_constructs(fn, lines):
81n/a """Check for suspicious reST constructs."""
82n/a inprod = False
83n/a for lno, line in enumerate(lines):
84n/a if seems_directive_re.search(line):
85n/a yield lno+1, 'comment seems to be intended as a directive'
86n/a if '.. productionlist::' in line:
87n/a inprod = True
88n/a elif not inprod and default_role_re.search(line):
89n/a yield lno+1, 'default role used'
90n/a elif inprod and not line.strip():
91n/a inprod = False
92n/a
93n/a
94n/a@checker('.py', '.rst')
95n/adef check_whitespace(fn, lines):
96n/a """Check for whitespace and line length issues."""
97n/a for lno, line in enumerate(lines):
98n/a if '\r' in line:
99n/a yield lno+1, '\\r in line'
100n/a if '\t' in line:
101n/a yield lno+1, 'OMG TABS!!!1'
102n/a if line[:-1].rstrip(' \t') != line[:-1]:
103n/a yield lno+1, 'trailing whitespace'
104n/a
105n/a
106n/a@checker('.rst', severity=0)
107n/adef check_line_length(fn, lines):
108n/a """Check for line length; this checker is not run by default."""
109n/a for lno, line in enumerate(lines):
110n/a if len(line) > 81:
111n/a # don't complain about tables, links and function signatures
112n/a if line.lstrip()[0] not in '+|' and \
113n/a 'http://' not in line and \
114n/a not line.lstrip().startswith(('.. function',
115n/a '.. method',
116n/a '.. cfunction')):
117n/a yield lno+1, "line too long"
118n/a
119n/a
120n/a@checker('.html', severity=2, falsepositives=True)
121n/adef check_leaked_markup(fn, lines):
122n/a """Check HTML files for leaked reST markup; this only works if
123n/a the HTML files have been built.
124n/a """
125n/a for lno, line in enumerate(lines):
126n/a if leaked_markup_re.search(line):
127n/a yield lno+1, 'possibly leaked markup: %r' % line
128n/a
129n/a
130n/adef main(argv):
131n/a usage = '''\
132n/aUsage: %s [-v] [-f] [-s sev] [-i path]* [path]
133n/a
134n/aOptions: -v verbose (print all checked file names)
135n/a -f enable checkers that yield many false positives
136n/a -s sev only show problems with severity >= sev
137n/a -i path ignore subdir or file path
138n/a''' % argv[0]
139n/a try:
140n/a gopts, args = getopt.getopt(argv[1:], 'vfs:i:')
141n/a except getopt.GetoptError:
142n/a print(usage)
143n/a return 2
144n/a
145n/a verbose = False
146n/a severity = 1
147n/a ignore = []
148n/a falsepos = False
149n/a for opt, val in gopts:
150n/a if opt == '-v':
151n/a verbose = True
152n/a elif opt == '-f':
153n/a falsepos = True
154n/a elif opt == '-s':
155n/a severity = int(val)
156n/a elif opt == '-i':
157n/a ignore.append(abspath(val))
158n/a
159n/a if len(args) == 0:
160n/a path = '.'
161n/a elif len(args) == 1:
162n/a path = args[0]
163n/a else:
164n/a print(usage)
165n/a return 2
166n/a
167n/a if not exists(path):
168n/a print('Error: path %s does not exist' % path)
169n/a return 2
170n/a
171n/a count = defaultdict(int)
172n/a
173n/a for root, dirs, files in os.walk(path):
174n/a # ignore subdirs in ignore list
175n/a if abspath(root) in ignore:
176n/a del dirs[:]
177n/a continue
178n/a
179n/a for fn in files:
180n/a fn = join(root, fn)
181n/a if fn[:2] == './':
182n/a fn = fn[2:]
183n/a
184n/a # ignore files in ignore list
185n/a if abspath(fn) in ignore:
186n/a continue
187n/a
188n/a ext = splitext(fn)[1]
189n/a checkerlist = checkers.get(ext, None)
190n/a if not checkerlist:
191n/a continue
192n/a
193n/a if verbose:
194n/a print('Checking %s...' % fn)
195n/a
196n/a try:
197n/a with open(fn, 'r', encoding='utf-8') as f:
198n/a lines = list(f)
199n/a except (IOError, OSError) as err:
200n/a print('%s: cannot open: %s' % (fn, err))
201n/a count[4] += 1
202n/a continue
203n/a
204n/a for checker in checkerlist:
205n/a if checker.falsepositives and not falsepos:
206n/a continue
207n/a csev = checker.severity
208n/a if csev >= severity:
209n/a for lno, msg in checker(fn, lines):
210n/a print('[%d] %s:%d: %s' % (csev, fn, lno, msg))
211n/a count[csev] += 1
212n/a if verbose:
213n/a print()
214n/a if not count:
215n/a if severity > 1:
216n/a print('No problems with severity >= %d found.' % severity)
217n/a else:
218n/a print('No problems found.')
219n/a else:
220n/a for severity in sorted(count):
221n/a number = count[severity]
222n/a print('%d problem%s with severity %d found.' %
223n/a (number, number > 1 and 's' or '', severity))
224n/a return int(bool(count))
225n/a
226n/a
227n/aif __name__ == '__main__':
228n/a sys.exit(main(sys.argv))