Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Common lint functions applicable to multiple types of files. |
| 4 | |
| 5 | import re |
| 6 | |
| 7 | def VerifyLineLength(filename, lines, max_length): |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 8 | """Checks to make sure the file has no lines with lines exceeding the length |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 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 |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 15 | |
| 16 | Returns: |
| 17 | A list of tuples with format [(filename, line number, msg), ...] with any |
| 18 | violations found. |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 19 | """ |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 20 | lint = [] |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 21 | line_num = 1 |
| 22 | for line in lines: |
Misha Brukman | 4c4c952 | 2009-02-20 22:28:45 +0000 | [diff] [blame] | 23 | length = len(line.rstrip('\n')) |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 24 | if length > max_length: |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 25 | lint.append((filename, line_num, |
| 26 | 'Line exceeds %d chars (%d)' % (max_length, length))) |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 27 | line_num += 1 |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 28 | return lint |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 29 | |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 30 | def VerifyTabs(filename, lines): |
| 31 | """Checks to make sure the file has no tab characters. |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 32 | |
| 33 | Args: |
| 34 | filename: the file under consideration as string |
| 35 | lines: contents of the file as string array |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 36 | |
| 37 | Returns: |
| 38 | A list of tuples with format [(line_number, msg), ...] with any violations |
| 39 | found. |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 40 | """ |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 41 | 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 | |
| 51 | def 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 Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 63 | trailing_whitespace_re = re.compile(r'\s+$') |
| 64 | line_num = 1 |
| 65 | for line in lines: |
Misha Brukman | 4c4c952 | 2009-02-20 22:28:45 +0000 | [diff] [blame] | 66 | if trailing_whitespace_re.match(line.rstrip('\n')): |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 67 | lint.append((filename, line_num, 'Trailing whitespace')) |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 68 | line_num += 1 |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 69 | return lint |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | class BaseLint: |
| 73 | def RunOnFile(filename, lines): |
| 74 | raise Exception('RunOnFile() unimplemented') |
| 75 | |
| 76 | |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 77 | def RunLintOverAllFiles(linter, filenames): |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 78 | """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 Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 83 | |
| 84 | Returns: |
| 85 | A list of tuples with format [(filename, line number, msg), ...] with any |
| 86 | violations found. |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 87 | """ |
Misha Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 88 | lint = [] |
Misha Brukman | 653222f | 2009-01-02 21:15:30 +0000 | [diff] [blame] | 89 | 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 Brukman | 9259905 | 2009-02-20 23:44:54 +0000 | [diff] [blame] | 95 | lint.extend(linter.RunOnFile(filename, lines)) |
| 96 | |
| 97 | return lint |