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