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 os | 12 import os |
13 import re | 13 import re |
14 import sys | 14 import sys |
15 | 15 |
16 | 16 |
17 RE_INCLUDE = re.compile('<include[^>]+src=[\'"]([^>]*)[\'"]>') | |
18 | |
19 # 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. |
20 # 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. |
21 # Otherwise, this method returns None. | 19 # Otherwise, this method returns None. |
22 def extract_partial_interface_name(filename): | 20 def extract_partial_interface_name(filename): |
23 basename, ext = os.path.splitext(filename) | 21 basename, ext = os.path.splitext(filename) |
24 assert ext == '.js' | 22 assert ext == '.js' |
25 # PrivateScriptRunner.js is a special JS script to control private scripts, | 23 # PrivateScriptRunner.js is a special JS script to control private scripts, |
26 # and doesn't have a corresponding IDL file. | 24 # and doesn't have a corresponding IDL file. |
27 if os.path.basename(basename) == 'PrivateScriptRunner': | 25 if os.path.basename(basename) == 'PrivateScriptRunner': |
28 return None | 26 return None |
29 idl_filename = basename + '.idl' | 27 idl_filename = basename + '.idl' |
30 with open(idl_filename) as f: | 28 with open(idl_filename) as f: |
31 contents = f.read() | 29 contents = f.read() |
32 match = re.search(r'partial\s+interface\s+(\w+)\s*{', contents) | 30 match = re.search(r'partial\s+interface\s+(\w+)\s*{', contents) |
33 return match and match.group(1) | 31 return match and match.group(1) |
34 | 32 |
35 | 33 |
36 # Expand <include src="file_name"> directives | |
37 # FIXME: Expansion of include directive will be done by GRD resources building | |
38 # system in the future. | |
39 def process_input_file(filename): | |
40 result_text = '' | |
41 dirname = os.path.dirname(filename) | |
42 with open(filename) as input_file: | |
43 for line in input_file.readlines(): | |
44 match = re.search(RE_INCLUDE, line) | |
45 if match: | |
46 result_text += process_input_file(os.path.join(dirname, match.gr
oup(1))) | |
47 else: | |
48 result_text += line | |
49 return result_text | |
50 | |
51 def main(): | 34 def main(): |
52 output_filename = sys.argv[1] | 35 output_filename = sys.argv[1] |
53 input_filenames = sys.argv[2:] | 36 input_filenames = sys.argv[2:] |
54 source_name, ext = os.path.splitext(os.path.basename(output_filename)) | 37 source_name, ext = os.path.splitext(os.path.basename(output_filename)) |
55 | 38 |
56 contents = [] | 39 contents = [] |
57 for input_filename in input_filenames: | 40 for input_filename in input_filenames: |
58 class_name, ext = os.path.splitext(os.path.basename(input_filename)) | 41 class_name, ext = os.path.splitext(os.path.basename(input_filename)) |
59 input_text = process_input_file(input_filename) | 42 with open(input_filename) as input_file: |
60 hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_text] | 43 input_text = input_file.read() |
61 contents.append('const unsigned char kSourceOf%s[] = {\n %s\n};\n\n'
% ( | 44 hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_text] |
62 class_name, ', '.join(hex_values))) | 45 contents.append('const unsigned char kSourceOf%s[] = {\n %s\n};\n
\n' % ( |
| 46 class_name, ', '.join(hex_values))) |
63 contents.append('struct %s {' % source_name) | 47 contents.append('struct %s {' % source_name) |
64 contents.append(""" | 48 contents.append(""" |
65 const char* scriptClassName; | 49 const char* scriptClassName; |
66 const char* className; | 50 const char* className; |
67 const unsigned char* source; | 51 const unsigned char* source; |
68 size_t size; | 52 size_t size; |
69 }; | 53 }; |
70 | 54 |
71 """) | 55 """) |
72 contents.append('struct %s k%s[] = {\n' % (source_name, source_name)) | 56 contents.append('struct %s k%s[] = {\n' % (source_name, source_name)) |
73 for input_filename in input_filenames: | 57 for input_filename in input_filenames: |
74 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)) |
75 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 |
76 contents.append(' { "%s", "%s", kSourceOf%s, sizeof(kSourceOf%s) },\n
' % (script_class_name, class_name, script_class_name, script_class_name)) | 60 contents.append(' { "%s", "%s", kSourceOf%s, sizeof(kSourceOf%s) },\n
' % (script_class_name, class_name, script_class_name, script_class_name)) |
77 contents.append('};\n') | 61 contents.append('};\n') |
78 | 62 |
79 with open(output_filename, 'w') as output_file: | 63 with open(output_filename, 'w') as output_file: |
80 output_file.write("".join(contents)) | 64 output_file.write("".join(contents)) |
81 | 65 |
82 | 66 |
83 if __name__ == '__main__': | 67 if __name__ == '__main__': |
84 sys.exit(main()) | 68 sys.exit(main()) |
OLD | NEW |