[email protected] | e4f373e | 2012-01-10 23:18:24 | [diff] [blame^] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 3937578 | 2011-09-14 16:55:09 | [diff] [blame] | 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 os |
| 6 | import sys |
| 7 | import subprocess |
| 8 | |
[email protected] | e4f373e | 2012-01-10 23:18:24 | [diff] [blame^] | 9 | def RunCmdAndCheck(cmd, err_string, output_api, cwd=None): |
[email protected] | 2978339a7 | 2011-11-30 17:59:14 | [diff] [blame] | 10 | results = [] |
[email protected] | e4f373e | 2012-01-10 23:18:24 | [diff] [blame^] | 11 | p = subprocess.Popen(cmd, cwd=cwd, |
[email protected] | 2978339a7 | 2011-11-30 17:59:14 | [diff] [blame] | 12 | stdout=subprocess.PIPE, |
| 13 | stderr=subprocess.PIPE) |
| 14 | (p_stdout, p_stderr) = p.communicate() |
| 15 | if p.returncode: |
| 16 | results.append( |
| 17 | output_api.PresubmitError(err_string, |
| 18 | long_text=p_stderr)) |
| 19 | return results |
| 20 | |
| 21 | |
| 22 | def RunUnittests(input_api, output_api): |
| 23 | # Run some Generator unittests if the generator source was changed. |
| 24 | results = [] |
| 25 | files = input_api.LocalPaths() |
| 26 | generator_files = [] |
| 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, |
[email protected] | 2978339a7 | 2011-11-30 17:59:14 | [diff] [blame] | 35 | 'PPAPI IDL Pnacl unittest failed.', |
[email protected] | e4f373e | 2012-01-10 23:18:24 | [diff] [blame^] | 36 | output_api, |
| 37 | os.path.join(ppapi_dir, 'generators'))) |
[email protected] | 2978339a7 | 2011-11-30 17:59:14 | [diff] [blame] | 38 | return results |
| 39 | |
| 40 | |
[email protected] | e4f373e | 2012-01-10 23:18:24 | [diff] [blame^] | 41 | # If any .srpc files were changed, run run_srpcgen.py --diff_mode. |
| 42 | def CheckSrpcChange(input_api, output_api): |
| 43 | if [True for filename in input_api.LocalPaths() if |
| 44 | os.path.splitext(filename)[1] == '.srpc']: |
| 45 | return RunCmdAndCheck([sys.executable, |
| 46 | os.path.join(input_api.PresubmitLocalPath(), |
| 47 | 'native_client', 'src', |
| 48 | 'shared', 'ppapi_proxy', |
| 49 | 'run_srpcgen.py'), |
| 50 | '--diff_mode'], |
| 51 | 'PPAPI SRPC Diff detected: Run run_srpcgen.py.', |
| 52 | output_api) |
| 53 | return [] |
| 54 | |
[email protected] | 3937578 | 2011-09-14 16:55:09 | [diff] [blame] | 55 | def CheckChange(input_api, output_api): |
| 56 | results = [] |
| 57 | |
[email protected] | e4f373e | 2012-01-10 23:18:24 | [diff] [blame^] | 58 | results.extend(CheckSrpcChange(input_api, output_api)) |
| 59 | |
[email protected] | 2978339a7 | 2011-11-30 17:59:14 | [diff] [blame] | 60 | results.extend(RunUnittests(input_api, output_api)) |
| 61 | |
[email protected] | 3937578 | 2011-09-14 16:55:09 | [diff] [blame] | 62 | # Verify all modified *.idl have a matching *.h |
| 63 | files = input_api.LocalPaths() |
| 64 | h_files = [] |
| 65 | idl_files = [] |
| 66 | |
| 67 | for filename in files: |
| 68 | name, ext = os.path.splitext(filename) |
| 69 | name_parts = name.split(os.sep) |
| 70 | if name_parts[0:2] == ['ppapi', 'c'] and ext == '.h': |
| 71 | h_files.append('/'.join(name_parts[2:])) |
| 72 | if name_parts[0:2] == ['ppapi', 'api'] and ext == '.idl': |
| 73 | idl_files.append('/'.join(name_parts[2:])) |
| 74 | |
| 75 | # Generate a list of all appropriate *.h and *.idl changes in this CL. |
| 76 | both = h_files + idl_files |
| 77 | |
| 78 | # If there aren't any, we are done checking. |
| 79 | if not both: return results |
| 80 | |
| 81 | missing = [] |
| 82 | for filename in idl_files: |
| 83 | if filename not in set(h_files): |
| 84 | missing.append(' ppapi/c/%s.h' % filename) |
| 85 | for filename in h_files: |
| 86 | if filename not in set(idl_files): |
| 87 | missing.append(' ppapi/api/%s.idl' % filename) |
| 88 | |
| 89 | if missing: |
| 90 | results.append( |
| 91 | output_api.PresubmitPromptWarning('Missing matching PPAPI definition:', |
| 92 | long_text='\n'.join(missing))) |
| 93 | |
| 94 | # Verify all *.h files match *.idl definitions, use: |
| 95 | # --test to prevent output to disk |
| 96 | # --diff to generate a unified diff |
| 97 | # --out to pick which files to examine (only the ones in the CL) |
| 98 | ppapi_dir = input_api.PresubmitLocalPath() |
| 99 | cmd = [ sys.executable, 'generator.py', |
[email protected] | a3aec32 | 2011-11-04 19:48:48 | [diff] [blame] | 100 | '--wnone', '--diff', '--test','--cgen', '--range=start,end'] |
[email protected] | 3937578 | 2011-09-14 16:55:09 | [diff] [blame] | 101 | |
| 102 | # Only generate output for IDL files references (as *.h or *.idl) in this CL |
| 103 | cmd.append('--out=' + ','.join([name + '.idl' for name in both])) |
[email protected] | 2978339a7 | 2011-11-30 17:59:14 | [diff] [blame] | 104 | results.extend(RunCmdAndCheck(cmd, |
[email protected] | 2978339a7 | 2011-11-30 17:59:14 | [diff] [blame] | 105 | 'PPAPI IDL Diff detected: Run the generator.', |
[email protected] | e4f373e | 2012-01-10 23:18:24 | [diff] [blame^] | 106 | output_api, |
| 107 | os.path.join(ppapi_dir, 'generators'))) |
[email protected] | 3937578 | 2011-09-14 16:55:09 | [diff] [blame] | 108 | return results |
| 109 | |
| 110 | def CheckChangeOnUpload(input_api, output_api): |
| 111 | # return [] |
| 112 | return CheckChange(input_api, output_api) |
| 113 | |
| 114 | def CheckChangeOnCommit(input_api, output_api): |
| 115 | # return [] |
| 116 | return CheckChange(input_api, output_api) |