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 """ |
(...skipping 14 matching lines...) Expand all Loading... | |
25 if os.path.basename(basename) == 'PrivateScriptRunner': | 25 if os.path.basename(basename) == 'PrivateScriptRunner': |
26 return None | 26 return None |
27 idl_filename = basename + '.idl' | 27 idl_filename = basename + '.idl' |
28 with open(idl_filename) as f: | 28 with open(idl_filename) as f: |
29 contents = f.read() | 29 contents = f.read() |
30 match = re.search(r'partial\s+interface\s+(\w+)\s*{', contents) | 30 match = re.search(r'partial\s+interface\s+(\w+)\s*{', contents) |
31 return match and match.group(1) | 31 return match and match.group(1) |
32 | 32 |
33 | 33 |
34 def main(): | 34 def main(): |
35 output_filename = sys.argv[1] | 35 for_testing = False |
36 input_filenames = sys.argv[2:] | 36 if '--for-testing' in sys.argv: |
jochen (gone - plz use gerrit)
2014/09/10 07:35:47
maybe use optparse?
| |
37 for_testing = True | |
38 output_filename = sys.argv[2] | |
39 input_filenames = sys.argv[3:] | |
40 else: | |
41 output_filename = sys.argv[1] | |
42 input_filenames = sys.argv[2:] | |
43 | |
37 source_name, ext = os.path.splitext(os.path.basename(output_filename)) | 44 source_name, ext = os.path.splitext(os.path.basename(output_filename)) |
38 | 45 |
39 contents = [] | 46 contents = [] |
40 for input_filename in input_filenames: | 47 contents.append('#ifndef %s_h\n' % source_name) |
41 class_name, ext = os.path.splitext(os.path.basename(input_filename)) | 48 contents.append('#define %s_h\n' % source_name) |
42 with open(input_filename) as input_file: | 49 if for_testing: |
43 input_text = input_file.read() | 50 for input_filename in input_filenames: |
44 hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_text] | 51 class_name, ext = os.path.splitext(os.path.basename(input_filename)) |
45 contents.append('const unsigned char kSourceOf%s[] = {\n %s\n};\n \n' % ( | 52 with open(input_filename) as input_file: |
46 class_name, ', '.join(hex_values))) | 53 input_text = input_file.read() |
54 hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_te xt] | |
55 contents.append('const char kSourceOf%s[] = {\n %s\n};\n\n' % ( | |
56 class_name, ', '.join(hex_values))) | |
47 contents.append('struct %s {' % source_name) | 57 contents.append('struct %s {' % source_name) |
48 contents.append(""" | 58 contents.append(""" |
49 const char* scriptClassName; | 59 const char* scriptClassName; |
50 const char* className; | 60 const char* className; |
51 const unsigned char* source; | 61 """) |
52 size_t size; | 62 if for_testing: |
63 contents.append(""" | |
64 const char* source; | |
65 size_t size;""") | |
66 else: | |
67 contents.append('const char* resourceFile;') | |
68 contents.append(""" | |
53 }; | 69 }; |
54 | 70 |
55 """) | 71 """) |
72 | |
56 contents.append('struct %s k%s[] = {\n' % (source_name, source_name)) | 73 contents.append('struct %s k%s[] = {\n' % (source_name, source_name)) |
57 for input_filename in input_filenames: | 74 for input_filename in input_filenames: |
58 script_class_name, ext = os.path.splitext(os.path.basename(input_filenam e)) | 75 script_class_name, ext = os.path.splitext(os.path.basename(input_filenam e)) |
59 class_name = extract_partial_interface_name(input_filename) or script_cl ass_name | 76 class_name = extract_partial_interface_name(input_filename) or script_cl ass_name |
60 contents.append(' { "%s", "%s", kSourceOf%s, sizeof(kSourceOf%s) },\n ' % (script_class_name, class_name, script_class_name, script_class_name)) | 77 if for_testing: |
78 contents.append(' { "%s", "%s", kSourceOf%s, sizeof(kSourceOf%s) },\n' % (script_class_name, class_name, script_class_name, script_class_name)) | |
79 else: | |
80 contents.append(' { "%s", "%s", "%s.js" },\n' % (script_class_nam e, class_name, script_class_name)) | |
61 contents.append('};\n') | 81 contents.append('};\n') |
62 | 82 contents.append('#endif // %s_h' % source_name) |
63 with open(output_filename, 'w') as output_file: | 83 with open(output_filename, 'w') as output_file: |
64 output_file.write("".join(contents)) | 84 output_file.write("".join(contents)) |
65 | 85 |
66 | 86 |
67 if __name__ == '__main__': | 87 if __name__ == '__main__': |
68 sys.exit(main()) | 88 sys.exit(main()) |
OLD | NEW |