Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. |
| 2 | # |
| 3 | # See file CREDITS for list of people who contributed to this |
| 4 | # project. |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or |
| 7 | # modify it under the terms of the GNU General Public License as |
| 8 | # published by the Free Software Foundation; either version 2 of |
| 9 | # the License, or (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 19 | # MA 02111-1307 USA |
| 20 | # |
| 21 | |
| 22 | import command |
| 23 | import gitutil |
| 24 | import os |
| 25 | import re |
| 26 | import terminal |
| 27 | |
| 28 | def FindCheckPatch(): |
Doug Anderson | d96ef37 | 2012-11-26 15:23:23 +0000 | [diff] [blame^] | 29 | top_level = gitutil.GetTopLevel() |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 30 | try_list = [ |
| 31 | os.getcwd(), |
| 32 | os.path.join(os.getcwd(), '..', '..'), |
Doug Anderson | d96ef37 | 2012-11-26 15:23:23 +0000 | [diff] [blame^] | 33 | os.path.join(top_level, 'tools'), |
| 34 | os.path.join(top_level, 'scripts'), |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 35 | '%s/bin' % os.getenv('HOME'), |
| 36 | ] |
| 37 | # Look in current dir |
| 38 | for path in try_list: |
| 39 | fname = os.path.join(path, 'checkpatch.pl') |
| 40 | if os.path.isfile(fname): |
| 41 | return fname |
| 42 | |
| 43 | # Look upwwards for a Chrome OS tree |
| 44 | while not os.path.ismount(path): |
| 45 | fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files', |
| 46 | 'scripts', 'checkpatch.pl') |
| 47 | if os.path.isfile(fname): |
| 48 | return fname |
| 49 | path = os.path.dirname(path) |
| 50 | print 'Could not find checkpatch.pl' |
| 51 | return None |
| 52 | |
| 53 | def CheckPatch(fname, verbose=False): |
| 54 | """Run checkpatch.pl on a file. |
| 55 | |
| 56 | Returns: |
| 57 | 4-tuple containing: |
| 58 | result: False=failure, True=ok |
| 59 | problems: List of problems, each a dict: |
| 60 | 'type'; error or warning |
| 61 | 'msg': text message |
| 62 | 'file' : filename |
| 63 | 'line': line number |
| 64 | lines: Number of lines |
| 65 | """ |
| 66 | result = False |
| 67 | error_count, warning_count, lines = 0, 0, 0 |
| 68 | problems = [] |
| 69 | chk = FindCheckPatch() |
| 70 | if not chk: |
| 71 | raise OSError, ('Cannot find checkpatch.pl - please put it in your ' + |
| 72 | '~/bin directory') |
| 73 | item = {} |
| 74 | stdout = command.Output(chk, '--no-tree', fname) |
| 75 | #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 76 | #stdout, stderr = pipe.communicate() |
| 77 | |
| 78 | # total: 0 errors, 0 warnings, 159 lines checked |
| 79 | re_stats = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)') |
| 80 | re_ok = re.compile('.*has no obvious style problems') |
| 81 | re_bad = re.compile('.*has style problems, please review') |
| 82 | re_error = re.compile('ERROR: (.*)') |
| 83 | re_warning = re.compile('WARNING: (.*)') |
| 84 | re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):') |
| 85 | |
| 86 | for line in stdout.splitlines(): |
| 87 | if verbose: |
| 88 | print line |
| 89 | |
| 90 | # A blank line indicates the end of a message |
| 91 | if not line and item: |
| 92 | problems.append(item) |
| 93 | item = {} |
| 94 | match = re_stats.match(line) |
| 95 | if match: |
| 96 | error_count = int(match.group(1)) |
| 97 | warning_count = int(match.group(2)) |
| 98 | lines = int(match.group(3)) |
| 99 | elif re_ok.match(line): |
| 100 | result = True |
| 101 | elif re_bad.match(line): |
| 102 | result = False |
| 103 | match = re_error.match(line) |
| 104 | if match: |
| 105 | item['msg'] = match.group(1) |
| 106 | item['type'] = 'error' |
| 107 | match = re_warning.match(line) |
| 108 | if match: |
| 109 | item['msg'] = match.group(1) |
| 110 | item['type'] = 'warning' |
| 111 | match = re_file.match(line) |
| 112 | if match: |
| 113 | item['file'] = match.group(1) |
| 114 | item['line'] = int(match.group(2)) |
| 115 | |
| 116 | return result, problems, error_count, warning_count, lines, stdout |
| 117 | |
| 118 | def GetWarningMsg(col, msg_type, fname, line, msg): |
| 119 | '''Create a message for a given file/line |
| 120 | |
| 121 | Args: |
| 122 | msg_type: Message type ('error' or 'warning') |
| 123 | fname: Filename which reports the problem |
| 124 | line: Line number where it was noticed |
| 125 | msg: Message to report |
| 126 | ''' |
| 127 | if msg_type == 'warning': |
| 128 | msg_type = col.Color(col.YELLOW, msg_type) |
| 129 | elif msg_type == 'error': |
| 130 | msg_type = col.Color(col.RED, msg_type) |
| 131 | return '%s: %s,%d: %s' % (msg_type, fname, line, msg) |
| 132 | |
| 133 | def CheckPatches(verbose, args): |
| 134 | '''Run the checkpatch.pl script on each patch''' |
| 135 | error_count = 0 |
| 136 | warning_count = 0 |
| 137 | col = terminal.Color() |
| 138 | |
| 139 | for fname in args: |
| 140 | ok, problems, errors, warnings, lines, stdout = CheckPatch(fname, |
| 141 | verbose) |
| 142 | if not ok: |
| 143 | error_count += errors |
| 144 | warning_count += warnings |
| 145 | print '%d errors, %d warnings for %s:' % (errors, |
| 146 | warnings, fname) |
| 147 | if len(problems) != error_count + warning_count: |
| 148 | print "Internal error: some problems lost" |
| 149 | for item in problems: |
Simon Glass | afb9bf5 | 2012-09-27 15:33:46 +0000 | [diff] [blame] | 150 | print GetWarningMsg(col, item['type'], |
| 151 | item.get('file', '<unknown>'), |
| 152 | item.get('line', 0), item['msg']) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 153 | #print stdout |
| 154 | if error_count != 0 or warning_count != 0: |
| 155 | str = 'checkpatch.pl found %d error(s), %d warning(s)' % ( |
| 156 | error_count, warning_count) |
| 157 | color = col.GREEN |
| 158 | if warning_count: |
| 159 | color = col.YELLOW |
| 160 | if error_count: |
| 161 | color = col.RED |
| 162 | print col.Color(color, str) |
| 163 | return False |
| 164 | return True |