blob: e08c93cb17011337d0a33a7224a2b7be4d2d52cf [file] [log] [blame]
Misha Brukman231aa292009-01-02 21:15:30 +00001#!/usr/bin/python
2#
3# Common lint functions applicable to multiple types of files.
4
5import re
6
7def VerifyLineLength(filename, lines, max_length):
8 """Checkes to make sure the file has no lines with lines exceeding the length
9 limit.
10
11 Args:
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
15 """
16 line_num = 1
17 for line in lines:
Misha Brukmanc035e902009-02-20 22:28:45 +000018 length = len(line.rstrip('\n'))
Misha Brukman231aa292009-01-02 21:15:30 +000019 if length > max_length:
20 print '%s:%d:Line exceeds %d chars (%d)' % (filename, line_num,
21 max_length, length)
22 line_num += 1
23
24
25def VerifyTrailingWhitespace(filename, lines):
26 """Checkes to make sure the file has no lines with trailing whitespace.
27
28 Args:
29 filename: the file under consideration as string
30 lines: contents of the file as string array
31 """
32 trailing_whitespace_re = re.compile(r'\s+$')
33 line_num = 1
34 for line in lines:
Misha Brukmanc035e902009-02-20 22:28:45 +000035 if trailing_whitespace_re.match(line.rstrip('\n')):
Misha Brukman231aa292009-01-02 21:15:30 +000036 print '%s:%d:Trailing whitespace' % (filename, line_num)
37 line_num += 1
38
39
40class BaseLint:
41 def RunOnFile(filename, lines):
42 raise Exception('RunOnFile() unimplemented')
43
44
45def RunLintOverAllFiles(lint, filenames):
46 """Runs linter over the contents of all files.
47
48 Args:
49 lint: subclass of BaseLint, implementing RunOnFile()
50 filenames: list of all files whose contents will be linted
51 """
52 for filename in filenames:
53 file = open(filename, 'r')
54 if not file:
55 print 'Cound not open %s' % filename
56 continue
57 lines = file.readlines()
58 lint.RunOnFile(filename, lines)