Simon Glass | 2613288 | 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 |
Vadim Bendebury | c549f08 | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 26 | import sys |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 27 | import terminal |
| 28 | |
| 29 | def FindCheckPatch(): |
Doug Anderson | 4baede0 | 2012-11-26 15:23:23 +0000 | [diff] [blame] | 30 | top_level = gitutil.GetTopLevel() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 31 | try_list = [ |
| 32 | os.getcwd(), |
| 33 | os.path.join(os.getcwd(), '..', '..'), |
Doug Anderson | 4baede0 | 2012-11-26 15:23:23 +0000 | [diff] [blame] | 34 | os.path.join(top_level, 'tools'), |
| 35 | os.path.join(top_level, 'scripts'), |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 36 | '%s/bin' % os.getenv('HOME'), |
| 37 | ] |
| 38 | # Look in current dir |
| 39 | for path in try_list: |
| 40 | fname = os.path.join(path, 'checkpatch.pl') |
| 41 | if os.path.isfile(fname): |
| 42 | return fname |
| 43 | |
| 44 | # Look upwwards for a Chrome OS tree |
| 45 | while not os.path.ismount(path): |
| 46 | fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files', |
| 47 | 'scripts', 'checkpatch.pl') |
| 48 | if os.path.isfile(fname): |
| 49 | return fname |
| 50 | path = os.path.dirname(path) |
Vadim Bendebury | c549f08 | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 51 | |
| 52 | print >> sys.stderr, ('Cannot find checkpatch.pl - please put it in your ' + |
| 53 | '~/bin directory or use --no-check') |
| 54 | sys.exit(1) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 55 | |
| 56 | def CheckPatch(fname, verbose=False): |
| 57 | """Run checkpatch.pl on a file. |
| 58 | |
| 59 | Returns: |
| 60 | 4-tuple containing: |
| 61 | result: False=failure, True=ok |
| 62 | problems: List of problems, each a dict: |
| 63 | 'type'; error or warning |
| 64 | 'msg': text message |
| 65 | 'file' : filename |
| 66 | 'line': line number |
| 67 | lines: Number of lines |
| 68 | """ |
| 69 | result = False |
| 70 | error_count, warning_count, lines = 0, 0, 0 |
| 71 | problems = [] |
| 72 | chk = FindCheckPatch() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 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 | eb1526c | 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 | 2613288 | 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 |