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