-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_asserts.py
More file actions
49 lines (42 loc) · 1.75 KB
/
check_asserts.py
File metadata and controls
49 lines (42 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/python
import compiler
import compiler.ast
import optparse
import sys
class AssertChecker(object):
def __init__(self, quiet=False):
self.quiet = quiet
self.errors = 0
self.current_filename = ""
def check_files(self, files):
for file in files:
self.check_file(file)
def check_file(self, filename):
self.current_filename = filename
try:
ast = compiler.parseFile(filename)
except SyntaxError:
print >>sys.stderr, "SyntaxError on file %s" % filename
return
compiler.walk(ast, self)
def visitAssert(self, node):
if isinstance(node.test, compiler.ast.Tuple):
if not self.quiet:
print >>sys.stderr, "%s:%d: assert called with a tuple" % (self.current_filename, node.lineno)
self.errors += 1
def main():
parser = optparse.OptionParser(usage="%prog [options] file [files]", description="Checks asserts "
"in python source files to ensure that they are not asserting "
"tuples. Exits with 1 if there are any errors, as well as "
"printing (unless -q is specified). Exits with 0 if there are "
"no errors or if the source file could not be parsed.")
parser.add_option("-q", "--quiet", dest="quiet", action="store_true", default=False,
help="Do not print anything (exit code determines failure)")
(opts, files) = parser.parse_args()
if len(files) == 0:
parser.error("No filenames provided")
checker = AssertChecker(opts.quiet)
checker.check_files(files)
return 1 if checker.errors else 0
if __name__ == '__main__':
sys.exit(main())