10d24de9dSSimon Glass# Copyright (c) 2011 The Chromium OS Authors. 20d24de9dSSimon Glass# 30d24de9dSSimon Glass# See file CREDITS for list of people who contributed to this 40d24de9dSSimon Glass# project. 50d24de9dSSimon Glass# 60d24de9dSSimon Glass# This program is free software; you can redistribute it and/or 70d24de9dSSimon Glass# modify it under the terms of the GNU General Public License as 80d24de9dSSimon Glass# published by the Free Software Foundation; either version 2 of 90d24de9dSSimon Glass# the License, or (at your option) any later version. 100d24de9dSSimon Glass# 110d24de9dSSimon Glass# This program is distributed in the hope that it will be useful, 120d24de9dSSimon Glass# but WITHOUT ANY WARRANTY; without even the implied warranty of 130d24de9dSSimon Glass# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 140d24de9dSSimon Glass# GNU General Public License for more details. 150d24de9dSSimon Glass# 160d24de9dSSimon Glass# You should have received a copy of the GNU General Public License 170d24de9dSSimon Glass# along with this program; if not, write to the Free Software 180d24de9dSSimon Glass# Foundation, Inc., 59 Temple Place, Suite 330, Boston, 190d24de9dSSimon Glass# MA 02111-1307 USA 200d24de9dSSimon Glass# 210d24de9dSSimon Glass 220d24de9dSSimon Glassimport command 230d24de9dSSimon Glassimport gitutil 240d24de9dSSimon Glassimport os 250d24de9dSSimon Glassimport re 260d24de9dSSimon Glassimport terminal 270d24de9dSSimon Glass 280d24de9dSSimon Glassdef FindCheckPatch(): 290d24de9dSSimon Glass try_list = [ 300d24de9dSSimon Glass os.getcwd(), 310d24de9dSSimon Glass os.path.join(os.getcwd(), '..', '..'), 320d24de9dSSimon Glass os.path.join(gitutil.GetTopLevel(), 'tools'), 330d24de9dSSimon Glass '%s/bin' % os.getenv('HOME'), 340d24de9dSSimon Glass ] 350d24de9dSSimon Glass # Look in current dir 360d24de9dSSimon Glass for path in try_list: 370d24de9dSSimon Glass fname = os.path.join(path, 'checkpatch.pl') 380d24de9dSSimon Glass if os.path.isfile(fname): 390d24de9dSSimon Glass return fname 400d24de9dSSimon Glass 410d24de9dSSimon Glass # Look upwwards for a Chrome OS tree 420d24de9dSSimon Glass while not os.path.ismount(path): 430d24de9dSSimon Glass fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files', 440d24de9dSSimon Glass 'scripts', 'checkpatch.pl') 450d24de9dSSimon Glass if os.path.isfile(fname): 460d24de9dSSimon Glass return fname 470d24de9dSSimon Glass path = os.path.dirname(path) 480d24de9dSSimon Glass print 'Could not find checkpatch.pl' 490d24de9dSSimon Glass return None 500d24de9dSSimon Glass 510d24de9dSSimon Glassdef CheckPatch(fname, verbose=False): 520d24de9dSSimon Glass """Run checkpatch.pl on a file. 530d24de9dSSimon Glass 540d24de9dSSimon Glass Returns: 550d24de9dSSimon Glass 4-tuple containing: 560d24de9dSSimon Glass result: False=failure, True=ok 570d24de9dSSimon Glass problems: List of problems, each a dict: 580d24de9dSSimon Glass 'type'; error or warning 590d24de9dSSimon Glass 'msg': text message 600d24de9dSSimon Glass 'file' : filename 610d24de9dSSimon Glass 'line': line number 620d24de9dSSimon Glass lines: Number of lines 630d24de9dSSimon Glass """ 640d24de9dSSimon Glass result = False 650d24de9dSSimon Glass error_count, warning_count, lines = 0, 0, 0 660d24de9dSSimon Glass problems = [] 670d24de9dSSimon Glass chk = FindCheckPatch() 680d24de9dSSimon Glass if not chk: 690d24de9dSSimon Glass raise OSError, ('Cannot find checkpatch.pl - please put it in your ' + 700d24de9dSSimon Glass '~/bin directory') 710d24de9dSSimon Glass item = {} 720d24de9dSSimon Glass stdout = command.Output(chk, '--no-tree', fname) 730d24de9dSSimon Glass #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE) 740d24de9dSSimon Glass #stdout, stderr = pipe.communicate() 750d24de9dSSimon Glass 760d24de9dSSimon Glass # total: 0 errors, 0 warnings, 159 lines checked 770d24de9dSSimon Glass re_stats = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)') 780d24de9dSSimon Glass re_ok = re.compile('.*has no obvious style problems') 790d24de9dSSimon Glass re_bad = re.compile('.*has style problems, please review') 800d24de9dSSimon Glass re_error = re.compile('ERROR: (.*)') 810d24de9dSSimon Glass re_warning = re.compile('WARNING: (.*)') 820d24de9dSSimon Glass re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):') 830d24de9dSSimon Glass 840d24de9dSSimon Glass for line in stdout.splitlines(): 850d24de9dSSimon Glass if verbose: 860d24de9dSSimon Glass print line 870d24de9dSSimon Glass 880d24de9dSSimon Glass # A blank line indicates the end of a message 890d24de9dSSimon Glass if not line and item: 900d24de9dSSimon Glass problems.append(item) 910d24de9dSSimon Glass item = {} 920d24de9dSSimon Glass match = re_stats.match(line) 930d24de9dSSimon Glass if match: 940d24de9dSSimon Glass error_count = int(match.group(1)) 950d24de9dSSimon Glass warning_count = int(match.group(2)) 960d24de9dSSimon Glass lines = int(match.group(3)) 970d24de9dSSimon Glass elif re_ok.match(line): 980d24de9dSSimon Glass result = True 990d24de9dSSimon Glass elif re_bad.match(line): 1000d24de9dSSimon Glass result = False 1010d24de9dSSimon Glass match = re_error.match(line) 1020d24de9dSSimon Glass if match: 1030d24de9dSSimon Glass item['msg'] = match.group(1) 1040d24de9dSSimon Glass item['type'] = 'error' 1050d24de9dSSimon Glass match = re_warning.match(line) 1060d24de9dSSimon Glass if match: 1070d24de9dSSimon Glass item['msg'] = match.group(1) 1080d24de9dSSimon Glass item['type'] = 'warning' 1090d24de9dSSimon Glass match = re_file.match(line) 1100d24de9dSSimon Glass if match: 1110d24de9dSSimon Glass item['file'] = match.group(1) 1120d24de9dSSimon Glass item['line'] = int(match.group(2)) 1130d24de9dSSimon Glass 1140d24de9dSSimon Glass return result, problems, error_count, warning_count, lines, stdout 1150d24de9dSSimon Glass 1160d24de9dSSimon Glassdef GetWarningMsg(col, msg_type, fname, line, msg): 1170d24de9dSSimon Glass '''Create a message for a given file/line 1180d24de9dSSimon Glass 1190d24de9dSSimon Glass Args: 1200d24de9dSSimon Glass msg_type: Message type ('error' or 'warning') 1210d24de9dSSimon Glass fname: Filename which reports the problem 1220d24de9dSSimon Glass line: Line number where it was noticed 1230d24de9dSSimon Glass msg: Message to report 1240d24de9dSSimon Glass ''' 1250d24de9dSSimon Glass if msg_type == 'warning': 1260d24de9dSSimon Glass msg_type = col.Color(col.YELLOW, msg_type) 1270d24de9dSSimon Glass elif msg_type == 'error': 1280d24de9dSSimon Glass msg_type = col.Color(col.RED, msg_type) 1290d24de9dSSimon Glass return '%s: %s,%d: %s' % (msg_type, fname, line, msg) 1300d24de9dSSimon Glass 1310d24de9dSSimon Glassdef CheckPatches(verbose, args): 1320d24de9dSSimon Glass '''Run the checkpatch.pl script on each patch''' 1330d24de9dSSimon Glass error_count = 0 1340d24de9dSSimon Glass warning_count = 0 1350d24de9dSSimon Glass col = terminal.Color() 1360d24de9dSSimon Glass 1370d24de9dSSimon Glass for fname in args: 1380d24de9dSSimon Glass ok, problems, errors, warnings, lines, stdout = CheckPatch(fname, 1390d24de9dSSimon Glass verbose) 1400d24de9dSSimon Glass if not ok: 1410d24de9dSSimon Glass error_count += errors 1420d24de9dSSimon Glass warning_count += warnings 1430d24de9dSSimon Glass print '%d errors, %d warnings for %s:' % (errors, 1440d24de9dSSimon Glass warnings, fname) 1450d24de9dSSimon Glass if len(problems) != error_count + warning_count: 1460d24de9dSSimon Glass print "Internal error: some problems lost" 1470d24de9dSSimon Glass for item in problems: 148*afb9bf55SSimon Glass print GetWarningMsg(col, item['type'], 149*afb9bf55SSimon Glass item.get('file', '<unknown>'), 150*afb9bf55SSimon Glass item.get('line', 0), item['msg']) 1510d24de9dSSimon Glass #print stdout 1520d24de9dSSimon Glass if error_count != 0 or warning_count != 0: 1530d24de9dSSimon Glass str = 'checkpatch.pl found %d error(s), %d warning(s)' % ( 1540d24de9dSSimon Glass error_count, warning_count) 1550d24de9dSSimon Glass color = col.GREEN 1560d24de9dSSimon Glass if warning_count: 1570d24de9dSSimon Glass color = col.YELLOW 1580d24de9dSSimon Glass if error_count: 1590d24de9dSSimon Glass color = col.RED 1600d24de9dSSimon Glass print col.Color(color, str) 1610d24de9dSSimon Glass return False 1620d24de9dSSimon Glass return True 163