2009-01-03 05:15:30 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Common lint functions applicable to multiple types of files.
|
|
|
|
|
2019-01-03 22:11:33 +08:00
|
|
|
from __future__ import print_function
|
2009-01-03 05:15:30 +08:00
|
|
|
import re
|
|
|
|
|
|
|
|
def VerifyLineLength(filename, lines, max_length):
|
2009-02-21 07:44:54 +08:00
|
|
|
"""Checks to make sure the file has no lines with lines exceeding the length
|
2009-01-03 05:15:30 +08: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-21 07:44:54 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of tuples with format [(filename, line number, msg), ...] with any
|
|
|
|
violations found.
|
2009-01-03 05:15:30 +08:00
|
|
|
"""
|
2009-02-21 07:44:54 +08:00
|
|
|
lint = []
|
2009-01-03 05:15:30 +08:00
|
|
|
line_num = 1
|
|
|
|
for line in lines:
|
2009-02-21 06:28:45 +08:00
|
|
|
length = len(line.rstrip('\n'))
|
2009-01-03 05:15:30 +08:00
|
|
|
if length > max_length:
|
2009-02-21 07:44:54 +08: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-03 05:15:30 +08:00
|
|
|
line_num += 1
|
2009-02-21 07:44:54 +08:00
|
|
|
return lint
|
2009-01-03 05:15:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
def VerifyTrailingWhitespace(filename, lines):
|
2009-02-21 07:44:54 +08:00
|
|
|
"""Checks to make sure the file has no lines with trailing whitespace.
|
2009-01-03 05:15:30 +08:00
|
|
|
|
|
|
|
Args:
|
|
|
|
filename: the file under consideration as string
|
|
|
|
lines: contents of the file as string array
|
2009-02-21 07:44:54 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of tuples with format [(filename, line number, msg), ...] with any
|
|
|
|
violations found.
|
2009-01-03 05:15:30 +08:00
|
|
|
"""
|
2009-02-21 07:44:54 +08:00
|
|
|
lint = []
|
2009-01-03 05:15:30 +08:00
|
|
|
trailing_whitespace_re = re.compile(r'\s+$')
|
|
|
|
line_num = 1
|
|
|
|
for line in lines:
|
2009-02-21 06:28:45 +08:00
|
|
|
if trailing_whitespace_re.match(line.rstrip('\n')):
|
2009-02-21 07:44:54 +08:00
|
|
|
lint.append((filename, line_num, 'Trailing whitespace'))
|
2009-01-03 05:15:30 +08:00
|
|
|
line_num += 1
|
2009-02-21 07:44:54 +08:00
|
|
|
return lint
|
2009-01-03 05:15:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BaseLint:
|
|
|
|
def RunOnFile(filename, lines):
|
|
|
|
raise Exception('RunOnFile() unimplemented')
|
|
|
|
|
|
|
|
|
2009-02-21 07:44:54 +08:00
|
|
|
def RunLintOverAllFiles(linter, filenames):
|
2009-01-03 05:15:30 +08: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-21 07:44:54 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of tuples with format [(filename, line number, msg), ...] with any
|
|
|
|
violations found.
|
2009-01-03 05:15:30 +08:00
|
|
|
"""
|
2009-02-21 07:44:54 +08:00
|
|
|
lint = []
|
2009-01-03 05:15:30 +08:00
|
|
|
for filename in filenames:
|
|
|
|
file = open(filename, 'r')
|
|
|
|
if not file:
|
2019-01-03 22:11:33 +08:00
|
|
|
print('Cound not open %s' % filename)
|
2009-01-03 05:15:30 +08:00
|
|
|
continue
|
|
|
|
lines = file.readlines()
|
2009-02-21 07:44:54 +08:00
|
|
|
lint.extend(linter.RunOnFile(filename, lines))
|
|
|
|
|
|
|
|
return lint
|