1#!/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: 18 length = len(line.rstrip()) 19 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: 35 if trailing_whitespace_re.match(line.rstrip()): 36 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) 59