|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +API for the command-line I{pyflakes} tool. |
| 4 | +
|
| 5 | +This is a copy of pyflakes/api.py file which excludes some files |
| 6 | +for twistedchecker. |
| 7 | +""" |
| 8 | +from __future__ import with_statement |
| 9 | + |
| 10 | +import sys |
| 11 | +import os |
| 12 | +import re |
| 13 | +import _ast |
| 14 | +from optparse import OptionParser |
| 15 | + |
| 16 | +from pyflakes import checker, __version__ |
| 17 | +from pyflakes import reporter as modReporter |
| 18 | + |
| 19 | +__all__ = ['check', 'checkPath', 'checkRecursive', 'iterSourceCode', 'main'] |
| 20 | + |
| 21 | + |
| 22 | +RE_EXCLUDE = '.*functionaltests.*' |
| 23 | + |
| 24 | + |
| 25 | +def check(codeString, filename, reporter=None): |
| 26 | + """ |
| 27 | + Check the Python source given by C{codeString} for flakes. |
| 28 | +
|
| 29 | + @param codeString: The Python source to check. |
| 30 | + @type codeString: C{str} |
| 31 | +
|
| 32 | + @param filename: The name of the file the source came from, used to report |
| 33 | + errors. |
| 34 | + @type filename: C{str} |
| 35 | +
|
| 36 | + @param reporter: A L{Reporter} instance, where errors and warnings will be |
| 37 | + reported. |
| 38 | +
|
| 39 | + @return: The number of warnings emitted. |
| 40 | + @rtype: C{int} |
| 41 | + """ |
| 42 | + if reporter is None: |
| 43 | + reporter = modReporter._makeDefaultReporter() |
| 44 | + # First, compile into an AST and handle syntax errors. |
| 45 | + try: |
| 46 | + tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST) |
| 47 | + except SyntaxError: |
| 48 | + value = sys.exc_info()[1] |
| 49 | + msg = value.args[0] |
| 50 | + |
| 51 | + (lineno, offset, text) = value.lineno, value.offset, value.text |
| 52 | + |
| 53 | + # If there's an encoding problem with the file, the text is None. |
| 54 | + if text is None: |
| 55 | + # Avoid using msg, since for the only known case, it contains a |
| 56 | + # bogus message that claims the encoding the file declared was |
| 57 | + # unknown. |
| 58 | + reporter.unexpectedError(filename, 'problem decoding source') |
| 59 | + else: |
| 60 | + reporter.syntaxError(filename, msg, lineno, offset, text) |
| 61 | + return 1 |
| 62 | + except Exception: |
| 63 | + reporter.unexpectedError(filename, 'problem decoding source') |
| 64 | + return 1 |
| 65 | + # Okay, it's syntactically valid. Now check it. |
| 66 | + w = checker.Checker(tree, filename) |
| 67 | + w.messages.sort(key=lambda m: m.lineno) |
| 68 | + for warning in w.messages: |
| 69 | + reporter.flake(warning) |
| 70 | + return len(w.messages) |
| 71 | + |
| 72 | + |
| 73 | +def checkPath(filename, reporter=None): |
| 74 | + """ |
| 75 | + Check the given path, printing out any warnings detected. |
| 76 | +
|
| 77 | + @param reporter: A L{Reporter} instance, where errors and warnings will be |
| 78 | + reported. |
| 79 | +
|
| 80 | + @return: the number of warnings printed |
| 81 | + """ |
| 82 | + if reporter is None: |
| 83 | + reporter = modReporter._makeDefaultReporter() |
| 84 | + try: |
| 85 | + with open(filename, 'rb') as f: |
| 86 | + codestr = f.read() |
| 87 | + if sys.version_info < (2, 7): |
| 88 | + codestr += '\n' # Work around for Python <= 2.6 |
| 89 | + except UnicodeError: |
| 90 | + reporter.unexpectedError(filename, 'problem decoding source') |
| 91 | + return 1 |
| 92 | + except IOError: |
| 93 | + msg = sys.exc_info()[1] |
| 94 | + reporter.unexpectedError(filename, msg.args[1]) |
| 95 | + return 1 |
| 96 | + return check(codestr, filename, reporter) |
| 97 | + |
| 98 | + |
| 99 | +def iterSourceCode(paths): |
| 100 | + """ |
| 101 | + Iterate over all Python source files in C{paths}. |
| 102 | +
|
| 103 | + @param paths: A list of paths. Directories will be recursed into and |
| 104 | + any .py files found will be yielded. Any non-directories will be |
| 105 | + yielded as-is. |
| 106 | + """ |
| 107 | + for path in paths: |
| 108 | + if os.path.isdir(path): |
| 109 | + for dirpath, dirnames, filenames in os.walk(path): |
| 110 | + for filename in filenames: |
| 111 | + if filename.endswith('.py'): |
| 112 | + yield os.path.join(dirpath, filename) |
| 113 | + else: |
| 114 | + yield path |
| 115 | + |
| 116 | + |
| 117 | +def checkRecursive(paths, reporter): |
| 118 | + """ |
| 119 | + Recursively check all source files in C{paths}. |
| 120 | +
|
| 121 | + @param paths: A list of paths to Python source files and directories |
| 122 | + containing Python source files. |
| 123 | + @param reporter: A L{Reporter} where all of the warnings and errors |
| 124 | + will be reported to. |
| 125 | + @return: The number of warnings found. |
| 126 | + """ |
| 127 | + warnings = 0 |
| 128 | + for sourcePath in iterSourceCode(paths): |
| 129 | + if re.search(RE_EXCLUDE, sourcePath): |
| 130 | + continue |
| 131 | + warnings += checkPath(sourcePath, reporter) |
| 132 | + return warnings |
| 133 | + |
| 134 | + |
| 135 | +def main(prog=None): |
| 136 | + parser = OptionParser(prog=prog, version=__version__) |
| 137 | + (__, args) = parser.parse_args() |
| 138 | + reporter = modReporter._makeDefaultReporter() |
| 139 | + if args: |
| 140 | + warnings = checkRecursive(args, reporter) |
| 141 | + else: |
| 142 | + warnings = check(sys.stdin.read(), '<stdin>', reporter) |
| 143 | + raise SystemExit(warnings > 0) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + main() |
0 commit comments