blob: e982680c052ad2669365edf91373d0a69f81688d [file] [log] [blame]
Misha Brukman653222f2009-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):
Misha Brukman92599052009-02-20 23:44:54 +00008 """Checks to make sure the file has no lines with lines exceeding the length
Misha Brukman653222f2009-01-02 21:15:30 +00009 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
Misha Brukman92599052009-02-20 23:44:54 +000015
16 Returns:
17 A list of tuples with format [(filename, line number, msg), ...] with any
18 violations found.
Misha Brukman653222f2009-01-02 21:15:30 +000019 """
Misha Brukman92599052009-02-20 23:44:54 +000020 lint = []
Misha Brukman653222f2009-01-02 21:15:30 +000021 line_num = 1
22 for line in lines:
Misha Brukman4c4c9522009-02-20 22:28:45 +000023 length = len(line.rstrip('\n'))
Misha Brukman653222f2009-01-02 21:15:30 +000024 if length > max_length:
Misha Brukman92599052009-02-20 23:44:54 +000025 lint.append((filename, line_num,
26 'Line exceeds %d chars (%d)' % (max_length, length)))
Misha Brukman653222f2009-01-02 21:15:30 +000027 line_num += 1
Misha Brukman92599052009-02-20 23:44:54 +000028 return lint
Misha Brukman653222f2009-01-02 21:15:30 +000029
Misha Brukman92599052009-02-20 23:44:54 +000030def VerifyTabs(filename, lines):
31 """Checks to make sure the file has no tab characters.
Misha Brukman653222f2009-01-02 21:15:30 +000032
33 Args:
34 filename: the file under consideration as string
35 lines: contents of the file as string array
Misha Brukman92599052009-02-20 23:44:54 +000036
37 Returns:
38 A list of tuples with format [(line_number, msg), ...] with any violations
39 found.
Misha Brukman653222f2009-01-02 21:15:30 +000040 """
Misha Brukman92599052009-02-20 23:44:54 +000041 lint = []
42 tab_re = re.compile(r'\t')
43 line_num = 1
44 for line in lines:
45 if tab_re.match(line.rstrip('\n')):
46 lint.append((filename, line_num, 'Tab found instead of whitespace'))
47 line_num += 1
48 return lint
49
50
51def VerifyTrailingWhitespace(filename, lines):
52 """Checks to make sure the file has no lines with trailing whitespace.
53
54 Args:
55 filename: the file under consideration as string
56 lines: contents of the file as string array
57
58 Returns:
59 A list of tuples with format [(filename, line number, msg), ...] with any
60 violations found.
61 """
62 lint = []
Misha Brukman653222f2009-01-02 21:15:30 +000063 trailing_whitespace_re = re.compile(r'\s+$')
64 line_num = 1
65 for line in lines:
Misha Brukman4c4c9522009-02-20 22:28:45 +000066 if trailing_whitespace_re.match(line.rstrip('\n')):
Misha Brukman92599052009-02-20 23:44:54 +000067 lint.append((filename, line_num, 'Trailing whitespace'))
Misha Brukman653222f2009-01-02 21:15:30 +000068 line_num += 1
Misha Brukman92599052009-02-20 23:44:54 +000069 return lint
Misha Brukman653222f2009-01-02 21:15:30 +000070
71
72class BaseLint:
73 def RunOnFile(filename, lines):
74 raise Exception('RunOnFile() unimplemented')
75
76
Misha Brukman92599052009-02-20 23:44:54 +000077def RunLintOverAllFiles(linter, filenames):
Misha Brukman653222f2009-01-02 21:15:30 +000078 """Runs linter over the contents of all files.
79
80 Args:
81 lint: subclass of BaseLint, implementing RunOnFile()
82 filenames: list of all files whose contents will be linted
Misha Brukman92599052009-02-20 23:44:54 +000083
84 Returns:
85 A list of tuples with format [(filename, line number, msg), ...] with any
86 violations found.
Misha Brukman653222f2009-01-02 21:15:30 +000087 """
Misha Brukman92599052009-02-20 23:44:54 +000088 lint = []
Misha Brukman653222f2009-01-02 21:15:30 +000089 for filename in filenames:
90 file = open(filename, 'r')
91 if not file:
92 print 'Cound not open %s' % filename
93 continue
94 lines = file.readlines()
Misha Brukman92599052009-02-20 23:44:54 +000095 lint.extend(linter.RunOnFile(filename, lines))
96
97 return lint