Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 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 |
| 13 | # distribution. |
| 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. |
| 17 | # |
| 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. |
| 29 | |
| 30 | # Usage: make-file-arrays.py [--condition=condition-string] --out-h=<header-file-name> --out-cpp=<cpp-file-name> <input-file>... |
| 31 | |
| 32 | import os.path |
| 33 | import re |
| 34 | import sys |
| 35 | from optparse import OptionParser |
| 36 | |
| 37 | |
| 38 | def make_variable_name_and_read(file_name): |
| 39 | result = re.match(r"([\w\d_]+)\.([\w\d_]+)", os.path.basename(file_name)) |
| 40 | if not result: |
| 41 | print "Invalid input file name:", os.path.basename(file_name) |
| 42 | sys.exit(1) |
| 43 | variable_name = result.group(1)[0].lower() + result.group(1)[1:] + result.group(2).capitalize() |
| 44 | file = open(file_name, "rb") |
| 45 | content = file.read() |
| 46 | file.close() |
| 47 | return (variable_name, content) |
| 48 | |
| 49 | |
| 50 | def strip_whitespace_and_comments(file_name, content): |
| 51 | result = re.match(r".*\.([^.]+)", file_name) |
| 52 | if not result: |
| 53 | print "The file name has no extension:", file_name |
| 54 | sys.exit(1) |
| 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+") |
| 62 | if extension == "js": |
| 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) |
| 75 | return content |
| 76 | |
| 77 | |
| 78 | def main(): |
| 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() |
| 84 | if len(args) < 1: |
| 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") |
| 90 | |
| 91 | if options.flag: |
| 92 | options.flag = options.flag.replace(" AND ", " && ") |
Torne (Richard Coles) | 926b001 | 2013-03-28 15:32:48 +0000 | [diff] [blame] | 93 | options.flag = options.flag.replace(" OR ", " || ") |
Torne (Richard Coles) | 5c87bf8 | 2012-11-14 11:46:17 +0000 | [diff] [blame] | 94 | |
| 95 | header_file = open(options.out_header, "w") |
| 96 | if options.flag: |
| 97 | header_file.write("#if " + options.flag + "\n") |
| 98 | header_file.write("namespace WebCore {\n") |
| 99 | |
| 100 | cpp_file = open(options.out_cpp, "w") |
| 101 | cpp_file.write("#include \"config.h\"\n") |
| 102 | cpp_file.write("#include \"" + os.path.basename(options.out_header) + "\"\n") |
| 103 | if options.flag: |
| 104 | cpp_file.write("#if " + options.flag + "\n") |
| 105 | cpp_file.write("namespace WebCore {\n") |
| 106 | |
| 107 | for file_name in args: |
| 108 | (variable_name, content) = make_variable_name_and_read(file_name) |
| 109 | content = strip_whitespace_and_comments(file_name, content) |
| 110 | size = len(content) |
| 111 | header_file.write("extern const char %s[%d];\n" % (variable_name, size)) |
| 112 | cpp_file.write("const char %s[%d] = {\n" % (variable_name, size)) |
| 113 | for index in range(size): |
| 114 | char_code = ord(content[index]) |
| 115 | if char_code < 128: |
| 116 | cpp_file.write("%d" % char_code) |
| 117 | else: |
| 118 | cpp_file.write("'\\x%02x'" % char_code) |
| 119 | cpp_file.write("," if index != len(content) - 1 else "};\n") |
| 120 | if index % 20 == 19: |
| 121 | cpp_file.write("\n") |
| 122 | cpp_file.write("\n") |
| 123 | |
| 124 | header_file.write("}\n") |
| 125 | if options.flag: |
| 126 | header_file.write("#endif\n") |
| 127 | header_file.close() |
| 128 | |
| 129 | cpp_file.write("}\n") |
| 130 | if options.flag: |
| 131 | cpp_file.write("#endif\n") |
| 132 | cpp_file.close() |
| 133 | |
| 134 | |
| 135 | if __name__ == "__main__": |
| 136 | main() |