]>
Commit | Line | Data |
---|---|---|
39236c6e A |
1 | #!/usr/bin/env python |
2 | ||
3 | helpdoc = """ | |
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 | |
8 | Usage: | |
9 | python syntax_checker.py <python_source_file> [<python_source_file> ..] | |
10 | """ | |
11 | import py_compile | |
12 | import sys | |
13 | import os | |
14 | import re | |
15 | ||
16 | tabs_search_rex = re.compile("^\s*\t+",re.MULTILINE|re.DOTALL) | |
17 | ||
18 | if __name__ == "__main__": | |
19 | if len(sys.argv) < 2: | |
fe8ab488 | 20 | print >>sys.stderr, "Error: Unknown arguments" |
39236c6e A |
21 | print helpdoc |
22 | sys.exit(1) | |
23 | for fname in sys.argv[1:]: | |
24 | if not os.path.exists(fname): | |
fe8ab488 | 25 | print >>sys.stderr, "Error: Cannot recognize %s as a file" % fname |
39236c6e A |
26 | sys.exit(1) |
27 | if fname.split('.')[-1] != 'py': | |
28 | print "Note: %s is not a valid python file. Skipping." % fname | |
29 | continue | |
30 | fh = open(fname) | |
31 | strdata = fh.readlines() | |
32 | lineno = 0 | |
33 | tab_check_status = True | |
34 | for linedata in strdata: | |
35 | lineno += 1 | |
36 | if len(tabs_search_rex.findall(linedata)) > 0 : | |
fe8ab488 | 37 | print >>sys.stderr, "Error: Found a TAB character at %s:%d" % (fname, lineno) |
39236c6e A |
38 | tab_check_status = False |
39 | if tab_check_status == False: | |
fe8ab488 | 40 | print >>sys.stderr, "Error: Syntax check failed. Please fix the errors and try again." |
39236c6e A |
41 | sys.exit(1) |
42 | #now check for error in compilation | |
43 | try: | |
44 | compile_result = py_compile.compile(fname, cfile="/dev/null", doraise=True) | |
45 | except py_compile.PyCompileError as exc: | |
39037602 | 46 | print >>sys.stderr, str(exc) |
fe8ab488 | 47 | print >>sys.stderr, "Error: Compilation failed. Please fix the errors and try again." |
39236c6e A |
48 | sys.exit(1) |
49 | print "Success: Checked %s. No syntax errors found." % fname | |
50 | sys.exit(0) | |
51 |