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 | |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 22 | import collections |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 23 | import command |
| 24 | import gitutil |
| 25 | import os |
| 26 | import re |
Vadim Bendebury | 99adf6e | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 27 | import sys |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 28 | import terminal |
| 29 | |
| 30 | def FindCheckPatch(): |
Doug Anderson | d96ef37 | 2012-11-26 15:23:23 +0000 | [diff] [blame] | 31 | top_level = gitutil.GetTopLevel() |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 32 | try_list = [ |
| 33 | os.getcwd(), |
| 34 | os.path.join(os.getcwd(), '..', '..'), |
Doug Anderson | d96ef37 | 2012-11-26 15:23:23 +0000 | [diff] [blame] | 35 | os.path.join(top_level, 'tools'), |
| 36 | os.path.join(top_level, 'scripts'), |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 37 | '%s/bin' % os.getenv('HOME'), |
| 38 | ] |
| 39 | # Look in current dir |
| 40 | for path in try_list: |
| 41 | fname = os.path.join(path, 'checkpatch.pl') |
| 42 | if os.path.isfile(fname): |
| 43 | return fname |
| 44 | |
| 45 | # Look upwwards for a Chrome OS tree |
| 46 | while not os.path.ismount(path): |
| 47 | fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files', |
| 48 | 'scripts', 'checkpatch.pl') |
| 49 | if os.path.isfile(fname): |
| 50 | return fname |
| 51 | path = os.path.dirname(path) |
Vadim Bendebury | 99adf6e | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 52 | |
| 53 | print >> sys.stderr, ('Cannot find checkpatch.pl - please put it in your ' + |
| 54 | '~/bin directory or use --no-check') |
| 55 | sys.exit(1) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 56 | |
| 57 | def CheckPatch(fname, verbose=False): |
| 58 | """Run checkpatch.pl on a file. |
| 59 | |
| 60 | Returns: |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 61 | namedtuple containing: |
| 62 | ok: False=failure, True=ok |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 63 | problems: List of problems, each a dict: |
| 64 | 'type'; error or warning |
| 65 | 'msg': text message |
| 66 | 'file' : filename |
| 67 | 'line': line number |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 68 | errors: Number of errors |
| 69 | warnings: Number of warnings |
| 70 | checks: Number of checks |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 71 | lines: Number of lines |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 72 | stdout: Full output of checkpatch |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 73 | """ |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 74 | fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines', |
| 75 | 'stdout'] |
| 76 | result = collections.namedtuple('CheckPatchResult', fields) |
| 77 | result.ok = False |
| 78 | result.errors, result.warning, result.checks = 0, 0, 0 |
| 79 | result.lines = 0 |
| 80 | result.problems = [] |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 81 | chk = FindCheckPatch() |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 82 | item = {} |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 83 | result.stdout = command.Output(chk, '--no-tree', fname) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 84 | #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 85 | #stdout, stderr = pipe.communicate() |
| 86 | |
| 87 | # total: 0 errors, 0 warnings, 159 lines checked |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 88 | # or: |
| 89 | # total: 0 errors, 2 warnings, 7 checks, 473 lines checked |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 90 | re_stats = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)') |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 91 | re_stats_full = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)' |
| 92 | ' checks, (\d+)') |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 93 | re_ok = re.compile('.*has no obvious style problems') |
| 94 | re_bad = re.compile('.*has style problems, please review') |
| 95 | re_error = re.compile('ERROR: (.*)') |
| 96 | re_warning = re.compile('WARNING: (.*)') |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 97 | re_check = re.compile('CHECK: (.*)') |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 98 | re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):') |
| 99 | |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 100 | for line in result.stdout.splitlines(): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 101 | if verbose: |
| 102 | print line |
| 103 | |
| 104 | # A blank line indicates the end of a message |
| 105 | if not line and item: |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 106 | result.problems.append(item) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 107 | item = {} |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 108 | match = re_stats_full.match(line) |
| 109 | if not match: |
| 110 | match = re_stats.match(line) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 111 | if match: |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 112 | result.errors = int(match.group(1)) |
| 113 | result.warnings = int(match.group(2)) |
| 114 | if len(match.groups()) == 4: |
| 115 | result.checks = int(match.group(3)) |
| 116 | result.lines = int(match.group(4)) |
| 117 | else: |
| 118 | result.lines = int(match.group(3)) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 119 | elif re_ok.match(line): |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 120 | result.ok = True |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 121 | elif re_bad.match(line): |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 122 | result.ok = False |
| 123 | err_match = re_error.match(line) |
| 124 | warn_match = re_warning.match(line) |
| 125 | file_match = re_file.match(line) |
| 126 | check_match = re_check.match(line) |
| 127 | if err_match: |
| 128 | item['msg'] = err_match.group(1) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 129 | item['type'] = 'error' |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 130 | elif warn_match: |
| 131 | item['msg'] = warn_match.group(1) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 132 | item['type'] = 'warning' |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 133 | elif check_match: |
| 134 | item['msg'] = check_match.group(1) |
| 135 | item['type'] = 'check' |
| 136 | elif file_match: |
| 137 | item['file'] = file_match.group(1) |
| 138 | item['line'] = int(file_match.group(2)) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 139 | |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 140 | return result |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 141 | |
| 142 | def GetWarningMsg(col, msg_type, fname, line, msg): |
| 143 | '''Create a message for a given file/line |
| 144 | |
| 145 | Args: |
| 146 | msg_type: Message type ('error' or 'warning') |
| 147 | fname: Filename which reports the problem |
| 148 | line: Line number where it was noticed |
| 149 | msg: Message to report |
| 150 | ''' |
| 151 | if msg_type == 'warning': |
| 152 | msg_type = col.Color(col.YELLOW, msg_type) |
| 153 | elif msg_type == 'error': |
| 154 | msg_type = col.Color(col.RED, msg_type) |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 155 | elif msg_type == 'check': |
| 156 | msg_type = col.Color(col.MAGENTA, msg_type) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 157 | return '%s: %s,%d: %s' % (msg_type, fname, line, msg) |
| 158 | |
| 159 | def CheckPatches(verbose, args): |
| 160 | '''Run the checkpatch.pl script on each patch''' |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 161 | error_count, warning_count, check_count = 0, 0, 0 |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 162 | col = terminal.Color() |
| 163 | |
| 164 | for fname in args: |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 165 | result = CheckPatch(fname, verbose) |
| 166 | if not result.ok: |
| 167 | error_count += result.errors |
| 168 | warning_count += result.warnings |
| 169 | check_count += result.checks |
| 170 | print '%d errors, %d warnings, %d checks for %s:' % (result.errors, |
| 171 | result.warnings, result.checks, col.Color(col.BLUE, fname)) |
| 172 | if (len(result.problems) != result.errors + result.warnings + |
| 173 | result.checks): |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 174 | print "Internal error: some problems lost" |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 175 | for item in result.problems: |
| 176 | print GetWarningMsg(col, item.get('type', '<unknown>'), |
Simon Glass | afb9bf5 | 2012-09-27 15:33:46 +0000 | [diff] [blame] | 177 | item.get('file', '<unknown>'), |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 178 | item.get('line', 0), item.get('msg', 'message')) |
| 179 | print |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 180 | #print stdout |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 181 | if error_count or warning_count or check_count: |
| 182 | str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)' |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 183 | color = col.GREEN |
| 184 | if warning_count: |
| 185 | color = col.YELLOW |
| 186 | if error_count: |
| 187 | color = col.RED |
Simon Glass | d29fe6e | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 188 | print col.Color(color, str % (error_count, warning_count, check_count)) |
Simon Glass | 0d24de9 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 189 | return False |
| 190 | return True |