Revert 208429 "Disable AutofillQueryXmlParserTest.ParseAutofillF..."
[chromium-blink-merge.git] / ppapi / PRESUBMIT.py
blob72672e9d6615f34dbd73db3c48aecef966fe9a21
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import os
6 import re
7 import sys
8 import subprocess
11 def RunCmdAndCheck(cmd, err_string, output_api, cwd=None):
12 results = []
13 p = subprocess.Popen(cmd, cwd=cwd,
14 stdout=subprocess.PIPE,
15 stderr=subprocess.PIPE)
16 (p_stdout, p_stderr) = p.communicate()
17 if p.returncode:
18 results.append(
19 output_api.PresubmitError(err_string,
20 long_text=p_stderr))
21 return results
24 def RunUnittests(input_api, output_api):
25 # Run some Generator unittests if the generator source was changed.
26 results = []
27 files = input_api.LocalPaths()
28 generator_files = []
29 for filename in files:
30 name_parts = filename.split(os.sep)
31 if name_parts[0:2] == ['ppapi', 'generators']:
32 generator_files.append(filename)
33 if generator_files != []:
34 cmd = [ sys.executable, 'idl_tests.py']
35 ppapi_dir = input_api.PresubmitLocalPath()
36 results.extend(RunCmdAndCheck(cmd,
37 'PPAPI IDL unittests failed.',
38 output_api,
39 os.path.join(ppapi_dir, 'generators')))
40 return results
43 # Verify that the files do not contain a 'TODO' in them.
44 RE_TODO = re.compile(r'\WTODO\W', flags=re.I)
45 def CheckTODO(input_api, output_api):
46 files = input_api.LocalPaths()
47 todo = []
49 for filename in files:
50 name, ext = os.path.splitext(filename)
51 name_parts = name.split(os.sep)
53 # Only check normal build sources.
54 if ext not in ['.h', '.idl']:
55 continue
57 # Only examine the ppapi directory.
58 if name_parts[0] != 'ppapi':
59 continue
61 # Only examine public plugin facing directories.
62 if name_parts[1] not in ['api', 'c', 'cpp', 'utility']:
63 continue
65 # Only examine public stable interfaces.
66 if name_parts[2] in ['dev', 'private', 'trusted']:
67 continue
68 if name_parts[2] == 'extensions' and name_parts[3] == 'dev':
69 continue
71 filepath = os.path.join('..', filename)
72 if RE_TODO.search(open(filepath, 'rb').read()):
73 todo.append(filename)
75 if todo:
76 return [output_api.PresubmitError(
77 'TODOs found in stable public PPAPI files:',
78 long_text='\n'.join(todo))]
79 return []
81 # Verify that no CPP wrappers use un-versioned PPB interface name macros.
82 RE_UNVERSIONED_PPB = re.compile(r'\bPPB_\w+_INTERFACE\b')
83 def CheckUnversionedPPB(input_api, output_api):
84 files = input_api.LocalPaths()
85 todo = []
87 for filename in files:
88 name, ext = os.path.splitext(filename)
89 name_parts = name.split(os.sep)
91 # Only check C++ sources.
92 if ext not in ['.cc']:
93 continue
95 # Only examine the public plugin facing ppapi/cpp directory.
96 if name_parts[0:2] != ['ppapi', 'cpp']:
97 continue
99 # Only examine public stable and trusted interfaces.
100 if name_parts[2] in ['dev', 'private']:
101 continue
103 filepath = os.path.join('..', filename)
104 if RE_UNVERSIONED_PPB.search(open(filepath, 'rb').read()):
105 todo.append(filename)
107 if todo:
108 return [output_api.PresubmitError(
109 'Unversioned PPB interface references found in PPAPI C++ wrappers:',
110 long_text='\n'.join(todo))]
111 return []
113 def CheckChange(input_api, output_api):
114 results = []
116 results.extend(RunUnittests(input_api, output_api))
118 results.extend(CheckTODO(input_api, output_api))
120 results.extend(CheckUnversionedPPB(input_api, output_api))
122 # Verify all modified *.idl have a matching *.h
123 files = input_api.LocalPaths()
124 h_files = []
125 idl_files = []
127 # Find all relevant .h and .idl files.
128 for filename in files:
129 name, ext = os.path.splitext(filename)
130 name_parts = name.split(os.sep)
131 if name_parts[0:2] == ['ppapi', 'c'] and ext == '.h':
132 h_files.append('/'.join(name_parts[2:]))
133 if name_parts[0:2] == ['ppapi', 'api'] and ext == '.idl':
134 idl_files.append('/'.join(name_parts[2:]))
136 # Generate a list of all appropriate *.h and *.idl changes in this CL.
137 both = h_files + idl_files
139 # If there aren't any, we are done checking.
140 if not both: return results
142 missing = []
143 for filename in idl_files:
144 if filename not in set(h_files):
145 missing.append('ppapi/api/%s.idl' % filename)
147 # An IDL change that includes [generate_thunk] doesn't need to have
148 # an update to the corresponding .h file.
149 new_thunk_files = []
150 for filename in missing:
151 lines = input_api.RightHandSideLines(lambda f: f.LocalPath() == filename)
152 for line in lines:
153 if line[2].strip() == '[generate_thunk]':
154 new_thunk_files.append(filename)
155 for filename in new_thunk_files:
156 missing.remove(filename)
158 if missing:
159 results.append(
160 output_api.PresubmitPromptWarning(
161 'Missing PPAPI header, no change or skipped generation?',
162 long_text='\n '.join(missing)))
164 missing_dev = []
165 missing_stable = []
166 missing_priv = []
167 for filename in h_files:
168 if filename not in set(idl_files):
169 name_parts = filename.split(os.sep)
171 if name_parts[-1] == 'pp_macros':
172 # The C header generator adds a PPAPI_RELEASE macro based on all the
173 # IDL files, so pp_macros.h may change while its IDL does not.
174 lines = input_api.RightHandSideLines(
175 lambda f: f.LocalPath() == 'ppapi/c/%s.h' % filename)
176 releaseChanged = False
177 for line in lines:
178 if line[2].split()[:2] == ['#define', 'PPAPI_RELEASE']:
179 results.append(
180 output_api.PresubmitPromptOrNotify(
181 'PPAPI_RELEASE has changed', long_text=line[2]))
182 releaseChanged = True
183 break
184 if releaseChanged:
185 continue
187 if 'trusted' in name_parts:
188 missing_priv.append(' ppapi/c/%s.h' % filename)
189 continue
191 if 'private' in name_parts:
192 missing_priv.append(' ppapi/c/%s.h' % filename)
193 continue
195 if 'dev' in name_parts:
196 missing_dev.append(' ppapi/c/%s.h' % filename)
197 continue
199 missing_stable.append(' ppapi/c/%s.h' % filename)
201 if missing_priv:
202 results.append(
203 output_api.PresubmitPromptWarning(
204 'Missing PPAPI IDL for private interface, please generate IDL:',
205 long_text='\n'.join(missing_priv)))
207 if missing_dev:
208 results.append(
209 output_api.PresubmitPromptWarning(
210 'Missing PPAPI IDL for DEV, required before moving to stable:',
211 long_text='\n'.join(missing_dev)))
213 if missing_stable:
214 results.append(
215 output_api.PresubmitError(
216 'Missing PPAPI IDL for stable interface:',
217 long_text='\n'.join(missing_stable)))
219 # Verify all *.h files match *.idl definitions, use:
220 # --test to prevent output to disk
221 # --diff to generate a unified diff
222 # --out to pick which files to examine (only the ones in the CL)
223 ppapi_dir = input_api.PresubmitLocalPath()
224 cmd = [sys.executable, 'generator.py',
225 '--wnone', '--diff', '--test','--cgen', '--range=start,end']
227 # Only generate output for IDL files references (as *.h or *.idl) in this CL
228 cmd.append('--out=' + ','.join([name + '.idl' for name in both]))
229 cmd_results = RunCmdAndCheck(cmd,
230 'PPAPI IDL Diff detected: Run the generator.',
231 output_api,
232 os.path.join(ppapi_dir, 'generators'))
233 if cmd_results:
234 results.extend(cmd_results)
236 return results
239 def CheckChangeOnUpload(input_api, output_api):
240 return CheckChange(input_api, output_api)
243 def CheckChangeOnCommit(input_api, output_api):
244 return CheckChange(input_api, output_api)