2009-01-02 21:15:30 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Common lint functions applicable to multiple types of files.
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
def VerifyLineLength(filename, lines, max_length):
|
2009-02-20 23:44:54 +00:00
|
|
|
"""Checks to make sure the file has no lines with lines exceeding the length
|
2009-01-02 21:15:30 +00:00
|
|
|
limit.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
filename: the file under consideration as string
|
|
|
|
lines: contents of the file as string array
|
|
|
|
max_length: maximum acceptable line length as number
|
2009-02-20 23:44:54 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of tuples with format [(filename, line number, msg), ...] with any
|
|
|
|
violations found.
|
2009-01-02 21:15:30 +00:00
|
|
|
"""
|
2009-02-20 23:44:54 +00:00
|
|
|
lint = []
|
2009-01-02 21:15:30 +00:00
|
|
|
line_num = 1
|
|
|
|
for line in lines:
|
2009-02-20 22:28:45 +00:00
|
|
|
length = len(line.rstrip('\n'))
|
2009-01-02 21:15:30 +00:00
|
|
|
if length > max_length:
|
2009-02-20 23:44:54 +00:00
|
|
|
lint.append((filename, line_num,
|
|
|
|
'Line exceeds %d chars (%d)' % (max_length, length)))
|
|
|
|
line_num += 1
|
|
|
|
return lint
|
|
|
|
|
|
|
|
def VerifyTabs(filename, lines):
|
|
|
|
"""Checks to make sure the file has no tab characters.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
filename: the file under consideration as string
|
|
|
|
lines: contents of the file as string array
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of tuples with format [(line_number, msg), ...] with any violations
|
|
|
|
found.
|
|
|
|
"""
|
|
|
|
lint = []
|
|
|
|
tab_re = re.compile(r'\t')
|
|
|
|
line_num = 1
|
|
|
|
for line in lines:
|
|
|
|
if tab_re.match(line.rstrip('\n')):
|
|
|
|
lint.append((filename, line_num, 'Tab found instead of whitespace'))
|
2009-01-02 21:15:30 +00:00
|
|
|
line_num += 1
|
2009-02-20 23:44:54 +00:00
|
|
|
return lint
|
2009-01-02 21:15:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def VerifyTrailingWhitespace(filename, lines):
|
2009-02-20 23:44:54 +00:00
|
|
|
"""Checks to make sure the file has no lines with trailing whitespace.
|
2009-01-02 21:15:30 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
filename: the file under consideration as string
|
|
|
|
lines: contents of the file as string array
|
2009-02-20 23:44:54 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of tuples with format [(filename, line number, msg), ...] with any
|
|
|
|
violations found.
|
2009-01-02 21:15:30 +00:00
|
|
|
"""
|
2009-02-20 23:44:54 +00:00
|
|
|
lint = []
|
2009-01-02 21:15:30 +00:00
|
|
|
trailing_whitespace_re = re.compile(r'\s+$')
|
|
|
|
line_num = 1
|
|
|
|
for line in lines:
|
2009-02-20 22:28:45 +00:00
|
|
|
if trailing_whitespace_re.match(line.rstrip('\n')):
|
2009-02-20 23:44:54 +00:00
|
|
|
lint.append((filename, line_num, 'Trailing whitespace'))
|
2009-01-02 21:15:30 +00:00
|
|
|
line_num += 1
|
2009-02-20 23:44:54 +00:00
|
|
|
return lint
|
2009-01-02 21:15:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BaseLint:
|
|
|
|
def RunOnFile(filename, lines):
|
|
|
|
raise Exception('RunOnFile() unimplemented')
|
|
|
|
|
|
|
|
|
2009-02-20 23:44:54 +00:00
|
|
|
def RunLintOverAllFiles(linter, filenames):
|
2009-01-02 21:15:30 +00:00
|
|
|
"""Runs linter over the contents of all files.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
lint: subclass of BaseLint, implementing RunOnFile()
|
|
|
|
filenames: list of all files whose contents will be linted
|
2009-02-20 23:44:54 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of tuples with format [(filename, line number, msg), ...] with any
|
|
|
|
violations found.
|
2009-01-02 21:15:30 +00:00
|
|
|
"""
|
2009-02-20 23:44:54 +00:00
|
|
|
lint = []
|
2009-01-02 21:15:30 +00:00
|
|
|
for filename in filenames:
|
|
|
|
file = open(filename, 'r')
|
|
|
|
if not file:
|
|
|
|
print 'Cound not open %s' % filename
|
|
|
|
continue
|
|
|
|
lines = file.readlines()
|
2009-02-20 23:44:54 +00:00
|
|
|
lint.extend(linter.RunOnFile(filename, lines))
|
|
|
|
|
|
|
|
return lint
|