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.
11 def RunCmdAndCheck(cmd, err_string, output_api, cwd=None):
13 p = subprocess.Popen(cmd, cwd=cwd,
14 stdout=subprocess.PIPE,
15 stderr=subprocess.PIPE)
16 (p_stdout, p_stderr) = p.communicate()
19 output_api.PresubmitError(err_string,
24 def RunUnittests(input_api, output_api):
25 # Run some Generator unittests if the generator source was changed.
27 files = input_api.LocalPaths()
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.',
39 os.path.join(ppapi_dir, 'generators')))
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 live_files = input_api.AffectedFiles(include_deletes=False)
47 files = [f.LocalPath() for f in live_files]
50 for filename in files:
51 name, ext = os.path.splitext(filename)
52 name_parts = name.split(os.sep)
54 # Only check normal build sources.
55 if ext not in ['.h', '.idl']:
58 # Only examine the ppapi directory.
59 if name_parts[0] != 'ppapi':
62 # Only examine public plugin facing directories.
63 if name_parts[1] not in ['api', 'c', 'cpp', 'utility']:
66 # Only examine public stable interfaces.
67 if name_parts[2] in ['dev', 'private', 'trusted']:
69 if name_parts[2] == 'extensions' and name_parts[3] == 'dev':
72 filepath = os.path.join('..', filename)
73 if RE_TODO.search(open(filepath, 'rb').read()):
77 return [output_api.PresubmitError(
78 'TODOs found in stable public PPAPI files:',
79 long_text='\n'.join(todo))]
82 # Verify that no CPP wrappers use un-versioned PPB interface name macros.
83 RE_UNVERSIONED_PPB = re.compile(r'\bPPB_\w+_INTERFACE\b')
84 def CheckUnversionedPPB(input_api, output_api):
85 live_files = input_api.AffectedFiles(include_deletes=False)
86 files = [f.LocalPath() for f in live_files]
89 for filename in files:
90 name, ext = os.path.splitext(filename)
91 name_parts = name.split(os.sep)
93 # Only check C++ sources.
94 if ext not in ['.cc']:
97 # Only examine the public plugin facing ppapi/cpp directory.
98 if name_parts[0:2] != ['ppapi', 'cpp']:
101 # Only examine public stable and trusted interfaces.
102 if name_parts[2] in ['dev', 'private']:
105 filepath = os.path.join('..', filename)
106 if RE_UNVERSIONED_PPB.search(open(filepath, 'rb').read()):
107 todo.append(filename)
110 return [output_api.PresubmitError(
111 'Unversioned PPB interface references found in PPAPI C++ wrappers:',
112 long_text='\n'.join(todo))]
115 # Verify that changes to ppapi headers/sources are also made to NaCl SDK.
116 def CheckUpdatedNaClSDK(input_api, output_api):
117 files = input_api.LocalPaths()
119 # PPAPI files the Native Client SDK cares about.
122 for filename in files:
123 name, ext = os.path.splitext(filename)
124 name_parts = name.split(os.sep)
126 if len(name_parts) <= 2:
129 if name_parts[0] != 'ppapi':
132 if ((name_parts[1] == 'c' and ext == '.h') or
133 (name_parts[1] in ('cpp', 'utility') and ext in ('.h', '.cc'))):
134 if name_parts[2] in ('documentation', 'trusted'):
136 nacl_sdk_files.append(filename)
138 if not nacl_sdk_files:
141 verify_ppapi_py = os.path.join(input_api.change.RepositoryRoot(),
142 'native_client_sdk', 'src', 'build_tools',
144 cmd = [sys.executable, verify_ppapi_py] + nacl_sdk_files
145 return RunCmdAndCheck(cmd,
146 'PPAPI Interface modified without updating NaCl SDK.',
149 def CheckChange(input_api, output_api):
152 results.extend(RunUnittests(input_api, output_api))
154 results.extend(CheckTODO(input_api, output_api))
156 results.extend(CheckUnversionedPPB(input_api, output_api))
158 results.extend(CheckUpdatedNaClSDK(input_api, output_api))
160 # Verify all modified *.idl have a matching *.h
161 files = input_api.LocalPaths()
164 generators_changed = False
166 # Find all relevant .h and .idl files.
167 for filename in files:
168 name, ext = os.path.splitext(filename)
169 name_parts = name.split(os.sep)
170 if name_parts[0:2] == ['ppapi', 'c'] and ext == '.h':
171 h_files.append('/'.join(name_parts[2:]))
172 elif name_parts[0:2] == ['ppapi', 'api'] and ext == '.idl':
173 idl_files.append('/'.join(name_parts[2:]))
174 elif name_parts[0:2] == ['ppapi', 'generators']:
175 generators_changed = True
177 # Generate a list of all appropriate *.h and *.idl changes in this CL.
178 both = h_files + idl_files
180 # If there aren't any, we are done checking.
181 if not both: return results
184 for filename in idl_files:
185 if filename not in set(h_files):
186 missing.append('ppapi/api/%s.idl' % filename)
188 # An IDL change that includes [generate_thunk] doesn't need to have
189 # an update to the corresponding .h file.
191 for filename in missing:
192 lines = input_api.RightHandSideLines(lambda f: f.LocalPath() == filename)
194 if line[2].strip() == '[generate_thunk]':
195 new_thunk_files.append(filename)
196 for filename in new_thunk_files:
197 missing.remove(filename)
201 output_api.PresubmitPromptWarning(
202 'Missing PPAPI header, no change or skipped generation?',
203 long_text='\n '.join(missing)))
208 for filename in h_files:
209 if filename not in set(idl_files):
210 name_parts = filename.split(os.sep)
212 if name_parts[-1] == 'pp_macros':
213 # The C header generator adds a PPAPI_RELEASE macro based on all the
214 # IDL files, so pp_macros.h may change while its IDL does not.
215 lines = input_api.RightHandSideLines(
216 lambda f: f.LocalPath() == 'ppapi/c/%s.h' % filename)
217 releaseChanged = False
219 if line[2].split()[:2] == ['#define', 'PPAPI_RELEASE']:
221 output_api.PresubmitPromptOrNotify(
222 'PPAPI_RELEASE has changed', long_text=line[2]))
223 releaseChanged = True
228 if 'trusted' in name_parts:
229 missing_priv.append(' ppapi/c/%s.h' % filename)
232 if 'private' in name_parts:
233 missing_priv.append(' ppapi/c/%s.h' % filename)
236 if 'dev' in name_parts:
237 missing_dev.append(' ppapi/c/%s.h' % filename)
240 missing_stable.append(' ppapi/c/%s.h' % filename)
244 output_api.PresubmitPromptWarning(
245 'Missing PPAPI IDL for private interface, please generate IDL:',
246 long_text='\n'.join(missing_priv)))
250 output_api.PresubmitPromptWarning(
251 'Missing PPAPI IDL for DEV, required before moving to stable:',
252 long_text='\n'.join(missing_dev)))
255 # It might be okay that the header changed without a corresponding IDL
256 # change. E.g., comment indenting may have been changed. Treat this as a
258 if generators_changed:
260 output_api.PresubmitPromptWarning(
261 'Missing PPAPI IDL for stable interface (due to change in ' +
263 long_text='\n'.join(missing_stable)))
266 output_api.PresubmitError(
267 'Missing PPAPI IDL for stable interface:',
268 long_text='\n'.join(missing_stable)))
270 # Verify all *.h files match *.idl definitions, use:
271 # --test to prevent output to disk
272 # --diff to generate a unified diff
273 # --out to pick which files to examine (only the ones in the CL)
274 ppapi_dir = input_api.PresubmitLocalPath()
275 cmd = [sys.executable, 'generator.py',
276 '--wnone', '--diff', '--test','--cgen', '--range=start,end']
278 # Only generate output for IDL files references (as *.h or *.idl) in this CL
279 cmd.append('--out=' + ','.join([name + '.idl' for name in both]))
280 cmd_results = RunCmdAndCheck(cmd,
281 'PPAPI IDL Diff detected: Run the generator.',
283 os.path.join(ppapi_dir, 'generators'))
285 results.extend(cmd_results)
290 def CheckChangeOnUpload(input_api, output_api):
291 return CheckChange(input_api, output_api)
294 def CheckChangeOnCommit(input_api, output_api):
295 return CheckChange(input_api, output_api)