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