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