OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Convert PrivateScript's sources to C++ constant strings. | 5 """Convert PrivateScript's sources to C++ constant strings. |
6 FIXME: We don't want to add more build scripts. Rewrite this script in grit. crb
ug.com/388121 | 6 FIXME: We don't want to add more build scripts. Rewrite this script in grit. crb
ug.com/388121 |
7 | 7 |
8 Usage: | 8 Usage: |
9 python make_private_script_source.py DESTINATION_FILE SOURCE_FILES | 9 python make_private_script_source.py DESTINATION_FILE SOURCE_FILES |
10 """ | 10 """ |
11 | 11 |
12 import optparse | |
13 import os | 12 import os |
14 import re | 13 import re |
15 import sys | 14 import sys |
16 | 15 |
17 | 16 |
18 # We assume that X.js has a corresponding X.idl in the same directory. | 17 # We assume that X.js has a corresponding X.idl in the same directory. |
19 # If X is a partial interface, this method extracts the base name of the partial
interface from X.idl. | 18 # If X is a partial interface, this method extracts the base name of the partial
interface from X.idl. |
20 # Otherwise, this method returns None. | 19 # Otherwise, this method returns None. |
21 def extract_partial_interface_name(filename): | 20 def extract_partial_interface_name(filename): |
22 basename, ext = os.path.splitext(filename) | 21 basename, ext = os.path.splitext(filename) |
23 assert ext == '.js' | 22 assert ext == '.js' |
24 # PrivateScriptRunner.js is a special JS script to control private scripts, | 23 # PrivateScriptRunner.js is a special JS script to control private scripts, |
25 # and doesn't have a corresponding IDL file. | 24 # and doesn't have a corresponding IDL file. |
26 if os.path.basename(basename) == 'PrivateScriptRunner': | 25 if os.path.basename(basename) == 'PrivateScriptRunner': |
27 return None | 26 return None |
28 idl_filename = basename + '.idl' | 27 idl_filename = basename + '.idl' |
29 with open(idl_filename) as f: | 28 with open(idl_filename) as f: |
30 contents = f.read() | 29 contents = f.read() |
31 match = re.search(r'partial\s+interface\s+(\w+)\s*{', contents) | 30 match = re.search(r'partial\s+interface\s+(\w+)\s*{', contents) |
32 return match and match.group(1) | 31 return match and match.group(1) |
33 | 32 |
34 | 33 |
35 def main(): | 34 def main(): |
36 parser = optparse.OptionParser() | 35 output_filename = sys.argv[1] |
37 parser.add_option('--for-testing', action="store_true", default=False) | 36 input_filenames = sys.argv[2:] |
38 | |
39 options, args = parser.parse_args() | |
40 output_filename = args[0] | |
41 input_filenames = args[1:] | |
42 source_name, ext = os.path.splitext(os.path.basename(output_filename)) | 37 source_name, ext = os.path.splitext(os.path.basename(output_filename)) |
43 | 38 |
44 contents = [] | 39 contents = [] |
45 contents.append('#ifndef %s_h\n' % source_name) | 40 for input_filename in input_filenames: |
46 contents.append('#define %s_h\n' % source_name) | 41 class_name, ext = os.path.splitext(os.path.basename(input_filename)) |
47 if options.for_testing: | 42 with open(input_filename) as input_file: |
48 for input_filename in input_filenames: | 43 input_text = input_file.read() |
49 class_name, ext = os.path.splitext(os.path.basename(input_filename)) | 44 hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_text] |
50 with open(input_filename) as input_file: | 45 contents.append('const unsigned char kSourceOf%s[] = {\n %s\n};\n
\n' % ( |
51 input_text = input_file.read() | 46 class_name, ', '.join(hex_values))) |
52 hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_te
xt] | |
53 contents.append('const char kSourceOf%s[] = {\n %s\n};\n\n' %
( | |
54 class_name, ', '.join(hex_values))) | |
55 contents.append('struct %s {' % source_name) | 47 contents.append('struct %s {' % source_name) |
56 contents.append(""" | 48 contents.append(""" |
57 const char* scriptClassName; | 49 const char* scriptClassName; |
58 const char* className; | 50 const char* className; |
59 """) | 51 const unsigned char* source; |
60 if options.for_testing: | 52 size_t size; |
61 contents.append(""" | |
62 const char* source; | |
63 size_t size;""") | |
64 else: | |
65 contents.append('const char* resourceFile;') | |
66 contents.append(""" | |
67 }; | 53 }; |
68 | 54 |
69 """) | 55 """) |
70 | |
71 contents.append('struct %s k%s[] = {\n' % (source_name, source_name)) | 56 contents.append('struct %s k%s[] = {\n' % (source_name, source_name)) |
72 for input_filename in input_filenames: | 57 for input_filename in input_filenames: |
73 script_class_name, ext = os.path.splitext(os.path.basename(input_filenam
e)) | 58 script_class_name, ext = os.path.splitext(os.path.basename(input_filenam
e)) |
74 class_name = extract_partial_interface_name(input_filename) or script_cl
ass_name | 59 class_name = extract_partial_interface_name(input_filename) or script_cl
ass_name |
75 if options.for_testing: | 60 contents.append(' { "%s", "%s", kSourceOf%s, sizeof(kSourceOf%s) },\n
' % (script_class_name, class_name, script_class_name, script_class_name)) |
76 contents.append(' { "%s", "%s", kSourceOf%s, sizeof(kSourceOf%s)
},\n' % (script_class_name, class_name, script_class_name, script_class_name)) | |
77 else: | |
78 contents.append(' { "%s", "%s", "%s.js" },\n' % (script_class_nam
e, class_name, script_class_name)) | |
79 contents.append('};\n') | 61 contents.append('};\n') |
80 contents.append('#endif // %s_h\n' % source_name) | 62 |
81 with open(output_filename, 'w') as output_file: | 63 with open(output_filename, 'w') as output_file: |
82 output_file.write("".join(contents)) | 64 output_file.write("".join(contents)) |
83 | 65 |
84 | 66 |
85 if __name__ == '__main__': | 67 if __name__ == '__main__': |
86 sys.exit(main()) | 68 sys.exit(main()) |
OLD | NEW |