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 22import command 23import gitutil 24import os 25import re 26import terminal 27 28def FindCheckPatch(): 29 top_level = gitutil.GetTopLevel() 30 try_list = [ 31 os.getcwd(), 32 os.path.join(os.getcwd(), '..', '..'), 33 os.path.join(top_level, 'tools'), 34 os.path.join(top_level, 'scripts'), 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 53def 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 118def 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 133def 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: 150 print GetWarningMsg(col, item['type'], 151 item.get('file', '<unknown>'), 152 item.get('line', 0), item['msg']) 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 165