[telemetry] Ensure gsutil is a file.
[chromium-blink-merge.git] / ppapi / PRESUBMIT.py
blobc5f8dd35e8a3bedd415b07f58771e0c3bd65083b
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 live_files = input_api.AffectedFiles(include_deletes=False)
47 files = [f.LocalPath() for f in live_files]
48 todo = []
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']:
56 continue
58 # Only examine the ppapi directory.
59 if name_parts[0] != 'ppapi':
60 continue
62 # Only examine public plugin facing directories.
63 if name_parts[1] not in ['api', 'c', 'cpp', 'utility']:
64 continue
66 # Only examine public stable interfaces.
67 if name_parts[2] in ['dev', 'private', 'trusted']:
68 continue
69 if name_parts[2] == 'extensions' and name_parts[3] == 'dev':
70 continue
72 filepath = os.path.join('..', filename)
73 if RE_TODO.search(open(filepath, 'rb').read()):
74 todo.append(filename)
76 if todo:
77 return [output_api.PresubmitError(
78 'TODOs found in stable public PPAPI files:',
79 long_text='\n'.join(todo))]
80 return []
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]
87 todo = []
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']:
95 continue
97 # Only examine the public plugin facing ppapi/cpp directory.
98 if name_parts[0:2] != ['ppapi', 'cpp']:
99 continue
101 # Only examine public stable and trusted interfaces.
102 if name_parts[2] in ['dev', 'private']:
103 continue
105 filepath = os.path.join('..', filename)
106 if RE_UNVERSIONED_PPB.search(open(filepath, 'rb').read()):
107 todo.append(filename)
109 if todo:
110 return [output_api.PresubmitError(
111 'Unversioned PPB interface references found in PPAPI C++ wrappers:',
112 long_text='\n'.join(todo))]
113 return []
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.
120 nacl_sdk_files = []
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:
127 continue
129 if name_parts[0] != 'ppapi':
130 continue
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'):
135 continue
136 nacl_sdk_files.append(filename)
138 if not nacl_sdk_files:
139 return []
141 verify_ppapi_py = os.path.join(input_api.change.RepositoryRoot(),
142 'native_client_sdk', 'src', 'build_tools',
143 'verify_ppapi.py')
144 cmd = [sys.executable, verify_ppapi_py] + nacl_sdk_files
145 return RunCmdAndCheck(cmd,
146 'PPAPI Interface modified without updating NaCl SDK.',
147 output_api)
149 def CheckChange(input_api, output_api):
150 results = []
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()
162 h_files = []
163 idl_files = []
164 generators_changed = False
166 # These are autogenerated by the command buffer generator, they don't go
167 # through idl.
168 whitelist = ['ppb_opengles2', 'ppb_opengles2ext_dev']
170 # Find all relevant .h and .idl files.
171 for filename in files:
172 name, ext = os.path.splitext(filename)
173 name_parts = name.split(os.sep)
174 if name_parts[-1] in whitelist:
175 continue
176 if name_parts[0:2] == ['ppapi', 'c'] and ext == '.h':
177 h_files.append('/'.join(name_parts[2:]))
178 elif name_parts[0:2] == ['ppapi', 'api'] and ext == '.idl':
179 idl_files.append('/'.join(name_parts[2:]))
180 elif name_parts[0:2] == ['ppapi', 'generators']:
181 generators_changed = True
183 # Generate a list of all appropriate *.h and *.idl changes in this CL.
184 both = h_files + idl_files
186 # If there aren't any, we are done checking.
187 if not both: return results
189 missing = []
190 for filename in idl_files:
191 if filename not in set(h_files):
192 missing.append('ppapi/api/%s.idl' % filename)
194 # An IDL change that includes [generate_thunk] doesn't need to have
195 # an update to the corresponding .h file.
196 new_thunk_files = []
197 for filename in missing:
198 lines = input_api.RightHandSideLines(lambda f: f.LocalPath() == filename)
199 for line in lines:
200 if line[2].strip() == '[generate_thunk]':
201 new_thunk_files.append(filename)
202 for filename in new_thunk_files:
203 missing.remove(filename)
205 if missing:
206 results.append(
207 output_api.PresubmitPromptWarning(
208 'Missing PPAPI header, no change or skipped generation?',
209 long_text='\n '.join(missing)))
211 missing_dev = []
212 missing_stable = []
213 missing_priv = []
214 for filename in h_files:
215 if filename not in set(idl_files):
216 name_parts = filename.split(os.sep)
218 if name_parts[-1] == 'pp_macros':
219 # The C header generator adds a PPAPI_RELEASE macro based on all the
220 # IDL files, so pp_macros.h may change while its IDL does not.
221 lines = input_api.RightHandSideLines(
222 lambda f: f.LocalPath() == 'ppapi/c/%s.h' % filename)
223 releaseChanged = False
224 for line in lines:
225 if line[2].split()[:2] == ['#define', 'PPAPI_RELEASE']:
226 results.append(
227 output_api.PresubmitPromptOrNotify(
228 'PPAPI_RELEASE has changed', long_text=line[2]))
229 releaseChanged = True
230 break
231 if releaseChanged:
232 continue
234 if 'trusted' in name_parts:
235 missing_priv.append(' ppapi/c/%s.h' % filename)
236 continue
238 if 'private' in name_parts:
239 missing_priv.append(' ppapi/c/%s.h' % filename)
240 continue
242 if 'dev' in name_parts:
243 missing_dev.append(' ppapi/c/%s.h' % filename)
244 continue
246 missing_stable.append(' ppapi/c/%s.h' % filename)
248 if missing_priv:
249 results.append(
250 output_api.PresubmitPromptWarning(
251 'Missing PPAPI IDL for private interface, please generate IDL:',
252 long_text='\n'.join(missing_priv)))
254 if missing_dev:
255 results.append(
256 output_api.PresubmitPromptWarning(
257 'Missing PPAPI IDL for DEV, required before moving to stable:',
258 long_text='\n'.join(missing_dev)))
260 if missing_stable:
261 # It might be okay that the header changed without a corresponding IDL
262 # change. E.g., comment indenting may have been changed. Treat this as a
263 # warning.
264 if generators_changed:
265 results.append(
266 output_api.PresubmitPromptWarning(
267 'Missing PPAPI IDL for stable interface (due to change in ' +
268 'generators?):',
269 long_text='\n'.join(missing_stable)))
270 else:
271 results.append(
272 output_api.PresubmitError(
273 'Missing PPAPI IDL for stable interface:',
274 long_text='\n'.join(missing_stable)))
276 # Verify all *.h files match *.idl definitions, use:
277 # --test to prevent output to disk
278 # --diff to generate a unified diff
279 # --out to pick which files to examine (only the ones in the CL)
280 ppapi_dir = input_api.PresubmitLocalPath()
281 cmd = [sys.executable, 'generator.py',
282 '--wnone', '--diff', '--test','--cgen', '--range=start,end']
284 # Only generate output for IDL files references (as *.h or *.idl) in this CL
285 cmd.append('--out=' + ','.join([name + '.idl' for name in both]))
286 cmd_results = RunCmdAndCheck(cmd,
287 'PPAPI IDL Diff detected: Run the generator.',
288 output_api,
289 os.path.join(ppapi_dir, 'generators'))
290 if cmd_results:
291 results.extend(cmd_results)
293 return results
296 def CheckChangeOnUpload(input_api, output_api):
297 return CheckChange(input_api, output_api)
300 def CheckChangeOnCommit(input_api, output_api):
301 return CheckChange(input_api, output_api)