Unreviewed EFL gardening. Rebaseline after r169522.
[WebKit-https.git] / Tools / Scripts / webkitpy / bindings / main.py
1 # Copyright (C) 2011 Google Inc.  All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
5 # are met:
6 # 1. Redistributions of source code must retain the above copyright
7 #    notice, this list of conditions and the following disclaimer.
8 # 2. Redistributions in binary form must reproduce the above copyright
9 #    notice, this list of conditions and the following disclaimer in the
10 #    documentation and/or other materials provided with the distribution.
11 #
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
13 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
16 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
20 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 #
24
25 import os
26 import os.path
27 import shutil
28 import subprocess
29 import sys
30 import tempfile
31 from webkitpy.common.checkout.scm.detection import detect_scm_system
32 from webkitpy.common.system.executive import ScriptError
33
34
35 class BindingsTests:
36
37     def __init__(self, reset_results, generators, executive):
38         self.reset_results = reset_results
39         self.generators = generators
40         self.executive = executive
41
42     def generate_from_idl(self, generator, idl_file, output_directory, supplemental_dependency_file):
43         cmd = ['perl', '-w',
44                '-IWebCore/bindings/scripts',
45                'WebCore/bindings/scripts/generate-bindings.pl',
46                # idl include directories (path relative to generate-bindings.pl)
47                '--include', '.',
48                '--defines', 'TESTING_%s' % generator,
49                '--generator', generator,
50                '--outputDir', output_directory,
51                '--supplementalDependencyFile', supplemental_dependency_file,
52                idl_file]
53
54         exit_code = 0
55         try:
56             output = self.executive.run_command(cmd)
57             if output:
58                 print output
59         except ScriptError, e:
60             print e.output
61             exit_code = e.exit_code
62         return exit_code
63
64     def generate_supplemental_dependency(self, input_directory, supplemental_dependency_file, window_constructors_file, workerglobalscope_constructors_file, sharedworkerglobalscope_constructors_file, dedicatedworkerglobalscope_constructors_file):
65         idl_files_list = tempfile.mkstemp()
66         for input_file in os.listdir(input_directory):
67             (name, extension) = os.path.splitext(input_file)
68             if extension != '.idl':
69                 continue
70             os.write(idl_files_list[0], os.path.join(input_directory, input_file) + "\n")
71         os.close(idl_files_list[0])
72
73         cmd = ['perl', '-w',
74                '-IWebCore/bindings/scripts',
75                'WebCore/bindings/scripts/preprocess-idls.pl',
76                '--idlFilesList', idl_files_list[1],
77                '--defines', '',
78                '--supplementalDependencyFile', supplemental_dependency_file,
79                '--windowConstructorsFile', window_constructors_file,
80                '--workerGlobalScopeConstructorsFile', workerglobalscope_constructors_file,
81                '--sharedWorkerGlobalScopeConstructorsFile', sharedworkerglobalscope_constructors_file,
82                '--dedicatedWorkerGlobalScopeConstructorsFile', dedicatedworkerglobalscope_constructors_file]
83
84         exit_code = 0
85         try:
86             output = self.executive.run_command(cmd)
87             if output:
88                 print output
89         except ScriptError, e:
90             print e.output
91             exit_code = e.exit_code
92         os.remove(idl_files_list[1])
93         return exit_code
94
95     def detect_changes(self, generator, work_directory, reference_directory):
96         changes_found = False
97         for output_file in os.listdir(work_directory):
98             cmd = ['diff',
99                    '-u',
100                    '-N',
101                    os.path.join(reference_directory, output_file),
102                    os.path.join(work_directory, output_file)]
103
104             exit_code = 0
105             try:
106                 output = self.executive.run_command(cmd)
107             except ScriptError, e:
108                 output = e.output
109                 exit_code = e.exit_code
110
111             if exit_code or output:
112                 print 'FAIL: (%s) %s' % (generator, output_file)
113                 print output
114                 changes_found = True
115             else:
116                 print 'PASS: (%s) %s' % (generator, output_file)
117         return changes_found
118
119     def run_tests(self, generator, input_directory, reference_directory, supplemental_dependency_file):
120         work_directory = reference_directory
121
122         passed = True
123         for input_file in os.listdir(input_directory):
124             (name, extension) = os.path.splitext(input_file)
125             if extension != '.idl':
126                 continue
127             # Generate output into the work directory (either the given one or a
128             # temp one if not reset_results is performed)
129             if not self.reset_results:
130                 work_directory = tempfile.mkdtemp()
131
132             if self.generate_from_idl(generator,
133                                       os.path.join(input_directory, input_file),
134                                       work_directory,
135                                       supplemental_dependency_file):
136                 passed = False
137
138             if self.reset_results:
139                 print "Reset results: (%s) %s" % (generator, input_file)
140                 continue
141
142             # Detect changes
143             if self.detect_changes(generator, work_directory, reference_directory):
144                 passed = False
145             shutil.rmtree(work_directory)
146
147         return passed
148
149     def main(self):
150         current_scm = detect_scm_system(os.curdir)
151         os.chdir(os.path.join(current_scm.checkout_root, 'Source'))
152
153         all_tests_passed = True
154
155         input_directory = os.path.join('WebCore', 'bindings', 'scripts', 'test')
156         supplemental_dependency_file = tempfile.mkstemp()[1]
157         window_constructors_file = tempfile.mkstemp()[1]
158         workerglobalscope_constructors_file = tempfile.mkstemp()[1]
159         sharedworkerglobalscope_constructors_file = tempfile.mkstemp()[1]
160         dedicatedworkerglobalscope_constructors_file = tempfile.mkstemp()[1]
161         if self.generate_supplemental_dependency(input_directory, supplemental_dependency_file, window_constructors_file, workerglobalscope_constructors_file, sharedworkerglobalscope_constructors_file, dedicatedworkerglobalscope_constructors_file):
162             print 'Failed to generate a supplemental dependency file.'
163             os.remove(supplemental_dependency_file)
164             os.remove(window_constructors_file)
165             os.remove(workerglobalscope_constructors_file)
166             os.remove(sharedworkerglobalscope_constructors_file)
167             os.remove(dedicatedworkerglobalscope_constructors_file)
168             return -1
169
170         for generator in self.generators:
171             input_directory = os.path.join('WebCore', 'bindings', 'scripts', 'test')
172             reference_directory = os.path.join('WebCore', 'bindings', 'scripts', 'test', generator)
173             if not self.run_tests(generator, input_directory, reference_directory, supplemental_dependency_file):
174                 all_tests_passed = False
175
176         os.remove(supplemental_dependency_file)
177         os.remove(window_constructors_file)
178         os.remove(workerglobalscope_constructors_file)
179         os.remove(sharedworkerglobalscope_constructors_file)
180         os.remove(dedicatedworkerglobalscope_constructors_file)
181         print ''
182         if all_tests_passed:
183             print 'All tests PASS!'
184             return 0
185         else:
186             print 'Some tests FAIL! (To update the reference files, execute "run-bindings-tests --reset-results")'
187             return -1