2012-01-14 15:12:45 +00:00
|
|
|
# Copyright (c) 2011 The Chromium OS Authors.
|
|
|
|
#
|
2013-07-08 07:37:19 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2012-01-14 15:12:45 +00:00
|
|
|
#
|
|
|
|
|
2013-03-26 13:09:39 +00:00
|
|
|
import collections
|
2012-01-14 15:12:45 +00:00
|
|
|
import command
|
|
|
|
import gitutil
|
|
|
|
import os
|
|
|
|
import re
|
2013-01-09 16:00:10 +00:00
|
|
|
import sys
|
2012-01-14 15:12:45 +00:00
|
|
|
import terminal
|
|
|
|
|
|
|
|
def FindCheckPatch():
|
2012-11-26 15:23:23 +00:00
|
|
|
top_level = gitutil.GetTopLevel()
|
2012-01-14 15:12:45 +00:00
|
|
|
try_list = [
|
|
|
|
os.getcwd(),
|
|
|
|
os.path.join(os.getcwd(), '..', '..'),
|
2012-11-26 15:23:23 +00:00
|
|
|
os.path.join(top_level, 'tools'),
|
|
|
|
os.path.join(top_level, 'scripts'),
|
2012-01-14 15:12:45 +00:00
|
|
|
'%s/bin' % os.getenv('HOME'),
|
|
|
|
]
|
|
|
|
# Look in current dir
|
|
|
|
for path in try_list:
|
|
|
|
fname = os.path.join(path, 'checkpatch.pl')
|
|
|
|
if os.path.isfile(fname):
|
|
|
|
return fname
|
|
|
|
|
|
|
|
# Look upwwards for a Chrome OS tree
|
|
|
|
while not os.path.ismount(path):
|
|
|
|
fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files',
|
|
|
|
'scripts', 'checkpatch.pl')
|
|
|
|
if os.path.isfile(fname):
|
|
|
|
return fname
|
|
|
|
path = os.path.dirname(path)
|
2013-01-09 16:00:10 +00:00
|
|
|
|
2014-08-15 15:59:26 +00:00
|
|
|
sys.exit('Cannot find checkpatch.pl - please put it in your ' +
|
|
|
|
'~/bin directory or use --no-check')
|
2012-01-14 15:12:45 +00:00
|
|
|
|
|
|
|
def CheckPatch(fname, verbose=False):
|
|
|
|
"""Run checkpatch.pl on a file.
|
|
|
|
|
|
|
|
Returns:
|
2013-03-26 13:09:39 +00:00
|
|
|
namedtuple containing:
|
|
|
|
ok: False=failure, True=ok
|
2012-01-14 15:12:45 +00:00
|
|
|
problems: List of problems, each a dict:
|
|
|
|
'type'; error or warning
|
|
|
|
'msg': text message
|
|
|
|
'file' : filename
|
|
|
|
'line': line number
|
2013-03-26 13:09:39 +00:00
|
|
|
errors: Number of errors
|
|
|
|
warnings: Number of warnings
|
|
|
|
checks: Number of checks
|
2012-01-14 15:12:45 +00:00
|
|
|
lines: Number of lines
|
2013-03-26 13:09:39 +00:00
|
|
|
stdout: Full output of checkpatch
|
2012-01-14 15:12:45 +00:00
|
|
|
"""
|
2013-03-26 13:09:39 +00:00
|
|
|
fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
|
|
|
|
'stdout']
|
|
|
|
result = collections.namedtuple('CheckPatchResult', fields)
|
|
|
|
result.ok = False
|
|
|
|
result.errors, result.warning, result.checks = 0, 0, 0
|
|
|
|
result.lines = 0
|
|
|
|
result.problems = []
|
2012-01-14 15:12:45 +00:00
|
|
|
chk = FindCheckPatch()
|
|
|
|
item = {}
|
2013-03-26 13:09:39 +00:00
|
|
|
result.stdout = command.Output(chk, '--no-tree', fname)
|
2012-01-14 15:12:45 +00:00
|
|
|
#pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
|
|
|
|
#stdout, stderr = pipe.communicate()
|
|
|
|
|
|
|
|
# total: 0 errors, 0 warnings, 159 lines checked
|
2013-03-26 13:09:39 +00:00
|
|
|
# or:
|
|
|
|
# total: 0 errors, 2 warnings, 7 checks, 473 lines checked
|
2012-01-14 15:12:45 +00:00
|
|
|
re_stats = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)')
|
2013-03-26 13:09:39 +00:00
|
|
|
re_stats_full = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)'
|
|
|
|
' checks, (\d+)')
|
2012-01-14 15:12:45 +00:00
|
|
|
re_ok = re.compile('.*has no obvious style problems')
|
|
|
|
re_bad = re.compile('.*has style problems, please review')
|
|
|
|
re_error = re.compile('ERROR: (.*)')
|
|
|
|
re_warning = re.compile('WARNING: (.*)')
|
2013-03-26 13:09:39 +00:00
|
|
|
re_check = re.compile('CHECK: (.*)')
|
2012-01-14 15:12:45 +00:00
|
|
|
re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):')
|
|
|
|
|
2013-03-26 13:09:39 +00:00
|
|
|
for line in result.stdout.splitlines():
|
2012-01-14 15:12:45 +00:00
|
|
|
if verbose:
|
|
|
|
print line
|
|
|
|
|
|
|
|
# A blank line indicates the end of a message
|
|
|
|
if not line and item:
|
2013-03-26 13:09:39 +00:00
|
|
|
result.problems.append(item)
|
2012-01-14 15:12:45 +00:00
|
|
|
item = {}
|
2013-03-26 13:09:39 +00:00
|
|
|
match = re_stats_full.match(line)
|
|
|
|
if not match:
|
|
|
|
match = re_stats.match(line)
|
2012-01-14 15:12:45 +00:00
|
|
|
if match:
|
2013-03-26 13:09:39 +00:00
|
|
|
result.errors = int(match.group(1))
|
|
|
|
result.warnings = int(match.group(2))
|
|
|
|
if len(match.groups()) == 4:
|
|
|
|
result.checks = int(match.group(3))
|
|
|
|
result.lines = int(match.group(4))
|
|
|
|
else:
|
|
|
|
result.lines = int(match.group(3))
|
2012-01-14 15:12:45 +00:00
|
|
|
elif re_ok.match(line):
|
2013-03-26 13:09:39 +00:00
|
|
|
result.ok = True
|
2012-01-14 15:12:45 +00:00
|
|
|
elif re_bad.match(line):
|
2013-03-26 13:09:39 +00:00
|
|
|
result.ok = False
|
|
|
|
err_match = re_error.match(line)
|
|
|
|
warn_match = re_warning.match(line)
|
|
|
|
file_match = re_file.match(line)
|
|
|
|
check_match = re_check.match(line)
|
|
|
|
if err_match:
|
|
|
|
item['msg'] = err_match.group(1)
|
2012-01-14 15:12:45 +00:00
|
|
|
item['type'] = 'error'
|
2013-03-26 13:09:39 +00:00
|
|
|
elif warn_match:
|
|
|
|
item['msg'] = warn_match.group(1)
|
2012-01-14 15:12:45 +00:00
|
|
|
item['type'] = 'warning'
|
2013-03-26 13:09:39 +00:00
|
|
|
elif check_match:
|
|
|
|
item['msg'] = check_match.group(1)
|
|
|
|
item['type'] = 'check'
|
|
|
|
elif file_match:
|
|
|
|
item['file'] = file_match.group(1)
|
|
|
|
item['line'] = int(file_match.group(2))
|
2012-01-14 15:12:45 +00:00
|
|
|
|
2013-03-26 13:09:39 +00:00
|
|
|
return result
|
2012-01-14 15:12:45 +00:00
|
|
|
|
|
|
|
def GetWarningMsg(col, msg_type, fname, line, msg):
|
|
|
|
'''Create a message for a given file/line
|
|
|
|
|
|
|
|
Args:
|
|
|
|
msg_type: Message type ('error' or 'warning')
|
|
|
|
fname: Filename which reports the problem
|
|
|
|
line: Line number where it was noticed
|
|
|
|
msg: Message to report
|
|
|
|
'''
|
|
|
|
if msg_type == 'warning':
|
|
|
|
msg_type = col.Color(col.YELLOW, msg_type)
|
|
|
|
elif msg_type == 'error':
|
|
|
|
msg_type = col.Color(col.RED, msg_type)
|
2013-03-26 13:09:39 +00:00
|
|
|
elif msg_type == 'check':
|
|
|
|
msg_type = col.Color(col.MAGENTA, msg_type)
|
2012-01-14 15:12:45 +00:00
|
|
|
return '%s: %s,%d: %s' % (msg_type, fname, line, msg)
|
|
|
|
|
|
|
|
def CheckPatches(verbose, args):
|
|
|
|
'''Run the checkpatch.pl script on each patch'''
|
2013-03-26 13:09:39 +00:00
|
|
|
error_count, warning_count, check_count = 0, 0, 0
|
2012-01-14 15:12:45 +00:00
|
|
|
col = terminal.Color()
|
|
|
|
|
|
|
|
for fname in args:
|
2013-03-26 13:09:39 +00:00
|
|
|
result = CheckPatch(fname, verbose)
|
|
|
|
if not result.ok:
|
|
|
|
error_count += result.errors
|
|
|
|
warning_count += result.warnings
|
|
|
|
check_count += result.checks
|
|
|
|
print '%d errors, %d warnings, %d checks for %s:' % (result.errors,
|
|
|
|
result.warnings, result.checks, col.Color(col.BLUE, fname))
|
|
|
|
if (len(result.problems) != result.errors + result.warnings +
|
|
|
|
result.checks):
|
2012-01-14 15:12:45 +00:00
|
|
|
print "Internal error: some problems lost"
|
2013-03-26 13:09:39 +00:00
|
|
|
for item in result.problems:
|
|
|
|
print GetWarningMsg(col, item.get('type', '<unknown>'),
|
2012-09-27 15:33:46 +00:00
|
|
|
item.get('file', '<unknown>'),
|
2013-03-26 13:09:39 +00:00
|
|
|
item.get('line', 0), item.get('msg', 'message'))
|
|
|
|
print
|
2012-01-14 15:12:45 +00:00
|
|
|
#print stdout
|
2013-03-26 13:09:39 +00:00
|
|
|
if error_count or warning_count or check_count:
|
|
|
|
str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
|
2012-01-14 15:12:45 +00:00
|
|
|
color = col.GREEN
|
|
|
|
if warning_count:
|
|
|
|
color = col.YELLOW
|
|
|
|
if error_count:
|
|
|
|
color = col.RED
|
2013-03-26 13:09:39 +00:00
|
|
|
print col.Color(color, str % (error_count, warning_count, check_count))
|
2012-01-14 15:12:45 +00:00
|
|
|
return False
|
|
|
|
return True
|