]>
git.saurik.com Git - apple/xnu.git/blob - tools/lldbmacros/core/syntax_checker.py
4 A simple utility that verifies the syntax for python scripts.
5 The checks it does are :
6 * Check for 'tab' characters in .py files
7 * Compile errors in py sources
9 python syntax_checker.py <python_source_file> [<python_source_file> ..]
16 tabs_search_rex
= re
.compile("^\s*\t+",re
.MULTILINE|re
.DOTALL
)
18 def find_non_ascii(s
):
20 if ord(c
) >= 0x80: return True
23 if __name__
== "__main__":
25 print >>sys
.stderr
, "Error: Unknown arguments"
28 for fname
in sys
.argv
[1:]:
29 if not os
.path
.exists(fname
):
30 print >>sys
.stderr
, "Error: Cannot recognize %s as a file" % fname
32 if fname
.split('.')[-1] != 'py':
33 print "Note: %s is not a valid python file. Skipping." % fname
36 strdata
= fh
.readlines()
39 for linedata
in strdata
:
41 if len(tabs_search_rex
.findall(linedata
)) > 0 :
42 print >>sys
.stderr
, "Error: Found a TAB character at %s:%d" % (fname
, lineno
)
44 if find_non_ascii(linedata
):
45 print >>sys
.stderr
, "Error: Found a non ascii character at %s:%d" % (fname
, lineno
)
48 print >>sys
.stderr
, "Error: Syntax check failed. Please fix the errors and try again."
50 #now check for error in compilation
52 compile_result
= py_compile
.compile(fname
, cfile
="/dev/null", doraise
=True)
53 except py_compile
.PyCompileError
as exc
:
54 print >>sys
.stderr
, str(exc
)
55 print >>sys
.stderr
, "Error: Compilation failed. Please fix the errors and try again."
57 print "Success: Checked %s. No syntax errors found." % fname