]>
git.ipfire.org Git - people/ms/u-boot.git/blob - tools/patman/checkpatch.py
1 # Copyright (c) 2011 The Chromium OS Authors.
3 # See file CREDITS for list of people who contributed to this
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.
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.
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,
30 top_level
= gitutil
.GetTopLevel()
33 os
.path
.join(os
.getcwd(), '..', '..'),
34 os
.path
.join(top_level
, 'tools'),
35 os
.path
.join(top_level
, 'scripts'),
36 '%s/bin' % os
.getenv('HOME'),
40 fname
= os
.path
.join(path
, 'checkpatch.pl')
41 if os
.path
.isfile(fname
):
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
):
50 path
= os
.path
.dirname(path
)
52 print >> sys
.stderr
, ('Cannot find checkpatch.pl - please put it in your ' +
53 '~/bin directory or use --no-check')
56 def CheckPatch(fname
, verbose
=False):
57 """Run checkpatch.pl on a file.
61 result: False=failure, True=ok
62 problems: List of problems, each a dict:
63 'type'; error or warning
67 lines: Number of lines
70 error_count
, warning_count
, lines
= 0, 0, 0
72 chk
= FindCheckPatch()
74 stdout
= command
.Output(chk
, '--no-tree', fname
)
75 #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
76 #stdout, stderr = pipe.communicate()
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+):')
86 for line
in stdout
.splitlines():
90 # A blank line indicates the end of a message
94 match
= re_stats
.match(line
)
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
):
101 elif re_bad
.match(line
):
103 match
= re_error
.match(line
)
105 item
['msg'] = match
.group(1)
106 item
['type'] = 'error'
107 match
= re_warning
.match(line
)
109 item
['msg'] = match
.group(1)
110 item
['type'] = 'warning'
111 match
= re_file
.match(line
)
113 item
['file'] = match
.group(1)
114 item
['line'] = int(match
.group(2))
116 return result
, problems
, error_count
, warning_count
, lines
, stdout
118 def GetWarningMsg(col
, msg_type
, fname
, line
, msg
):
119 '''Create a message for a given file/line
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
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
)
133 def CheckPatches(verbose
, args
):
134 '''Run the checkpatch.pl script on each patch'''
137 col
= terminal
.Color()
140 ok
, problems
, errors
, warnings
, lines
, stdout
= CheckPatch(fname
,
143 error_count
+= errors
144 warning_count
+= warnings
145 print '%d errors, %d warnings for %s:' % (errors
,
147 if len(problems
) != error_count
+ warning_count
:
148 print "Internal error: some problems lost"
149 for item
in problems
:
150 print GetWarningMsg(col
, item
['type'],
151 item
.get('file', '<unknown>'),
152 item
.get('line', 0), item
['msg'])
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
)
162 print col
.Color(color
, str)