]>
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,
31 os
.path
.join(os
.getcwd(), '..', '..'),
32 os
.path
.join(gitutil
.GetTopLevel(), 'tools'),
33 '%s/bin' % os
.getenv('HOME'),
37 fname
= os
.path
.join(path
, 'checkpatch.pl')
38 if os
.path
.isfile(fname
):
41 # Look upwwards for a Chrome OS tree
42 while not os
.path
.ismount(path
):
43 fname
= os
.path
.join(path
, 'src', 'third_party', 'kernel', 'files',
44 'scripts', 'checkpatch.pl')
45 if os
.path
.isfile(fname
):
47 path
= os
.path
.dirname(path
)
48 print 'Could not find checkpatch.pl'
51 def CheckPatch(fname
, verbose
=False):
52 """Run checkpatch.pl on a file.
56 result: False=failure, True=ok
57 problems: List of problems, each a dict:
58 'type'; error or warning
62 lines: Number of lines
65 error_count
, warning_count
, lines
= 0, 0, 0
67 chk
= FindCheckPatch()
69 raise OSError, ('Cannot find checkpatch.pl - please put it in your ' +
72 stdout
= command
.Output(chk
, '--no-tree', fname
)
73 #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
74 #stdout, stderr = pipe.communicate()
76 # total: 0 errors, 0 warnings, 159 lines checked
77 re_stats
= re
.compile('total: (\\d+) errors, (\d+) warnings, (\d+)')
78 re_ok
= re
.compile('.*has no obvious style problems')
79 re_bad
= re
.compile('.*has style problems, please review')
80 re_error
= re
.compile('ERROR: (.*)')
81 re_warning
= re
.compile('WARNING: (.*)')
82 re_file
= re
.compile('#\d+: FILE: ([^:]*):(\d+):')
84 for line
in stdout
.splitlines():
88 # A blank line indicates the end of a message
92 match
= re_stats
.match(line
)
94 error_count
= int(match
.group(1))
95 warning_count
= int(match
.group(2))
96 lines
= int(match
.group(3))
97 elif re_ok
.match(line
):
99 elif re_bad
.match(line
):
101 match
= re_error
.match(line
)
103 item
['msg'] = match
.group(1)
104 item
['type'] = 'error'
105 match
= re_warning
.match(line
)
107 item
['msg'] = match
.group(1)
108 item
['type'] = 'warning'
109 match
= re_file
.match(line
)
111 item
['file'] = match
.group(1)
112 item
['line'] = int(match
.group(2))
114 return result
, problems
, error_count
, warning_count
, lines
, stdout
116 def GetWarningMsg(col
, msg_type
, fname
, line
, msg
):
117 '''Create a message for a given file/line
120 msg_type: Message type ('error' or 'warning')
121 fname: Filename which reports the problem
122 line: Line number where it was noticed
123 msg: Message to report
125 if msg_type
== 'warning':
126 msg_type
= col
.Color(col
.YELLOW
, msg_type
)
127 elif msg_type
== 'error':
128 msg_type
= col
.Color(col
.RED
, msg_type
)
129 return '%s: %s,%d: %s' % (msg_type
, fname
, line
, msg
)
131 def CheckPatches(verbose
, args
):
132 '''Run the checkpatch.pl script on each patch'''
135 col
= terminal
.Color()
138 ok
, problems
, errors
, warnings
, lines
, stdout
= CheckPatch(fname
,
141 error_count
+= errors
142 warning_count
+= warnings
143 print '%d errors, %d warnings for %s:' % (errors
,
145 if len(problems
) != error_count
+ warning_count
:
146 print "Internal error: some problems lost"
147 for item
in problems
:
148 print GetWarningMsg(col
, item
['type'],
149 item
.get('file', '<unknown>'),
150 item
.get('line', 0), item
['msg'])
152 if error_count
!= 0 or warning_count
!= 0:
153 str = 'checkpatch.pl found %d error(s), %d warning(s)' % (
154 error_count
, warning_count
)
160 print col
.Color(color
, str)