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 try_list = [ 30 os.getcwd(), 31 os.path.join(os.getcwd(), '..', '..'), 32 os.path.join(gitutil.GetTopLevel(), 'tools'), 33 '%s/bin' % os.getenv('HOME'), 34 ] 35 # Look in current dir 36 for path in try_list: 37 fname = os.path.join(path, 'checkpatch.pl') 38 if os.path.isfile(fname): 39 return fname 40 41 # Look upwwards for a Chrome OS tree 42 while not os.path.ismount(path): 43 fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files', 44 'scripts', 'checkpatch.pl') 45 if os.path.isfile(fname): 46 return fname 47 path = os.path.dirname(path) 48 print 'Could not find checkpatch.pl' 49 return None 50 51def CheckPatch(fname, verbose=False): 52 """Run checkpatch.pl on a file. 53 54 Returns: 55 4-tuple containing: 56 result: False=failure, True=ok 57 problems: List of problems, each a dict: 58 'type'; error or warning 59 'msg': text message 60 'file' : filename 61 'line': line number 62 lines: Number of lines 63 """ 64 result = False 65 error_count, warning_count, lines = 0, 0, 0 66 problems = [] 67 chk = FindCheckPatch() 68 if not chk: 69 raise OSError, ('Cannot find checkpatch.pl - please put it in your ' + 70 '~/bin directory') 71 item = {} 72 stdout = command.Output(chk, '--no-tree', fname) 73 #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE) 74 #stdout, stderr = pipe.communicate() 75 76 # total: 0 errors, 0 warnings, 159 lines checked 77 re_stats = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)') 78 re_ok = re.compile('.*has no obvious style problems') 79 re_bad = re.compile('.*has style problems, please review') 80 re_error = re.compile('ERROR: (.*)') 81 re_warning = re.compile('WARNING: (.*)') 82 re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):') 83 84 for line in stdout.splitlines(): 85 if verbose: 86 print line 87 88 # A blank line indicates the end of a message 89 if not line and item: 90 problems.append(item) 91 item = {} 92 match = re_stats.match(line) 93 if match: 94 error_count = int(match.group(1)) 95 warning_count = int(match.group(2)) 96 lines = int(match.group(3)) 97 elif re_ok.match(line): 98 result = True 99 elif re_bad.match(line): 100 result = False 101 match = re_error.match(line) 102 if match: 103 item['msg'] = match.group(1) 104 item['type'] = 'error' 105 match = re_warning.match(line) 106 if match: 107 item['msg'] = match.group(1) 108 item['type'] = 'warning' 109 match = re_file.match(line) 110 if match: 111 item['file'] = match.group(1) 112 item['line'] = int(match.group(2)) 113 114 return result, problems, error_count, warning_count, lines, stdout 115 116def GetWarningMsg(col, msg_type, fname, line, msg): 117 '''Create a message for a given file/line 118 119 Args: 120 msg_type: Message type ('error' or 'warning') 121 fname: Filename which reports the problem 122 line: Line number where it was noticed 123 msg: Message to report 124 ''' 125 if msg_type == 'warning': 126 msg_type = col.Color(col.YELLOW, msg_type) 127 elif msg_type == 'error': 128 msg_type = col.Color(col.RED, msg_type) 129 return '%s: %s,%d: %s' % (msg_type, fname, line, msg) 130 131def CheckPatches(verbose, args): 132 '''Run the checkpatch.pl script on each patch''' 133 error_count = 0 134 warning_count = 0 135 col = terminal.Color() 136 137 for fname in args: 138 ok, problems, errors, warnings, lines, stdout = CheckPatch(fname, 139 verbose) 140 if not ok: 141 error_count += errors 142 warning_count += warnings 143 print '%d errors, %d warnings for %s:' % (errors, 144 warnings, fname) 145 if len(problems) != error_count + warning_count: 146 print "Internal error: some problems lost" 147 for item in problems: 148 print GetWarningMsg(col, item['type'], item['file'], 149 item['line'], item['msg']) 150 #print stdout 151 if error_count != 0 or warning_count != 0: 152 str = 'checkpatch.pl found %d error(s), %d warning(s)' % ( 153 error_count, warning_count) 154 color = col.GREEN 155 if warning_count: 156 color = col.YELLOW 157 if error_count: 158 color = col.RED 159 print col.Color(color, str) 160 return False 161 return True 162