diff -r 16f9875e278b -r e8882d16384c UtilitiesPython2/Py2SyntaxChecker.py --- a/UtilitiesPython2/Py2SyntaxChecker.py Sun Jan 02 12:01:37 2011 +0100 +++ b/UtilitiesPython2/Py2SyntaxChecker.py Mon Jan 03 17:10:45 2011 +0100 @@ -12,6 +12,9 @@ import traceback from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32 +from py2flakes.checker import Checker +from py2flakes.messages import ImportStarUsed + coding_regexps = [ (2, re.compile(r'''coding[:=]\s*([-\w_.]+)''')), (1, re.compile(r'''<\?xml.*\bencoding\s*=\s*['"]([-\w_.]+)['"]\?>''')), @@ -72,9 +75,10 @@ @param file source filename (string) @return A tuple indicating status (1 = an error was found), the - filename, the linenumber, the code string and the error message - (boolean, string, string, string, string). The values are only - valid, if the status equals 1. + filename, the linenumber, the code string, the error message + and the full source code (boolean, string, string, string, + string, string). The values are only valid, if the status + equals 1. """ import __builtin__ try: @@ -82,7 +86,7 @@ codestring, encoding = decode(f.read()) f.close() except IOError, msg: - return (1, file, "1", "", "I/O Error: %s" % unicode(msg)) + return (1, file, "1", "", "I/O Error: %s" % unicode(msg), "") if type(codestring) == type(u""): codestring = codestring.encode('utf-8') @@ -103,9 +107,8 @@ return (0, None, None, None, None) template = quixote.ptl_compile.Template(codestring, file) template.compile() - codeobject = template.code else: - codeobject = __builtin__.compile(codestring, file, 'exec') + __builtin__.compile(codestring, file, 'exec') except SyntaxError, detail: lines = traceback.format_exception_only(SyntaxError, detail) match = re.match('\s*File "(.+)", line (\d+)', @@ -126,7 +129,7 @@ line = detail.lineno and detail.lineno or 1 code = "" error = detail.msg - return (1, fn, line, code, error) + return (1, fn, line, code, error, codestring) except ValueError, detail: try: fn = detail.filename @@ -137,31 +140,67 @@ line = 1 error = unicode(detail) code = "" - return (1, fn, line, code, error) + return (1, fn, line, code, error, codestring) except StandardError, detail: try: fn = detail.filename line = detail.lineno code = "" error = detail.msg - return (1, fn, line, code, error) + return (1, fn, line, code, error, codestring) except: # this catchall is intentional pass - return (0, None, None, None, None) + return (0, None, None, None, None, codestring) + +def flakesCheck(fileName, codestring, ignoreStarImportWarnings): + """ + Function to perform a pyflakes check. + + @param fileName name of the file (string) + @param codestring source code to be checked (string) + @param ignoreStarImportWarnings flag indicating to + ignore 'star import' warnings (boolean) + @return list of strings containing the warnings + (marker, file name, line number, message) + """ + strings = [] + lines = codestring.splitlines() + try: + warnings = Checker(codestring, fileName) + warnings.messages.sort(key = lambda a: a.lineno) + for warning in warnings.messages: + if ignoreStarImportWarnings and \ + isinstance(warning, ImportStarUsed): + continue + + _fn, lineno, message = warning.getMessageData() + if not lines[lineno - 1].strip()\ + .endswith("__IGNORE_WARNING__"): + strings.extend(["FLAKES_WARNING", _fn, lineno, message]) + except SyntaxError as err: + if err.text.strip(): + msg = err.text.strip() + else: + msg = err.msg + strings.extend(["FLAKES_ERROR", fileName, err.lineno, msg]) + + return strings if __name__ == "__main__": - if len(sys.argv) != 2: + if len(sys.argv) < 2 or \ + len(sys.argv) > 3 or \ + (len(sys.argv) == 3 and sys.argv[1] not in ["-fi", "-fs"]): print "ERROR" print "" print "" print "" print "No file name given." else: - filename = sys.argv[1] - res, fname, line, code, error = compile(filename) + filename = sys.argv[-1] + syntaxerror, fname, line, code, error, codestring = compile(filename) - if res: + if syntaxerror: print "ERROR" else: print "NO_ERROR" @@ -169,6 +208,12 @@ print line print code print error + + if not syntaxerror and sys.argv[1] in ["-fi", "-fs"]: + # do pyflakes check + warningLines = flakesCheck(filename, codestring, sys.argv[1] == "-fi") + for warningLine in warningLines: + print warningLine sys.exit(0)