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