[M120 Migration][NaCl][PPFWK] Change plugin process name
[platform/framework/web/chromium-efl.git] / ppapi / PRESUBMIT.py
1 # Copyright 2012 The Chromium Authors
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import io
6 import os
7 import re
8 import subprocess
9 import sys
10
11
12 # In this file `sys.executable` is used instead of
13 # `input_api.python3_executable` because on Windows
14 # `input_api.python3_executable` is `vpython3.bat` whereas `sys.executable` is
15 # `python.exe`. If `input_api.python3_executable` is used, we need to explicitly
16 # pass `shell=True` to `subprocess.Popen()`, which is a security risk
17 # (https://docs.python.org/3/library/subprocess.html#security-considerations).
18 #
19 # TODO: Investigate the incompatibility of `input_api.python3_executable` on
20 # Windows, for this particular PRESUBMIT script.
21
22 def RunCmdAndCheck(cmd, err_string, output_api, cwd=None, warning=False):
23   results = []
24   p = subprocess.Popen(cmd, cwd=cwd,
25                        stdout=subprocess.PIPE,
26                        stderr=subprocess.PIPE)
27   (_, p_stderr) = p.communicate()
28   if p.returncode:
29     if warning:
30       results.append(output_api.PresubmitPromptWarning(
31         '%s\n\n%s' % (err_string, p_stderr.decode('utf-8'))))
32     else:
33       results.append(
34           output_api.PresubmitError(err_string,
35                                     long_text=p_stderr.decode('utf-8')))
36   return results
37
38
39 def RunUnittests(input_api, output_api):
40   # Run some Generator unittests if the generator source was changed.
41   results = []
42   files = input_api.LocalPaths()
43   generator_files = []
44   for filename in files:
45     name_parts = filename.split(os.sep)
46     if name_parts[0:2] == ['ppapi', 'generators']:
47       generator_files.append(filename)
48   if generator_files != []:
49     cmd = [sys.executable, 'idl_tests.py']
50     ppapi_dir = input_api.PresubmitLocalPath()
51     results.extend(RunCmdAndCheck(cmd,
52                                   'PPAPI IDL unittests failed.',
53                                   output_api,
54                                   os.path.join(ppapi_dir, 'generators')))
55   return results
56
57
58 # Verify that the files do not contain a 'TODO' in them.
59 RE_TODO = re.compile(r'\WTODO\W', flags=re.I)
60 def CheckTODO(input_api, output_api):
61   live_files = input_api.AffectedFiles(include_deletes=False)
62   files = [f.LocalPath() for f in live_files]
63   todo = []
64
65   for filename in files:
66     name, ext = os.path.splitext(filename)
67     name_parts = name.split(os.sep)
68
69     # Only check normal build sources.
70     if ext not in ['.h', '.idl']:
71       continue
72
73     # Only examine the ppapi directory.
74     if name_parts[0] != 'ppapi':
75       continue
76
77     # Only examine public plugin facing directories.
78     if name_parts[1] not in ['api', 'c', 'cpp', 'utility']:
79       continue
80
81     # Only examine public stable interfaces.
82     if name_parts[2] in ['dev', 'private', 'trusted']:
83       continue
84
85     filepath = os.path.join('..', filename)
86     with io.open(filepath, encoding='utf-8') as f:
87       if RE_TODO.search(f.read()):
88         todo.append(filename)
89
90   if todo:
91     return [output_api.PresubmitPromptWarning(
92         'TODOs found in stable public PPAPI files:',
93         long_text='\n'.join(todo))]
94   return []
95
96 # Verify that no CPP wrappers use un-versioned PPB interface name macros.
97 RE_UNVERSIONED_PPB = re.compile(r'\bPPB_\w+_INTERFACE\b')
98 def CheckUnversionedPPB(input_api, output_api):
99   live_files = input_api.AffectedFiles(include_deletes=False)
100   files = [f.LocalPath() for f in live_files]
101   todo = []
102
103   for filename in files:
104     name, ext = os.path.splitext(filename)
105     name_parts = name.split(os.sep)
106
107     # Only check C++ sources.
108     if ext not in ['.cc']:
109       continue
110
111     # Only examine the public plugin facing ppapi/cpp directory.
112     if name_parts[0:2] != ['ppapi', 'cpp']:
113       continue
114
115     # Only examine public stable and trusted interfaces.
116     if name_parts[2] in ['dev', 'private']:
117       continue
118
119     filepath = os.path.join('..', filename)
120     with io.open(filepath, encoding='utf-8') as f:
121       if RE_UNVERSIONED_PPB.search(f.read()):
122         todo.append(filename)
123
124   if todo:
125     return [output_api.PresubmitError(
126         'Unversioned PPB interface references found in PPAPI C++ wrappers:',
127         long_text='\n'.join(todo))]
128   return []
129
130 # Verify that changes to ppapi headers/sources are also made to NaCl SDK.
131 def CheckUpdatedNaClSDK(input_api, output_api):
132   files = input_api.LocalPaths()
133
134   # PPAPI files the Native Client SDK cares about.
135   nacl_sdk_files = []
136
137   for filename in files:
138     name, ext = os.path.splitext(filename)
139     name_parts = name.split(os.sep)
140
141     if len(name_parts) <= 2:
142       continue
143
144     if name_parts[0] != 'ppapi':
145       continue
146
147     if ((name_parts[1] == 'c' and ext == '.h') or
148         (name_parts[1] in ('cpp', 'utility') and ext in ('.h', '.cc'))):
149       if name_parts[2] in ('documentation', 'trusted'):
150         continue
151       nacl_sdk_files.append(filename)
152
153   if not nacl_sdk_files:
154     return []
155
156   verify_ppapi_py = os.path.join(input_api.change.RepositoryRoot(),
157                                  'native_client_sdk', 'src', 'build_tools',
158                                  'verify_ppapi.py')
159   # When running git cl presubmit --all this presubmit may be asked to check
160   # ~300 files, leading to a command line that is ~9,500 characters, which
161   # exceeds the Windows 8191 character cmd.exe limit and causes cryptic failures
162   # with no context. To avoid these we break the command up into smaller pieces.
163   # The error is:
164   #     The command line is too long.
165   files_per_command = 25 if input_api.is_windows else 1000
166   results = []
167   for i in range(0, len(nacl_sdk_files), files_per_command):
168     cmd = [sys.executable, verify_ppapi_py
169            ] + nacl_sdk_files[i:i + files_per_command]
170     results.extend(
171         RunCmdAndCheck(
172             cmd,'PPAPI Interface modified without updating NaCl SDK.\n'
173                 '(note that some dev interfaces should not be added '
174                 'the NaCl SDK; when in doubt, ask a ppapi OWNER.\n'
175                 'To ignore a file, add it to IGNORED_FILES in '
176                 'native_client_sdk/src/build_tools/verify_ppapi.py)',
177                 output_api,
178                 warning=True))
179   return results
180
181 # Verify that changes to ppapi/thunk/interfaces_* files have a corresponding
182 # change to tools/metrics/histograms/enums.xml for UMA tracking.
183 def CheckHistogramXml(input_api, output_api):
184   # We can't use input_api.LocalPaths() here because we need to know about
185   # changes outside of ppapi/. See tools/depot_tools/presubmit_support.py for
186   # details on input_api.
187   files = input_api.change.AffectedFiles()
188
189   INTERFACE_FILES = ('ppapi/thunk/interfaces_legacy.h',
190                      'ppapi/thunk/interfaces_ppb_private.h',
191                      'ppapi/thunk/interfaces_ppb_private_no_permissions.h',
192                      'ppapi/thunk/interfaces_ppb_public_dev_channel.h',
193                      'ppapi/thunk/interfaces_ppb_public_dev.h',
194                      'ppapi/thunk/interfaces_ppb_public_stable.h',
195                      'ppapi/thunk/interfaces_ppb_public_socket.h',
196                      'ppapi/thunk/interfaces_ppb_samsung.h')
197
198   HISTOGRAM_XML_FILE = 'tools/metrics/histograms/enums.xml'
199   interface_changes = []
200   has_histogram_xml_change = False
201   for filename in files:
202     path = filename.LocalPath()
203     if path in INTERFACE_FILES:
204       interface_changes.append(path)
205     if path == HISTOGRAM_XML_FILE:
206       has_histogram_xml_change = True
207
208   if interface_changes and not has_histogram_xml_change:
209     return [output_api.PresubmitNotifyResult(
210         'Missing change to tools/metrics/histograms/enums.xml.\n' +
211         'Run pepper_hash_for_uma to make get values for new interfaces.\n' +
212         'Interface changes:\n' + '\n'.join(interface_changes))]
213   return []
214
215 def CheckChange(input_api, output_api):
216   results = []
217
218   results.extend(RunUnittests(input_api, output_api))
219
220   results.extend(CheckTODO(input_api, output_api))
221
222   results.extend(CheckUnversionedPPB(input_api, output_api))
223
224   results.extend(CheckUpdatedNaClSDK(input_api, output_api))
225
226   results.extend(CheckHistogramXml(input_api, output_api))
227
228   # Verify all modified *.idl have a matching *.h
229   files = input_api.LocalPaths()
230   h_files = []
231   idl_files = []
232   generators_changed = False
233
234   # These are autogenerated by the command buffer generator, they don't go
235   # through idl.
236   whitelist = ['ppb_opengles2', 'ppb_opengles2ext_dev']
237
238   # Find all relevant .h and .idl files.
239   for filename in files:
240     name, ext = os.path.splitext(filename)
241     name_parts = name.split(os.sep)
242     if name_parts[-1] in whitelist:
243       continue
244     if name_parts[0:2] == ['ppapi', 'c'] and ext == '.h':
245       h_files.append('/'.join(name_parts[2:]))
246     elif name_parts[0:2] == ['ppapi', 'api'] and ext == '.idl':
247       idl_files.append('/'.join(name_parts[2:]))
248     elif name_parts[0:2] == ['ppapi', 'generators']:
249       generators_changed = True
250
251   # Generate a list of all appropriate *.h and *.idl changes in this CL.
252   both = h_files + idl_files
253
254   # If there aren't any, we are done checking.
255   if not both: return results
256
257   missing = []
258   for filename in idl_files:
259     if filename not in set(h_files):
260       missing.append('ppapi/api/%s.idl' % filename)
261
262   # An IDL change that includes [generate_thunk] doesn't need to have
263   # an update to the corresponding .h file.
264   new_thunk_files = []
265   for filename in missing:
266     lines = input_api.RightHandSideLines(lambda f: f.LocalPath() == filename)
267     for line in lines:
268       if line[2].strip() == '[generate_thunk]':
269         new_thunk_files.append(filename)
270   for filename in new_thunk_files:
271     missing.remove(filename)
272
273   if missing:
274     results.append(
275         output_api.PresubmitPromptWarning(
276             'Missing PPAPI header, no change or skipped generation?',
277             long_text='\n  '.join(missing)))
278
279   missing_dev = []
280   missing_stable = []
281   missing_priv = []
282   for filename in h_files:
283     if filename not in set(idl_files):
284       name_parts = filename.split(os.sep)
285
286       if name_parts[-1] == 'pp_macros':
287         # The C header generator adds a PPAPI_RELEASE macro based on all the
288         # IDL files, so pp_macros.h may change while its IDL does not.
289         lines = input_api.RightHandSideLines(
290             lambda f: f.LocalPath() == 'ppapi/c/%s.h' % filename)
291         releaseChanged = False
292         for line in lines:
293           if line[2].split()[:2] == ['#define', 'PPAPI_RELEASE']:
294             results.append(
295                 output_api.PresubmitPromptOrNotify(
296                     'PPAPI_RELEASE has changed', long_text=line[2]))
297             releaseChanged = True
298             break
299         if releaseChanged:
300           continue
301
302       if 'trusted' in name_parts:
303         missing_priv.append('  ppapi/c/%s.h' % filename)
304         continue
305
306       if 'private' in name_parts:
307         missing_priv.append('  ppapi/c/%s.h' % filename)
308         continue
309
310       if 'dev' in name_parts:
311         missing_dev.append('  ppapi/c/%s.h' % filename)
312         continue
313
314       missing_stable.append('  ppapi/c/%s.h' % filename)
315
316   if missing_priv:
317     results.append(
318         output_api.PresubmitPromptWarning(
319             'Missing PPAPI IDL for private interface, please generate IDL:',
320             long_text='\n'.join(missing_priv)))
321
322   if missing_dev:
323     results.append(
324         output_api.PresubmitPromptWarning(
325             'Missing PPAPI IDL for DEV, required before moving to stable:',
326             long_text='\n'.join(missing_dev)))
327
328   if missing_stable:
329     # It might be okay that the header changed without a corresponding IDL
330     # change. E.g., comment indenting may have been changed. Treat this as a
331     # warning.
332     if generators_changed:
333       results.append(
334           output_api.PresubmitPromptWarning(
335               'Missing PPAPI IDL for stable interface (due to change in ' +
336               'generators?):',
337               long_text='\n'.join(missing_stable)))
338     else:
339       results.append(
340           output_api.PresubmitError(
341               'Missing PPAPI IDL for stable interface:',
342               long_text='\n'.join(missing_stable)))
343
344   # Verify all *.h files match *.idl definitions, use:
345   #   --test to prevent output to disk
346   #   --diff to generate a unified diff
347   #   --out to pick which files to examine (only the ones in the CL)
348   ppapi_dir = input_api.PresubmitLocalPath()
349   cmd = [sys.executable, 'generator.py',
350          '--wnone', '--diff', '--test','--cgen', '--range=start,end']
351
352   # Only generate output for IDL files references (as *.h or *.idl) in this CL
353   cmd.append('--out=' + ','.join([name + '.idl' for name in both]))
354   cmd_results = RunCmdAndCheck(cmd,
355                                'PPAPI IDL Diff detected: Run the generator.',
356                                output_api,
357                                os.path.join(ppapi_dir, 'generators'))
358   if cmd_results:
359     results.extend(cmd_results)
360
361   return results
362
363
364 def CheckChangeOnUpload(input_api, output_api):
365   return CheckChange(input_api, output_api)
366
367
368 def CheckChangeOnCommit(input_api, output_api):
369   return CheckChange(input_api, output_api)