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.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 sys | 13 import sys |
14 | 14 |
15 | 15 |
16 def main(): | 16 def main(): |
17 output_filename = sys.argv[1] | 17 output_filename = sys.argv[1] |
18 input_filenames = sys.argv[2:] | 18 input_filenames = sys.argv[2:] |
| 19 source_name, ext = os.path.splitext(os.path.basename(output_filename)) |
| 20 |
19 contents = [] | 21 contents = [] |
20 for input_filename in input_filenames: | 22 for input_filename in input_filenames: |
21 class_name, ext = os.path.splitext(os.path.basename(input_filename)) | 23 class_name, ext = os.path.splitext(os.path.basename(input_filename)) |
22 with open(input_filename) as input_file: | 24 with open(input_filename) as input_file: |
23 input_text = input_file.read() | 25 input_text = input_file.read() |
24 hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_text] | 26 hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_text] |
25 contents.append('const unsigned char kSourceOf%s[] = {\n %s\n};\n
' % ( | 27 contents.append('const unsigned char kSourceOf%s[] = {\n %s\n};\n
' % ( |
26 class_name, ', '.join(hex_values))) | 28 class_name, ', '.join(hex_values))) |
| 29 contents.append('struct %s {' % source_name) |
27 contents.append(""" | 30 contents.append(""" |
28 struct PrivateScriptSources { | |
29 const char* name; | 31 const char* name; |
30 const unsigned char* source; | 32 const unsigned char* source; |
31 size_t size; | 33 size_t size; |
32 }; | 34 }; |
33 | 35 |
34 """) | 36 """) |
35 contents.append('struct PrivateScriptSources kPrivateScriptSources[] = {\n') | 37 contents.append('struct %s k%s[] = {\n' % (source_name, source_name)) |
36 for input_filename in input_filenames: | 38 for input_filename in input_filenames: |
37 class_name, ext = os.path.splitext(os.path.basename(input_filename)) | 39 class_name, ext = os.path.splitext(os.path.basename(input_filename)) |
38 contents.append(' { "%s", kSourceOf%s, sizeof(kSourceOf%s) },\n' % (c
lass_name, class_name, class_name)) | 40 contents.append(' { "%s", kSourceOf%s, sizeof(kSourceOf%s) },\n' % (c
lass_name, class_name, class_name)) |
39 contents.append('};\n') | 41 contents.append('};\n') |
40 | 42 |
41 with open(output_filename, 'w') as output_file: | 43 with open(output_filename, 'w') as output_file: |
42 output_file.write("".join(contents)) | 44 output_file.write("".join(contents)) |
43 | 45 |
44 | 46 |
45 if __name__ == '__main__': | 47 if __name__ == '__main__': |
46 sys.exit(main()) | 48 sys.exit(main()) |
OLD | NEW |