2 # Copyright (C) 2012 Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 # Usage: make-file-arrays.py [--condition=condition-string] --out-h=<header-file-name> --out-cpp=<cpp-file-name> <input-file>...
35 from optparse import OptionParser
38 def make_variable_name_and_read(file_name):
39 result = re.match(r"([\w\d_]+)\.([\w\d_]+)", os.path.basename(file_name))
41 print "Invalid input file name:", os.path.basename(file_name)
43 variable_name = result.group(1)[0].lower() + result.group(1)[1:] + result.group(2).capitalize()
44 file = open(file_name, "rb")
47 return (variable_name, content)
50 def strip_whitespace_and_comments(file_name, content):
51 result = re.match(r".*\.([^.]+)", file_name)
53 print "The file name has no extension:", file_name
55 extension = result.group(1).lower()
56 multi_line_comment = re.compile(r"/\*.*?\*/", re.MULTILINE | re.DOTALL)
57 single_line_comment = re.compile(r"//.*$", re.MULTILINE)
58 repeating_space = re.compile(r"[ \t]+", re.MULTILINE)
59 leading_space = re.compile(r"^[ \t]+", re.MULTILINE)
60 trailing_space = re.compile(r"[ \t]+$", re.MULTILINE)
61 empty_line = re.compile(r"\n+")
63 content = multi_line_comment.sub("", content)
64 content = single_line_comment.sub("", content)
65 content = repeating_space.sub(" ", content)
66 content = leading_space.sub("", content)
67 content = trailing_space.sub("", content)
68 content = empty_line.sub("\n", content)
69 elif extension == "css":
70 content = multi_line_comment.sub("", content)
71 content = repeating_space.sub(" ", content)
72 content = leading_space.sub("", content)
73 content = trailing_space.sub("", content)
74 content = empty_line.sub("\n", content)
79 parser = OptionParser()
80 parser.add_option("--out-h", dest="out_header")
81 parser.add_option("--out-cpp", dest="out_cpp")
82 parser.add_option("--condition", dest="flag")
83 (options, args) = parser.parse_args()
85 parser.error("Need one or more input files")
86 if not options.out_header:
87 parser.error("Need to specify --out-h=filename")
88 if not options.out_cpp:
89 parser.error("Need to specify --out-cpp=filename")
92 options.flag = options.flag.replace(" AND ", " && ")
94 header_file = open(options.out_header, "w")
96 header_file.write("#if " + options.flag + "\n")
97 header_file.write("namespace WebCore {\n")
99 cpp_file = open(options.out_cpp, "w")
100 cpp_file.write("#include \"config.h\"\n")
101 cpp_file.write("#include \"" + os.path.basename(options.out_header) + "\"\n")
103 cpp_file.write("#if " + options.flag + "\n")
104 cpp_file.write("namespace WebCore {\n")
106 for file_name in args:
107 (variable_name, content) = make_variable_name_and_read(file_name)
108 content = strip_whitespace_and_comments(file_name, content)
110 header_file.write("extern const char %s[%d];\n" % (variable_name, size))
111 cpp_file.write("const char %s[%d] = {\n" % (variable_name, size))
112 for index in range(size):
113 char_code = ord(content[index])
115 cpp_file.write("%d" % char_code)
117 cpp_file.write("'\\x%02x'" % char_code)
118 cpp_file.write("," if index != len(content) - 1 else "};\n")
123 header_file.write("}\n")
125 header_file.write("#endif\n")
128 cpp_file.write("}\n")
130 cpp_file.write("#endif\n")
134 if __name__ == "__main__":