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(): 29*d96ef37dSDoug Anderson top_level = gitutil.GetTopLevel() 300d24de9dSSimon Glass try_list = [ 310d24de9dSSimon Glass os.getcwd(), 320d24de9dSSimon Glass os.path.join(os.getcwd(), '..', '..'), 33*d96ef37dSDoug Anderson os.path.join(top_level, 'tools'), 34*d96ef37dSDoug Anderson os.path.join(top_level, 'scripts'), 350d24de9dSSimon Glass '%s/bin' % os.getenv('HOME'), 360d24de9dSSimon Glass ] 370d24de9dSSimon Glass # Look in current dir 380d24de9dSSimon Glass for path in try_list: 390d24de9dSSimon Glass fname = os.path.join(path, 'checkpatch.pl') 400d24de9dSSimon Glass if os.path.isfile(fname): 410d24de9dSSimon Glass return fname 420d24de9dSSimon Glass 430d24de9dSSimon Glass # Look upwwards for a Chrome OS tree 440d24de9dSSimon Glass while not os.path.ismount(path): 450d24de9dSSimon Glass fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files', 460d24de9dSSimon Glass 'scripts', 'checkpatch.pl') 470d24de9dSSimon Glass if os.path.isfile(fname): 480d24de9dSSimon Glass return fname 490d24de9dSSimon Glass path = os.path.dirname(path) 500d24de9dSSimon Glass print 'Could not find checkpatch.pl' 510d24de9dSSimon Glass return None 520d24de9dSSimon Glass 530d24de9dSSimon Glassdef CheckPatch(fname, verbose=False): 540d24de9dSSimon Glass """Run checkpatch.pl on a file. 550d24de9dSSimon Glass 560d24de9dSSimon Glass Returns: 570d24de9dSSimon Glass 4-tuple containing: 580d24de9dSSimon Glass result: False=failure, True=ok 590d24de9dSSimon Glass problems: List of problems, each a dict: 600d24de9dSSimon Glass 'type'; error or warning 610d24de9dSSimon Glass 'msg': text message 620d24de9dSSimon Glass 'file' : filename 630d24de9dSSimon Glass 'line': line number 640d24de9dSSimon Glass lines: Number of lines 650d24de9dSSimon Glass """ 660d24de9dSSimon Glass result = False 670d24de9dSSimon Glass error_count, warning_count, lines = 0, 0, 0 680d24de9dSSimon Glass problems = [] 690d24de9dSSimon Glass chk = FindCheckPatch() 700d24de9dSSimon Glass if not chk: 710d24de9dSSimon Glass raise OSError, ('Cannot find checkpatch.pl - please put it in your ' + 720d24de9dSSimon Glass '~/bin directory') 730d24de9dSSimon Glass item = {} 740d24de9dSSimon Glass stdout = command.Output(chk, '--no-tree', fname) 750d24de9dSSimon Glass #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE) 760d24de9dSSimon Glass #stdout, stderr = pipe.communicate() 770d24de9dSSimon Glass 780d24de9dSSimon Glass # total: 0 errors, 0 warnings, 159 lines checked 790d24de9dSSimon Glass re_stats = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)') 800d24de9dSSimon Glass re_ok = re.compile('.*has no obvious style problems') 810d24de9dSSimon Glass re_bad = re.compile('.*has style problems, please review') 820d24de9dSSimon Glass re_error = re.compile('ERROR: (.*)') 830d24de9dSSimon Glass re_warning = re.compile('WARNING: (.*)') 840d24de9dSSimon Glass re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):') 850d24de9dSSimon Glass 860d24de9dSSimon Glass for line in stdout.splitlines(): 870d24de9dSSimon Glass if verbose: 880d24de9dSSimon Glass print line 890d24de9dSSimon Glass 900d24de9dSSimon Glass # A blank line indicates the end of a message 910d24de9dSSimon Glass if not line and item: 920d24de9dSSimon Glass problems.append(item) 930d24de9dSSimon Glass item = {} 940d24de9dSSimon Glass match = re_stats.match(line) 950d24de9dSSimon Glass if match: 960d24de9dSSimon Glass error_count = int(match.group(1)) 970d24de9dSSimon Glass warning_count = int(match.group(2)) 980d24de9dSSimon Glass lines = int(match.group(3)) 990d24de9dSSimon Glass elif re_ok.match(line): 1000d24de9dSSimon Glass result = True 1010d24de9dSSimon Glass elif re_bad.match(line): 1020d24de9dSSimon Glass result = False 1030d24de9dSSimon Glass match = re_error.match(line) 1040d24de9dSSimon Glass if match: 1050d24de9dSSimon Glass item['msg'] = match.group(1) 1060d24de9dSSimon Glass item['type'] = 'error' 1070d24de9dSSimon Glass match = re_warning.match(line) 1080d24de9dSSimon Glass if match: 1090d24de9dSSimon Glass item['msg'] = match.group(1) 1100d24de9dSSimon Glass item['type'] = 'warning' 1110d24de9dSSimon Glass match = re_file.match(line) 1120d24de9dSSimon Glass if match: 1130d24de9dSSimon Glass item['file'] = match.group(1) 1140d24de9dSSimon Glass item['line'] = int(match.group(2)) 1150d24de9dSSimon Glass 1160d24de9dSSimon Glass return result, problems, error_count, warning_count, lines, stdout 1170d24de9dSSimon Glass 1180d24de9dSSimon Glassdef GetWarningMsg(col, msg_type, fname, line, msg): 1190d24de9dSSimon Glass '''Create a message for a given file/line 1200d24de9dSSimon Glass 1210d24de9dSSimon Glass Args: 1220d24de9dSSimon Glass msg_type: Message type ('error' or 'warning') 1230d24de9dSSimon Glass fname: Filename which reports the problem 1240d24de9dSSimon Glass line: Line number where it was noticed 1250d24de9dSSimon Glass msg: Message to report 1260d24de9dSSimon Glass ''' 1270d24de9dSSimon Glass if msg_type == 'warning': 1280d24de9dSSimon Glass msg_type = col.Color(col.YELLOW, msg_type) 1290d24de9dSSimon Glass elif msg_type == 'error': 1300d24de9dSSimon Glass msg_type = col.Color(col.RED, msg_type) 1310d24de9dSSimon Glass return '%s: %s,%d: %s' % (msg_type, fname, line, msg) 1320d24de9dSSimon Glass 1330d24de9dSSimon Glassdef CheckPatches(verbose, args): 1340d24de9dSSimon Glass '''Run the checkpatch.pl script on each patch''' 1350d24de9dSSimon Glass error_count = 0 1360d24de9dSSimon Glass warning_count = 0 1370d24de9dSSimon Glass col = terminal.Color() 1380d24de9dSSimon Glass 1390d24de9dSSimon Glass for fname in args: 1400d24de9dSSimon Glass ok, problems, errors, warnings, lines, stdout = CheckPatch(fname, 1410d24de9dSSimon Glass verbose) 1420d24de9dSSimon Glass if not ok: 1430d24de9dSSimon Glass error_count += errors 1440d24de9dSSimon Glass warning_count += warnings 1450d24de9dSSimon Glass print '%d errors, %d warnings for %s:' % (errors, 1460d24de9dSSimon Glass warnings, fname) 1470d24de9dSSimon Glass if len(problems) != error_count + warning_count: 1480d24de9dSSimon Glass print "Internal error: some problems lost" 1490d24de9dSSimon Glass for item in problems: 150afb9bf55SSimon Glass print GetWarningMsg(col, item['type'], 151afb9bf55SSimon Glass item.get('file', '<unknown>'), 152afb9bf55SSimon Glass item.get('line', 0), item['msg']) 1530d24de9dSSimon Glass #print stdout 1540d24de9dSSimon Glass if error_count != 0 or warning_count != 0: 1550d24de9dSSimon Glass str = 'checkpatch.pl found %d error(s), %d warning(s)' % ( 1560d24de9dSSimon Glass error_count, warning_count) 1570d24de9dSSimon Glass color = col.GREEN 1580d24de9dSSimon Glass if warning_count: 1590d24de9dSSimon Glass color = col.YELLOW 1600d24de9dSSimon Glass if error_count: 1610d24de9dSSimon Glass color = col.RED 1620d24de9dSSimon Glass print col.Color(color, str) 1630d24de9dSSimon Glass return False 1640d24de9dSSimon Glass return True 165