| OLD | NEW |
| (Empty) |
| 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 rjsmin_path = os.path.abspath(os.path.join( | |
| 38 os.path.dirname(__file__), | |
| 39 "..", | |
| 40 "..", | |
| 41 "build", | |
| 42 "scripts")) | |
| 43 sys.path.append(rjsmin_path) | |
| 44 import rjsmin | |
| 45 | |
| 46 | |
| 47 def make_variable_name_and_read(file_name): | |
| 48 result = re.match(r'([\w\d_]+)\.([\w\d_]+)', os.path.basename(file_name)) | |
| 49 if not result: | |
| 50 print 'Invalid input file name:', os.path.basename(file_name) | |
| 51 sys.exit(1) | |
| 52 variable_name = result.group(1)[0].lower() + result.group(1)[1:] + result.gr
oup(2).capitalize() | |
| 53 with open(file_name, 'rb') as f: | |
| 54 content = f.read() | |
| 55 return variable_name, content | |
| 56 | |
| 57 | |
| 58 def strip_whitespace_and_comments(file_name, content): | |
| 59 result = re.match(r'.*\.([^.]+)', file_name) | |
| 60 if not result: | |
| 61 print 'The file name has no extension:', file_name | |
| 62 sys.exit(1) | |
| 63 extension = result.group(1).lower() | |
| 64 multi_line_comment = re.compile(r'/\*.*?\*/', re.MULTILINE | re.DOTALL) | |
| 65 # Don't accidentally match URLs (http://...) | |
| 66 repeating_space = re.compile(r'[ \t]+', re.MULTILINE) | |
| 67 leading_space = re.compile(r'^[ \t]+', re.MULTILINE) | |
| 68 trailing_space = re.compile(r'[ \t]+$', re.MULTILINE) | |
| 69 empty_line = re.compile(r'\n+') | |
| 70 if extension == 'js': | |
| 71 content = rjsmin.jsmin(content) | |
| 72 elif extension == 'css': | |
| 73 content = multi_line_comment.sub('', content) | |
| 74 content = repeating_space.sub(' ', content) | |
| 75 content = leading_space.sub('', content) | |
| 76 content = trailing_space.sub('', content) | |
| 77 content = empty_line.sub('\n', content) | |
| 78 return content | |
| 79 | |
| 80 | |
| 81 def process_file(file_name): | |
| 82 variable_name, content = make_variable_name_and_read(file_name) | |
| 83 content = strip_whitespace_and_comments(file_name, content) | |
| 84 size = len(content) | |
| 85 return variable_name, content | |
| 86 | |
| 87 | |
| 88 def write_header_file(header_file_name, flag, names_and_contents, namespace='bli
nk'): | |
| 89 with open(header_file_name, 'w') as header_file: | |
| 90 if flag: | |
| 91 header_file.write('#if ' + flag + '\n') | |
| 92 header_file.write('namespace %s {\n' % namespace) | |
| 93 for variable_name, content in names_and_contents: | |
| 94 size = len(content) | |
| 95 header_file.write('extern const char %s[%d];\n' % (variable_name, si
ze)) | |
| 96 header_file.write('}\n') | |
| 97 if flag: | |
| 98 header_file.write('#endif\n') | |
| 99 | |
| 100 | |
| 101 def write_cpp_file(cpp_file_name, flag, names_and_contents, header_file_name, na
mespace='blink'): | |
| 102 with open(cpp_file_name, 'w') as cpp_file: | |
| 103 cpp_file.write('#include "config.h"\n') | |
| 104 cpp_file.write('#include "%s"\n' % os.path.basename(header_file_name)) | |
| 105 if flag: | |
| 106 cpp_file.write('#if ' + flag + '\n') | |
| 107 cpp_file.write('namespace %s {\n' % namespace) | |
| 108 for variable_name, content in names_and_contents: | |
| 109 cpp_file.write(cpp_constant_string(variable_name, content)) | |
| 110 cpp_file.write('}\n') | |
| 111 if flag: | |
| 112 cpp_file.write('#endif\n') | |
| 113 | |
| 114 | |
| 115 def cpp_constant_string(variable_name, content): | |
| 116 output = [] | |
| 117 size = len(content) | |
| 118 output.append('const char %s[%d] = {\n' % (variable_name, size)) | |
| 119 for index in range(size): | |
| 120 char_code = ord(content[index]) | |
| 121 if char_code < 128: | |
| 122 output.append('%d' % char_code) | |
| 123 else: | |
| 124 output.append(r"'\x%02x'" % char_code) | |
| 125 output.append(',' if index != len(content) - 1 else '};\n') | |
| 126 if index % 20 == 19: | |
| 127 output.append('\n') | |
| 128 output.append('\n') | |
| 129 return ''.join(output) | |
| 130 | |
| 131 | |
| 132 def main(): | |
| 133 parser = OptionParser() | |
| 134 parser.add_option('--out-h', dest='out_header') | |
| 135 parser.add_option('--out-cpp', dest='out_cpp') | |
| 136 parser.add_option('--condition', dest='flag') | |
| 137 (options, args) = parser.parse_args() | |
| 138 if len(args) < 1: | |
| 139 parser.error('Need one or more input files') | |
| 140 if not options.out_header: | |
| 141 parser.error('Need to specify --out-h=filename') | |
| 142 if not options.out_cpp: | |
| 143 parser.error('Need to specify --out-cpp=filename') | |
| 144 | |
| 145 if options.flag: | |
| 146 options.flag = options.flag.replace(' AND ', ' && ') | |
| 147 options.flag = options.flag.replace(' OR ', ' || ') | |
| 148 | |
| 149 names_and_contents = [process_file(file_name) for file_name in args] | |
| 150 | |
| 151 if options.out_header: | |
| 152 write_header_file(options.out_header, options.flag, names_and_contents) | |
| 153 write_cpp_file(options.out_cpp, options.flag, names_and_contents, options.ou
t_header) | |
| 154 | |
| 155 | |
| 156 if __name__ == '__main__': | |
| 157 sys.exit(main()) | |
| OLD | NEW |