3 # Copyright (C) 2010 Google Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 from __future__ import with_statement
34 from webkitpy.common.checkout import scm
35 from webkitpy.common.system.filesystem import FileSystem
36 from webkitpy.common.system.executive import Executive
39 _log = logging.getLogger(__name__)
42 def remove_first_line_comment(text):
43 return re.compile(r'^<!--.*?-->\s*', re.DOTALL).sub('', text)
46 def translate_includes(text):
47 # Mapping of single filename to relative path under WebKit root.
48 # Assumption: these filenames are globally unique.
50 "js-test-style.css": "../../js/resources",
51 "js-test-pre.js": "../../js/resources",
52 "js-test-post.js": "../../js/resources",
53 "desktop-gl-constants.js": "resources",
56 for filename, path in include_mapping.items():
57 search = r'(?:[^"\'= ]*/)?' + re.escape(filename)
58 # We use '/' instead of os.path.join in order to produce consistent
59 # output cross-platform.
60 replace = path + '/' + filename
61 text = re.sub(search, replace, text)
66 def translate_khronos_test(text):
68 This method translates the contents of a Khronos test to a WebKit test.
72 remove_first_line_comment,
76 for f in translateFuncs:
82 def update_file(in_filename, out_dir):
83 # check in_filename exists
84 # check out_dir exists
85 out_filename = os.path.join(out_dir, os.path.basename(in_filename))
87 _log.debug("Processing " + in_filename)
88 with open(in_filename, 'r') as in_file:
89 with open(out_filename, 'w') as out_file:
90 out_file.write(translate_khronos_test(in_file.read()))
93 def update_directory(in_dir, out_dir):
94 for filename in glob.glob(os.path.join(in_dir, '*.html')):
95 update_file(os.path.join(in_dir, filename), out_dir)
98 def default_out_dir():
99 detector = scm.SCMDetector(FileSystem(), Executive())
100 current_scm = detector.detect_scm_system(os.path.dirname(sys.argv[0]))
103 root_dir = current_scm.checkout_root
106 out_dir = os.path.join(root_dir, "LayoutTests/fast/canvas/webgl")
107 if os.path.isdir(out_dir):
112 def configure_logging(options):
113 """Configures the logging system."""
114 log_fmt = '%(levelname)s: %(message)s'
115 log_datefmt = '%y%m%d %H:%M:%S'
116 log_level = logging.INFO
118 log_fmt = ('%(asctime)s %(filename)s:%(lineno)-4d %(levelname)s '
120 log_level = logging.DEBUG
121 logging.basicConfig(level=log_level, format=log_fmt,
126 usage = "usage: %prog [options] (input file or directory)"
127 parser = optparse.OptionParser(usage=usage)
128 parser.add_option('-v', '--verbose',
131 help='include debug-level logging')
132 parser.add_option('-o', '--output',
135 default=default_out_dir(),
137 help='specify an output directory to place files '
138 'in [default: %default]')
143 parser = option_parser()
144 (options, args) = parser.parse_args()
145 configure_logging(options)
148 _log.error("Must specify an input directory or filename.")
153 if os.path.isfile(in_name):
154 update_file(in_name, options.output)
155 elif os.path.isdir(in_name):
156 update_directory(in_name, options.output)
158 _log.error("'%s' is not a directory or a file.", in_name)
164 if __name__ == "__main__":