3 # Common lint functions applicable to multiple types of files.
7 def VerifyLineLength(filename, lines, max_length):
8 """Checks to make sure the file has no lines with lines exceeding the length
12 filename: the file under consideration as string
13 lines: contents of the file as string array
14 max_length: maximum acceptable line length as number
17 A list of tuples with format [(filename, line number, msg), ...] with any
23 length = len(line.rstrip('\n'))
24 if length > max_length:
25 lint.append((filename, line_num,
26 'Line exceeds %d chars (%d)' % (max_length, length)))
30 def VerifyTabs(filename, lines):
31 """Checks to make sure the file has no tab characters.
34 filename: the file under consideration as string
35 lines: contents of the file as string array
38 A list of tuples with format [(line_number, msg), ...] with any violations
42 tab_re = re.compile(r'\t')
45 if tab_re.match(line.rstrip('\n')):
46 lint.append((filename, line_num, 'Tab found instead of whitespace'))
51 def VerifyTrailingWhitespace(filename, lines):
52 """Checks to make sure the file has no lines with trailing whitespace.
55 filename: the file under consideration as string
56 lines: contents of the file as string array
59 A list of tuples with format [(filename, line number, msg), ...] with any
63 trailing_whitespace_re = re.compile(r'\s+$')
66 if trailing_whitespace_re.match(line.rstrip('\n')):
67 lint.append((filename, line_num, 'Trailing whitespace'))
73 def RunOnFile(filename, lines):
74 raise Exception('RunOnFile() unimplemented')
77 def RunLintOverAllFiles(linter, filenames):
78 """Runs linter over the contents of all files.
81 lint: subclass of BaseLint, implementing RunOnFile()
82 filenames: list of all files whose contents will be linted
85 A list of tuples with format [(filename, line number, msg), ...] with any
89 for filename in filenames:
90 file = open(filename, 'r')
92 print 'Cound not open %s' % filename
94 lines = file.readlines()
95 lint.extend(linter.RunOnFile(filename, lines))