| 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 sys | 14 import sys |
| 14 | 15 |
| 15 | 16 |
| 17 def extract_partial_interface_name(filename): |
| 18 basename, ext = os.path.splitext(filename) |
| 19 assert ext == '.js' |
| 20 if os.path.basename(basename) == 'PrivateScriptRunner': |
| 21 return None |
| 22 idl_filename = basename + '.idl' |
| 23 with open(idl_filename) as f: |
| 24 contents = f.read() |
| 25 match = re.search(r'partial\s+interface\s+(\w+)\s*{', contents) |
| 26 return match and match.group(1) |
| 27 |
| 28 |
| 16 def main(): | 29 def main(): |
| 17 output_filename = sys.argv[1] | 30 output_filename = sys.argv[1] |
| 18 input_filenames = sys.argv[2:] | 31 input_filenames = sys.argv[2:] |
| 19 source_name, ext = os.path.splitext(os.path.basename(output_filename)) | 32 source_name, ext = os.path.splitext(os.path.basename(output_filename)) |
| 20 | 33 |
| 21 contents = [] | 34 contents = [] |
| 22 for input_filename in input_filenames: | 35 for input_filename in input_filenames: |
| 23 class_name, ext = os.path.splitext(os.path.basename(input_filename)) | 36 class_name, ext = os.path.splitext(os.path.basename(input_filename)) |
| 24 with open(input_filename) as input_file: | 37 with open(input_filename) as input_file: |
| 25 input_text = input_file.read() | 38 input_text = input_file.read() |
| 26 hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_text] | 39 hex_values = ['0x{0:02x}'.format(ord(char)) for char in input_text] |
| 27 contents.append('const unsigned char kSourceOf%s[] = {\n %s\n};\n
' % ( | 40 contents.append('const unsigned char kSourceOf%s[] = {\n %s\n};\n
\n' % ( |
| 28 class_name, ', '.join(hex_values))) | 41 class_name, ', '.join(hex_values))) |
| 29 contents.append('struct %s {' % source_name) | 42 contents.append('struct %s {' % source_name) |
| 30 contents.append(""" | 43 contents.append(""" |
| 31 const char* name; | 44 const char* className; |
| 45 const char* dependencyClassName; |
| 32 const unsigned char* source; | 46 const unsigned char* source; |
| 33 size_t size; | 47 size_t size; |
| 34 }; | 48 }; |
| 35 | 49 |
| 36 """) | 50 """) |
| 37 contents.append('struct %s k%s[] = {\n' % (source_name, source_name)) | 51 contents.append('struct %s k%s[] = {\n' % (source_name, source_name)) |
| 38 for input_filename in input_filenames: | 52 for input_filename in input_filenames: |
| 39 class_name, ext = os.path.splitext(os.path.basename(input_filename)) | 53 class_name, ext = os.path.splitext(os.path.basename(input_filename)) |
| 40 contents.append(' { "%s", kSourceOf%s, sizeof(kSourceOf%s) },\n' % (c
lass_name, class_name, class_name)) | 54 dependency_class_name = extract_partial_interface_name(input_filename) o
r class_name |
| 55 contents.append(' { "%s", "%s", kSourceOf%s, sizeof(kSourceOf%s) },\n
' % (class_name, dependency_class_name, class_name, class_name)) |
| 41 contents.append('};\n') | 56 contents.append('};\n') |
| 42 | 57 |
| 43 with open(output_filename, 'w') as output_file: | 58 with open(output_filename, 'w') as output_file: |
| 44 output_file.write("".join(contents)) | 59 output_file.write("".join(contents)) |
| 45 | 60 |
| 46 | 61 |
| 47 if __name__ == '__main__': | 62 if __name__ == '__main__': |
| 48 sys.exit(main()) | 63 sys.exit(main()) |
| OLD | NEW |