1 # Copyright (c) 2011 The Chromium OS Authors.
3 # SPDX-License-Identifier: GPL-2.0+
15 top_level = gitutil.GetTopLevel()
18 os.path.join(os.getcwd(), '..', '..'),
19 os.path.join(top_level, 'tools'),
20 os.path.join(top_level, 'scripts'),
21 '%s/bin' % os.getenv('HOME'),
25 fname = os.path.join(path, 'checkpatch.pl')
26 if os.path.isfile(fname):
29 # Look upwwards for a Chrome OS tree
30 while not os.path.ismount(path):
31 fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files',
32 'scripts', 'checkpatch.pl')
33 if os.path.isfile(fname):
35 path = os.path.dirname(path)
37 sys.exit('Cannot find checkpatch.pl - please put it in your ' +
38 '~/bin directory or use --no-check')
40 def CheckPatch(fname, verbose=False):
41 """Run checkpatch.pl on a file.
44 namedtuple containing:
45 ok: False=failure, True=ok
46 problems: List of problems, each a dict:
47 'type'; error or warning
51 errors: Number of errors
52 warnings: Number of warnings
53 checks: Number of checks
54 lines: Number of lines
55 stdout: Full output of checkpatch
57 fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
59 result = collections.namedtuple('CheckPatchResult', fields)
61 result.errors, result.warning, result.checks = 0, 0, 0
64 chk = FindCheckPatch()
66 result.stdout = command.Output(chk, '--no-tree', fname)
67 #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
68 #stdout, stderr = pipe.communicate()
70 # total: 0 errors, 0 warnings, 159 lines checked
72 # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
73 re_stats = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)')
74 re_stats_full = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)'
76 re_ok = re.compile('.*has no obvious style problems')
77 re_bad = re.compile('.*has style problems, please review')
78 re_error = re.compile('ERROR: (.*)')
79 re_warning = re.compile('WARNING: (.*)')
80 re_check = re.compile('CHECK: (.*)')
81 re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):')
83 for line in result.stdout.splitlines():
87 # A blank line indicates the end of a message
89 result.problems.append(item)
91 match = re_stats_full.match(line)
93 match = re_stats.match(line)
95 result.errors = int(match.group(1))
96 result.warnings = int(match.group(2))
97 if len(match.groups()) == 4:
98 result.checks = int(match.group(3))
99 result.lines = int(match.group(4))
101 result.lines = int(match.group(3))
102 elif re_ok.match(line):
104 elif re_bad.match(line):
106 err_match = re_error.match(line)
107 warn_match = re_warning.match(line)
108 file_match = re_file.match(line)
109 check_match = re_check.match(line)
111 item['msg'] = err_match.group(1)
112 item['type'] = 'error'
114 item['msg'] = warn_match.group(1)
115 item['type'] = 'warning'
117 item['msg'] = check_match.group(1)
118 item['type'] = 'check'
120 item['file'] = file_match.group(1)
121 item['line'] = int(file_match.group(2))
125 def GetWarningMsg(col, msg_type, fname, line, msg):
126 '''Create a message for a given file/line
129 msg_type: Message type ('error' or 'warning')
130 fname: Filename which reports the problem
131 line: Line number where it was noticed
132 msg: Message to report
134 if msg_type == 'warning':
135 msg_type = col.Color(col.YELLOW, msg_type)
136 elif msg_type == 'error':
137 msg_type = col.Color(col.RED, msg_type)
138 elif msg_type == 'check':
139 msg_type = col.Color(col.MAGENTA, msg_type)
140 return '%s: %s,%d: %s' % (msg_type, fname, line, msg)
142 def CheckPatches(verbose, args):
143 '''Run the checkpatch.pl script on each patch'''
144 error_count, warning_count, check_count = 0, 0, 0
145 col = terminal.Color()
148 result = CheckPatch(fname, verbose)
150 error_count += result.errors
151 warning_count += result.warnings
152 check_count += result.checks
153 print '%d errors, %d warnings, %d checks for %s:' % (result.errors,
154 result.warnings, result.checks, col.Color(col.BLUE, fname))
155 if (len(result.problems) != result.errors + result.warnings +
157 print "Internal error: some problems lost"
158 for item in result.problems:
159 print GetWarningMsg(col, item.get('type', '<unknown>'),
160 item.get('file', '<unknown>'),
161 item.get('line', 0), item.get('msg', 'message'))
164 if error_count or warning_count or check_count:
165 str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
171 print col.Color(color, str % (error_count, warning_count, check_count))