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

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: 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)
tkent 2013/10/25 04:06:07 This pattern will break XMLView.js. http://src.ch
abarth-chromium 2013/10/25 04:09:35 That's a good point. Currently XMLViewer.js goes
Nils Barth (inactive) 2013/10/25 05:03:57 Rather, XMLViewer.js. Good point, thanks! I've add
Nils Barth (inactive) 2013/10/25 05:03:57 That would be much better; using regexes for parsi
58 repeating_space = re.compile(r"[ \t]+", re.MULTILINE) 57 repeating_space = re.compile(r'[ \t]+', re.MULTILINE)
59 leading_space = re.compile(r"^[ \t]+", re.MULTILINE) 58 leading_space = re.compile(r'^[ \t]+', re.MULTILINE)
60 trailing_space = re.compile(r"[ \t]+$", re.MULTILINE) 59 trailing_space = re.compile(r'[ \t]+$', re.MULTILINE)
61 empty_line = re.compile(r"\n+") 60 empty_line = re.compile(r'\n+')
62 if extension == "js": 61 if extension == 'js':
63 content = multi_line_comment.sub("", content) 62 content = multi_line_comment.sub('', content)
64 content = single_line_comment.sub("", content) 63 content = single_line_comment.sub('', content)
65 content = repeating_space.sub(" ", content) 64 content = repeating_space.sub(' ', content)
66 content = leading_space.sub("", content) 65 content = leading_space.sub('', content)
67 content = trailing_space.sub("", content) 66 content = trailing_space.sub('', content)
68 content = empty_line.sub("\n", content) 67 content = empty_line.sub('\n', content)
69 elif extension == "css": 68 elif extension == 'css':
70 content = multi_line_comment.sub("", content) 69 content = multi_line_comment.sub('', content)
71 content = repeating_space.sub(" ", content) 70 content = repeating_space.sub(' ', content)
72 content = leading_space.sub("", content) 71 content = leading_space.sub('', content)
73 content = trailing_space.sub("", content) 72 content = trailing_space.sub('', content)
74 content = empty_line.sub("\n", content) 73 content = empty_line.sub('\n', content)
75 return content 74 return content
76 75
77 76
77 def process_file(file_name):
78 variable_name, content = make_variable_name_and_read(file_name)
79 content = strip_whitespace_and_comments(file_name, content)
80 size = len(content)
81 return variable_name, content
82
83
84 def write_header_file(header_file_name, flag, names_and_contents):
85 with open(header_file_name, 'w') as header_file:
86 if flag:
87 header_file.write('#if ' + flag + '\n')
88 header_file.write('namespace WebKit {\n')
abarth-chromium 2013/10/25 04:04:02 It's still hard-coded to use the WebKit namespace.
Nils Barth (inactive) 2013/10/25 05:03:57 Got it, made |namespace| into a parameter.
89 for variable_name, content in names_and_contents:
90 size = len(content)
91 header_file.write('extern const char %s[%d];\n' % (variable_name, si ze))
92 header_file.write('}\n')
93 if flag:
94 header_file.write('#endif\n')
95
96
97 def write_cpp_file(cpp_file_name, flag, names_and_contents, header_file_name):
98 with open(cpp_file_name, 'w') as cpp_file:
99 cpp_file.write('#include "config.h"\n')
100 cpp_file.write('#include "%s"\n' % os.path.basename(header_file_name))
101 if flag:
102 cpp_file.write('#if ' + flag + '\n')
103 cpp_file.write('namespace WebKit {\n')
104 for variable_name, content in names_and_contents:
105 cpp_file.write(cpp_constant_string(variable_name, content))
106 cpp_file.write('}\n')
107 if flag:
108 cpp_file.write('#endif\n')
109
110
111 def cpp_constant_string(variable_name, content):
112 output = []
113 size = len(content)
114 output.append('const char %s[%d] = {\n' % (variable_name, size))
115 for index in range(size):
116 char_code = ord(content[index])
117 if char_code < 128:
118 output.append('%d' % char_code)
119 else:
120 output.append(r"'\x%02x'" % char_code)
121 output.append(',' if index != len(content) - 1 else '};\n')
122 if index % 20 == 19:
123 output.append('\n')
124 output.append('\n')
125 return ''.join(output)
126
127
78 def main(): 128 def main():
79 parser = OptionParser() 129 parser = OptionParser()
80 parser.add_option("--out-h", dest="out_header") 130 parser.add_option('--out-h', dest='out_header')
81 parser.add_option("--out-cpp", dest="out_cpp") 131 parser.add_option('--out-cpp', dest='out_cpp')
82 parser.add_option("--condition", dest="flag") 132 parser.add_option('--condition', dest='flag')
83 (options, args) = parser.parse_args() 133 (options, args) = parser.parse_args()
84 if len(args) < 1: 134 if len(args) < 1:
85 parser.error("Need one or more input files") 135 parser.error("Need one or more input files")
86 if not options.out_header: 136 if not options.out_header:
87 parser.error("Need to specify --out-h=filename") 137 parser.error("Need to specify --out-h=filename")
88 if not options.out_cpp: 138 if not options.out_cpp:
89 parser.error("Need to specify --out-cpp=filename") 139 parser.error("Need to specify --out-cpp=filename")
90 140
91 if options.flag: 141 if options.flag:
92 options.flag = options.flag.replace(" AND ", " && ") 142 options.flag = options.flag.replace(' AND ', ' && ')
93 options.flag = options.flag.replace(" OR ", " || ") 143 options.flag = options.flag.replace(' OR ', ' || ')
94 144
95 header_file = open(options.out_header, "w") 145 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 146
100 cpp_file = open(options.out_cpp, "w") 147 if options.out_header:
101 cpp_file.write("#include \"config.h\"\n") 148 write_header_file(options.out_header, options.flag, names_and_contents)
102 cpp_file.write("#include \"" + os.path.basename(options.out_header) + "\"\n" ) 149 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 150
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 151
134 152
135 if __name__ == "__main__": 153 if __name__ == "__main__":
136 main() 154 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