Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1520)

Side by Side Diff: Source/web/scripts/make-file-arrays.py

Issue 43083002: Refactor make-file-arrays.py to ease reuse (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Revised Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (C) 2012 Google Inc. All rights reserved. 2 # Copyright (C) 2012 Google Inc. All rights reserved.
3 # 3 #
4 # Redistribution and use in source and binary forms, with or without 4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are 5 # modification, are permitted provided that the following conditions are
6 # met: 6 # met:
7 # 7 #
8 # * Redistributions of source code must retain the above copyright 8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer. 9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above 10 # * Redistributions in binary form must reproduce the above
(...skipping 18 matching lines...) Expand all
29 29
30 # Usage: make-file-arrays.py [--condition=condition-string] --out-h=<header-file -name> --out-cpp=<cpp-file-name> <input-file>... 30 # Usage: make-file-arrays.py [--condition=condition-string] --out-h=<header-file -name> --out-cpp=<cpp-file-name> <input-file>...
31 31
32 import os.path 32 import os.path
33 import re 33 import re
34 import sys 34 import sys
35 from optparse import OptionParser 35 from optparse import OptionParser
36 36
37 37
38 def make_variable_name_and_read(file_name): 38 def make_variable_name_and_read(file_name):
39 result = re.match(r"([\w\d_]+)\.([\w\d_]+)", os.path.basename(file_name)) 39 result = re.match(r'([\w\d_]+)\.([\w\d_]+)', os.path.basename(file_name))
40 if not result: 40 if not result:
41 print "Invalid input file name:", os.path.basename(file_name) 41 print 'Invalid input file name:', os.path.basename(file_name)
42 sys.exit(1) 42 sys.exit(1)
43 variable_name = result.group(1)[0].lower() + result.group(1)[1:] + result.gr oup(2).capitalize() 43 variable_name = result.group(1)[0].lower() + result.group(1)[1:] + result.gr oup(2).capitalize()
44 file = open(file_name, "rb") 44 with open(file_name, 'rb') as f:
45 content = file.read() 45 content = f.read()
46 file.close() 46 return variable_name, content
47 return (variable_name, content)
48 47
49 48
50 def strip_whitespace_and_comments(file_name, content): 49 def strip_whitespace_and_comments(file_name, content):
51 result = re.match(r".*\.([^.]+)", file_name) 50 result = re.match(r'.*\.([^.]+)', file_name)
52 if not result: 51 if not result:
53 print "The file name has no extension:", file_name 52 print 'The file name has no extension:', file_name
54 sys.exit(1) 53 sys.exit(1)
55 extension = result.group(1).lower() 54 extension = result.group(1).lower()
56 multi_line_comment = re.compile(r"/\*.*?\*/", re.MULTILINE | re.DOTALL) 55 multi_line_comment = re.compile(r'/\*.*?\*/', re.MULTILINE | re.DOTALL)
57 single_line_comment = re.compile(r"//.*$", re.MULTILINE) 56 single_line_comment = re.compile(r'^//.*$', re.MULTILINE)
58 repeating_space = re.compile(r"[ \t]+", re.MULTILINE) 57 # Don't accidentally match URLs (http://...)
59 leading_space = re.compile(r"^[ \t]+", re.MULTILINE) 58 trailing_comment = re.compile(r'([^:])//.*$', re.MULTILINE)
60 trailing_space = re.compile(r"[ \t]+$", re.MULTILINE) 59 repeating_space = re.compile(r'[ \t]+', re.MULTILINE)
61 empty_line = re.compile(r"\n+") 60 leading_space = re.compile(r'^[ \t]+', re.MULTILINE)
62 if extension == "js": 61 trailing_space = re.compile(r'[ \t]+$', re.MULTILINE)
63 content = multi_line_comment.sub("", content) 62 empty_line = re.compile(r'\n+')
64 content = single_line_comment.sub("", content) 63 if extension == 'js':
65 content = repeating_space.sub(" ", content) 64 content = multi_line_comment.sub('', content)
66 content = leading_space.sub("", content) 65 content = single_line_comment.sub('', content)
67 content = trailing_space.sub("", content) 66 content = trailing_comment.sub(r'\1', content)
68 content = empty_line.sub("\n", content) 67 content = repeating_space.sub(' ', content)
69 elif extension == "css": 68 content = leading_space.sub('', content)
70 content = multi_line_comment.sub("", content) 69 content = trailing_space.sub('', content)
71 content = repeating_space.sub(" ", content) 70 content = empty_line.sub('\n', content)
72 content = leading_space.sub("", content) 71 elif extension == 'css':
73 content = trailing_space.sub("", content) 72 content = multi_line_comment.sub('', content)
74 content = empty_line.sub("\n", content) 73 content = repeating_space.sub(' ', content)
74 content = leading_space.sub('', content)
75 content = trailing_space.sub('', content)
76 content = empty_line.sub('\n', content)
75 return content 77 return content
76 78
77 79
80 def process_file(file_name):
81 variable_name, content = make_variable_name_and_read(file_name)
82 content = strip_whitespace_and_comments(file_name, content)
83 size = len(content)
84 return variable_name, content
85
86
87 def write_header_file(header_file_name, flag, names_and_contents, namespace='Web Kit'):
88 with open(header_file_name, 'w') as header_file:
89 if flag:
90 header_file.write('#if ' + flag + '\n')
91 header_file.write('namespace %s {\n' % namespace)
92 for variable_name, content in names_and_contents:
93 size = len(content)
94 header_file.write('extern const char %s[%d];\n' % (variable_name, si ze))
95 header_file.write('}\n')
96 if flag:
97 header_file.write('#endif\n')
98
99
100 def write_cpp_file(cpp_file_name, flag, names_and_contents, header_file_name, na mespace='WebKit'):
101 with open(cpp_file_name, 'w') as cpp_file:
102 cpp_file.write('#include "config.h"\n')
103 cpp_file.write('#include "%s"\n' % os.path.basename(header_file_name))
104 if flag:
105 cpp_file.write('#if ' + flag + '\n')
106 cpp_file.write('namespace %s {\n' % namespace)
107 for variable_name, content in names_and_contents:
108 cpp_file.write(cpp_constant_string(variable_name, content))
109 cpp_file.write('}\n')
110 if flag:
111 cpp_file.write('#endif\n')
112
113
114 def cpp_constant_string(variable_name, content):
115 output = []
116 size = len(content)
117 output.append('const char %s[%d] = {\n' % (variable_name, size))
118 for index in range(size):
119 char_code = ord(content[index])
120 if char_code < 128:
121 output.append('%d' % char_code)
122 else:
123 output.append(r"'\x%02x'" % char_code)
124 output.append(',' if index != len(content) - 1 else '};\n')
125 if index % 20 == 19:
126 output.append('\n')
127 output.append('\n')
128 return ''.join(output)
129
130
78 def main(): 131 def main():
79 parser = OptionParser() 132 parser = OptionParser()
80 parser.add_option("--out-h", dest="out_header") 133 parser.add_option('--out-h', dest='out_header')
81 parser.add_option("--out-cpp", dest="out_cpp") 134 parser.add_option('--out-cpp', dest='out_cpp')
82 parser.add_option("--condition", dest="flag") 135 parser.add_option('--condition', dest='flag')
83 (options, args) = parser.parse_args() 136 (options, args) = parser.parse_args()
84 if len(args) < 1: 137 if len(args) < 1:
85 parser.error("Need one or more input files") 138 parser.error('Need one or more input files')
86 if not options.out_header: 139 if not options.out_header:
87 parser.error("Need to specify --out-h=filename") 140 parser.error('Need to specify --out-h=filename')
88 if not options.out_cpp: 141 if not options.out_cpp:
89 parser.error("Need to specify --out-cpp=filename") 142 parser.error('Need to specify --out-cpp=filename')
90 143
91 if options.flag: 144 if options.flag:
92 options.flag = options.flag.replace(" AND ", " && ") 145 options.flag = options.flag.replace(' AND ', ' && ')
93 options.flag = options.flag.replace(" OR ", " || ") 146 options.flag = options.flag.replace(' OR ', ' || ')
94 147
95 header_file = open(options.out_header, "w") 148 names_and_contents = [process_file(file_name) for file_name in args]
96 if options.flag:
97 header_file.write("#if " + options.flag + "\n")
98 header_file.write("namespace WebKit {\n")
99 149
100 cpp_file = open(options.out_cpp, "w") 150 if options.out_header:
101 cpp_file.write("#include \"config.h\"\n") 151 write_header_file(options.out_header, options.flag, names_and_contents)
102 cpp_file.write("#include \"" + os.path.basename(options.out_header) + "\"\n" ) 152 write_cpp_file(options.out_cpp, options.flag, names_and_contents, options.ou t_header)
103 if options.flag:
104 cpp_file.write("#if " + options.flag + "\n")
105 cpp_file.write("namespace WebKit {\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 153
134 154
135 if __name__ == "__main__": 155 if __name__ == '__main__':
136 main() 156 sys.exit(main())
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698