1 # Copyright (c) 2011 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.
9 def RunCmdAndCheck(cmd
, ppapi_dir
, err_string
, output_api
):
11 p
= subprocess
.Popen(cmd
, cwd
=os
.path
.join(ppapi_dir
, 'generators'),
12 stdout
=subprocess
.PIPE
,
13 stderr
=subprocess
.PIPE
)
14 (p_stdout
, p_stderr
) = p
.communicate()
17 output_api
.PresubmitError(err_string
,
22 def RunUnittests(input_api
, output_api
):
23 # Run some Generator unittests if the generator source was changed.
25 files
= input_api
.LocalPaths()
27 for filename
in files
:
28 name_parts
= filename
.split(os
.sep
)
29 if name_parts
[0:2] == ['ppapi', 'generators']:
30 generator_files
.append(filename
)
31 if generator_files
!= []:
32 cmd
= [ sys
.executable
, 'idl_gen_pnacl.py', '--wnone', '--test']
33 ppapi_dir
= input_api
.PresubmitLocalPath()
34 results
.extend(RunCmdAndCheck(cmd
,
36 'PPAPI IDL Pnacl unittest failed.',
41 def CheckChange(input_api
, output_api
):
44 results
.extend(RunUnittests(input_api
, output_api
))
46 # Verify all modified *.idl have a matching *.h
47 files
= input_api
.LocalPaths()
51 for filename
in files
:
52 name
, ext
= os
.path
.splitext(filename
)
53 name_parts
= name
.split(os
.sep
)
54 if name_parts
[0:2] == ['ppapi', 'c'] and ext
== '.h':
55 h_files
.append('/'.join(name_parts
[2:]))
56 if name_parts
[0:2] == ['ppapi', 'api'] and ext
== '.idl':
57 idl_files
.append('/'.join(name_parts
[2:]))
59 # Generate a list of all appropriate *.h and *.idl changes in this CL.
60 both
= h_files
+ idl_files
62 # If there aren't any, we are done checking.
63 if not both
: return results
66 for filename
in idl_files
:
67 if filename
not in set(h_files
):
68 missing
.append(' ppapi/c/%s.h' % filename
)
69 for filename
in h_files
:
70 if filename
not in set(idl_files
):
71 missing
.append(' ppapi/api/%s.idl' % filename
)
75 output_api
.PresubmitPromptWarning('Missing matching PPAPI definition:',
76 long_text
='\n'.join(missing
)))
78 # Verify all *.h files match *.idl definitions, use:
79 # --test to prevent output to disk
80 # --diff to generate a unified diff
81 # --out to pick which files to examine (only the ones in the CL)
82 ppapi_dir
= input_api
.PresubmitLocalPath()
83 cmd
= [ sys
.executable
, 'generator.py',
84 '--wnone', '--diff', '--test','--cgen', '--range=start,end']
86 # Only generate output for IDL files references (as *.h or *.idl) in this CL
87 cmd
.append('--out=' + ','.join([name
+ '.idl' for name
in both
]))
88 results
.extend(RunCmdAndCheck(cmd
,
90 'PPAPI IDL Diff detected: Run the generator.',
94 def CheckChangeOnUpload(input_api
, output_api
):
96 return CheckChange(input_api
, output_api
)
98 def CheckChangeOnCommit(input_api
, output_api
):
100 return CheckChange(input_api
, output_api
)