The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / v8 / tools / presubmit.py @ 40c0f755
History | View | Annotate | Download (6.63 KB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
#
|
3 |
# Copyright 2008 the V8 project authors. All rights reserved.
|
4 |
# Redistribution and use in source and binary forms, with or without
|
5 |
# modification, are permitted provided that the following conditions are
|
6 |
# met:
|
7 |
#
|
8 |
# * Redistributions of source code must retain the above copyright
|
9 |
# notice, this list of conditions and the following disclaimer.
|
10 |
# * Redistributions in binary form must reproduce the above
|
11 |
# copyright notice, this list of conditions and the following
|
12 |
# disclaimer in the documentation and/or other materials provided
|
13 |
# with the distribution.
|
14 |
# * Neither the name of Google Inc. nor the names of its
|
15 |
# contributors may be used to endorse or promote products derived
|
16 |
# from this software without specific prior written permission.
|
17 |
#
|
18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
20 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
21 |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
22 |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
25 |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
26 |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29 |
|
30 |
|
31 |
import optparse |
32 |
import os |
33 |
from os.path import abspath, join, dirname, basename |
34 |
import re |
35 |
import sys |
36 |
import subprocess |
37 |
|
38 |
# Disabled LINT rules and reason.
|
39 |
# build/include_what_you_use: Started giving false positives for variables
|
40 |
# named "string" and "map" assuming that you needed to include STL headers.
|
41 |
|
42 |
ENABLED_LINT_RULES = """
|
43 |
build/class
|
44 |
build/deprecated
|
45 |
build/endif_comment
|
46 |
build/forward_decl
|
47 |
build/include_order
|
48 |
build/printf_format
|
49 |
build/storage_class
|
50 |
legal/copyright
|
51 |
readability/boost
|
52 |
readability/braces
|
53 |
readability/casting
|
54 |
readability/check
|
55 |
readability/constructors
|
56 |
readability/fn_size
|
57 |
readability/function
|
58 |
readability/multiline_comment
|
59 |
readability/multiline_string
|
60 |
readability/streams
|
61 |
readability/todo
|
62 |
readability/utf8
|
63 |
runtime/arrays
|
64 |
runtime/casting
|
65 |
runtime/deprecated_fn
|
66 |
runtime/explicit
|
67 |
runtime/int
|
68 |
runtime/memset
|
69 |
runtime/mutex
|
70 |
runtime/nonconf
|
71 |
runtime/printf
|
72 |
runtime/printf_format
|
73 |
runtime/references
|
74 |
runtime/rtti
|
75 |
runtime/sizeof
|
76 |
runtime/string
|
77 |
runtime/virtual
|
78 |
runtime/vlog
|
79 |
whitespace/blank_line
|
80 |
whitespace/braces
|
81 |
whitespace/comma
|
82 |
whitespace/comments
|
83 |
whitespace/end_of_line
|
84 |
whitespace/ending_newline
|
85 |
whitespace/indent
|
86 |
whitespace/labels
|
87 |
whitespace/line_length
|
88 |
whitespace/newline
|
89 |
whitespace/operators
|
90 |
whitespace/parens
|
91 |
whitespace/tab
|
92 |
whitespace/todo
|
93 |
""".split()
|
94 |
|
95 |
|
96 |
class SourceFileProcessor(object): |
97 |
"""
|
98 |
Utility class that can run through a directory structure, find all relevant
|
99 |
files and invoke a custom check on the files.
|
100 |
"""
|
101 |
|
102 |
def Run(self, path): |
103 |
all_files = [] |
104 |
for file in self.GetPathsToSearch(): |
105 |
all_files += self.FindFilesIn(join(path, file)) |
106 |
if not self.ProcessFiles(all_files): |
107 |
return False |
108 |
return True |
109 |
|
110 |
def IgnoreDir(self, name): |
111 |
return name.startswith('.') or name == 'data' |
112 |
|
113 |
def IgnoreFile(self, name): |
114 |
return name.startswith('.') |
115 |
|
116 |
def FindFilesIn(self, path): |
117 |
result = [] |
118 |
for (root, dirs, files) in os.walk(path): |
119 |
for ignored in [x for x in dirs if self.IgnoreDir(x)]: |
120 |
dirs.remove(ignored) |
121 |
for file in files: |
122 |
if not self.IgnoreFile(file) and self.IsRelevant(file): |
123 |
result.append(join(root, file))
|
124 |
return result
|
125 |
|
126 |
|
127 |
class CppLintProcessor(SourceFileProcessor): |
128 |
"""
|
129 |
Lint files to check that they follow the google code style.
|
130 |
"""
|
131 |
|
132 |
def IsRelevant(self, name): |
133 |
return name.endswith('.cc') or name.endswith('.h') |
134 |
|
135 |
def IgnoreDir(self, name): |
136 |
return (super(CppLintProcessor, self).IgnoreDir(name) |
137 |
or (name == 'third_party')) |
138 |
|
139 |
IGNORE_LINT = ['flag-definitions.h']
|
140 |
|
141 |
def IgnoreFile(self, name): |
142 |
return (super(CppLintProcessor, self).IgnoreFile(name) |
143 |
or (name in CppLintProcessor.IGNORE_LINT)) |
144 |
|
145 |
def GetPathsToSearch(self): |
146 |
return ['src', 'public', 'samples', join('test', 'cctest')] |
147 |
|
148 |
def ProcessFiles(self, files): |
149 |
filt = '-,' + ",".join(['+' + n for n in ENABLED_LINT_RULES]) |
150 |
command = ['cpplint.py', '--filter', filt] + join(files) |
151 |
process = subprocess.Popen(command) |
152 |
return process.wait() == 0 |
153 |
|
154 |
|
155 |
COPYRIGHT_HEADER_PATTERN = re.compile( |
156 |
r'Copyright [\d-]*200[8-9] the V8 project authors. All rights reserved.')
|
157 |
|
158 |
class SourceProcessor(SourceFileProcessor): |
159 |
"""
|
160 |
Check that all files include a copyright notice.
|
161 |
"""
|
162 |
|
163 |
RELEVANT_EXTENSIONS = ['.js', '.cc', '.h', '.py', '.c', 'SConscript', |
164 |
'SConstruct', '.status'] |
165 |
def IsRelevant(self, name): |
166 |
for ext in SourceProcessor.RELEVANT_EXTENSIONS: |
167 |
if name.endswith(ext):
|
168 |
return True |
169 |
return False |
170 |
|
171 |
def GetPathsToSearch(self): |
172 |
return ['.'] |
173 |
|
174 |
def IgnoreDir(self, name): |
175 |
return (super(SourceProcessor, self).IgnoreDir(name) |
176 |
or (name == 'third_party') |
177 |
or (name == 'obj')) |
178 |
|
179 |
IGNORE_COPYRIGHTS = ['earley-boyer.js', 'raytrace.js', 'crypto.js', |
180 |
'libraries.cc', 'libraries-empty.cc', 'jsmin.py', 'regexp-pcre.js'] |
181 |
IGNORE_TABS = IGNORE_COPYRIGHTS + ['unicode-test.js',
|
182 |
'html-comments.js']
|
183 |
|
184 |
def ProcessContents(self, name, contents): |
185 |
result = True
|
186 |
base = basename(name) |
187 |
if not base in SourceProcessor.IGNORE_TABS: |
188 |
if '\t' in contents: |
189 |
print "%s contains tabs" % name |
190 |
result = False
|
191 |
if not base in SourceProcessor.IGNORE_COPYRIGHTS: |
192 |
if not COPYRIGHT_HEADER_PATTERN.search(contents): |
193 |
print "%s is missing a correct copyright header." % name |
194 |
result = False
|
195 |
return result
|
196 |
|
197 |
def ProcessFiles(self, files): |
198 |
success = True
|
199 |
for file in files: |
200 |
try:
|
201 |
handle = open(file) |
202 |
contents = handle.read() |
203 |
success = self.ProcessContents(file, contents) and success |
204 |
finally:
|
205 |
handle.close() |
206 |
return success
|
207 |
|
208 |
|
209 |
def GetOptions(): |
210 |
result = optparse.OptionParser() |
211 |
result.add_option('--no-lint', help="Do not run cpplint", default=False, |
212 |
action="store_true")
|
213 |
return result
|
214 |
|
215 |
|
216 |
def Main(): |
217 |
workspace = abspath(join(dirname(sys.argv[0]), '..')) |
218 |
parser = GetOptions() |
219 |
(options, args) = parser.parse_args() |
220 |
success = True
|
221 |
if not options.no_lint: |
222 |
success = CppLintProcessor().Run(workspace) and success
|
223 |
success = SourceProcessor().Run(workspace) and success
|
224 |
if success:
|
225 |
return 0 |
226 |
else:
|
227 |
return 1 |
228 |
|
229 |
|
230 |
if __name__ == '__main__': |
231 |
sys.exit(Main()) |