| OLD | NEW |
| 1 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 # for details. All rights reserved. Use of this source code is governed by a | 2 # for details. All rights reserved. Use of this source code is governed by a |
| 3 # BSD-style license that can be found in the LICENSE file. | 3 # BSD-style license that can be found in the LICENSE file. |
| 4 # | 4 # |
| 5 # This python script creates string literals in a C++ source file from a C++ | 5 # This python script creates string literals in a C++ source file from a C++ |
| 6 # source template and one or more resource files. | 6 # source template and one or more resource files. |
| 7 | 7 |
| 8 import os | 8 import os |
| 9 import sys | 9 import sys |
| 10 from os.path import join | 10 from os.path import join |
| 11 import time | 11 import time |
| 12 from optparse import OptionParser | 12 from optparse import OptionParser |
| 13 import re | 13 import re |
| 14 from datetime import date | 14 from datetime import date |
| 15 | 15 |
| 16 def makeResources(root_dir, input_files): | 16 def makeResources(root_dir, input_files, table_name): |
| 17 result = '' | 17 result = '' |
| 18 resources = [] | 18 resources = [] |
| 19 | 19 |
| 20 # Write each file's contents as a byte string constant. | 20 # Write each file's contents as a byte string constant. |
| 21 for resource_file in input_files: | 21 for resource_file in input_files: |
| 22 if root_dir and resource_file.startswith(root_dir): | 22 if root_dir and resource_file.startswith(root_dir): |
| 23 resource_file_name = resource_file[ len(root_dir) : ] | 23 resource_file_name = resource_file[ len(root_dir) : ] |
| 24 else: | 24 else: |
| 25 resource_file_name = resource_file | 25 resource_file_name = resource_file |
| 26 resource_url = '/%s' % resource_file_name | 26 resource_url = '/%s' % resource_file_name |
| (...skipping 10 matching lines...) Expand all Loading... |
| 37 if lineCounter == 10: | 37 if lineCounter == 10: |
| 38 result += '\n ' | 38 result += '\n ' |
| 39 lineCounter = 0 | 39 lineCounter = 0 |
| 40 if lineCounter != 0: | 40 if lineCounter != 0: |
| 41 result += '\n ' | 41 result += '\n ' |
| 42 result += ' 0\n};\n\n' | 42 result += ' 0\n};\n\n' |
| 43 resources.append( | 43 resources.append( |
| 44 (resource_url, resource_name, os.stat(resource_file).st_size) ); | 44 (resource_url, resource_name, os.stat(resource_file).st_size) ); |
| 45 | 45 |
| 46 # Write the resource table. | 46 # Write the resource table. |
| 47 result += 'Resources::resource_map_entry Resources::builtin_resources_[] = ' | 47 result += 'ResourcesEntry __%s_resources_[] = ' % table_name |
| 48 result += '{\n' | 48 result += '{\n' |
| 49 for res in resources: | 49 for res in resources: |
| 50 result += ' { "%s", %s, %d },\n' % res | 50 result += ' { "%s", %s, %d },\n' % res |
| 51 result += ' { 0, 0, 0 },\n' |
| 51 result += '};\n\n' | 52 result += '};\n\n' |
| 52 result += 'const intptr_t Resources::builtin_resources_count_ ' | |
| 53 result += '= %d;\n' % len(resources) | |
| 54 return result | 53 return result |
| 55 | 54 |
| 56 | 55 |
| 57 def makeFile(output_file, root_dir, input_files): | 56 def makeFile(output_file, root_dir, input_files, outer_namespace, |
| 57 inner_namespace, table_name): |
| 58 cc_text = ''' | 58 cc_text = ''' |
| 59 // Copyright (c) %d, the Dart project authors. Please see the AUTHORS file | 59 // Copyright (c) %d, the Dart project authors. Please see the AUTHORS file |
| 60 // for details. All rights reserved. Use of this source code is governed by a | 60 // for details. All rights reserved. Use of this source code is governed by a |
| 61 // BSD-style license that can be found in the LICENSE file. | 61 // BSD-style license that can be found in the LICENSE file. |
| 62 | 62 |
| 63 ''' % date.today().year | 63 ''' % date.today().year |
| 64 cc_text += '#include "bin/resources.h"\n\n' | 64 cc_text += 'namespace %s {\n' % outer_namespace |
| 65 cc_text += 'namespace dart {\n' | 65 if inner_namespace != None: |
| 66 cc_text += 'namespace bin {\n' | 66 cc_text += 'namespace %s {\n' % inner_namespace |
| 67 cc_text += makeResources(root_dir, input_files) | 67 cc_text += ''' |
| 68 cc_text += '} // namespace bin\n} // namespace dart\n' | 68 struct ResourcesEntry { |
| 69 const char* path_; |
| 70 const char* resource_; |
| 71 int length_; |
| 72 }; |
| 73 |
| 74 ''' |
| 75 cc_text += makeResources(root_dir, input_files, table_name) |
| 76 cc_text += '\n' |
| 77 if inner_namespace != None: |
| 78 cc_text += '} // namespace %s\n' % inner_namespace |
| 79 cc_text += '} // namespace %s\n' % outer_namespace |
| 69 open(output_file, 'w').write(cc_text) | 80 open(output_file, 'w').write(cc_text) |
| 70 return True | 81 return True |
| 71 | 82 |
| 72 | 83 |
| 73 def main(args): | 84 def main(args): |
| 74 try: | 85 try: |
| 75 # Parse input. | 86 # Parse input. |
| 76 parser = OptionParser() | 87 parser = OptionParser() |
| 77 parser.add_option("--output", | 88 parser.add_option("--output", |
| 78 action="store", type="string", | 89 action="store", type="string", |
| 79 help="output file name") | 90 help="output file name") |
| 80 parser.add_option("--root_prefix", | 91 parser.add_option("--root_prefix", |
| 81 action="store", type="string", | 92 action="store", type="string", |
| 82 help="root directory for resources") | 93 help="root directory for resources") |
| 94 parser.add_option("--outer_namespace", |
| 95 action="store", type="string", |
| 96 help="outer C++ namespace", |
| 97 default="dart") |
| 98 parser.add_option("--inner_namespace", |
| 99 action="store", type="string", |
| 100 help="inner C++ namespace") |
| 101 parser.add_option("--table_name", |
| 102 action="store", type="string", |
| 103 help="name of table") |
| 83 (options, args) = parser.parse_args() | 104 (options, args) = parser.parse_args() |
| 84 if not options.output: | 105 if not options.output: |
| 85 sys.stderr.write('--output not specified\n') | 106 sys.stderr.write('--output not specified\n') |
| 86 return -1 | 107 return -1 |
| 108 if not options.table_name: |
| 109 sys.stderr.write('--table_name not specified\n') |
| 110 return -1 |
| 87 if len(args) == 0: | 111 if len(args) == 0: |
| 88 sys.stderr.write('No input files specified\n') | 112 sys.stderr.write('No input files specified\n') |
| 89 return -1 | 113 return -1 |
| 90 | 114 |
| 91 files = [ ] | 115 files = [ ] |
| 92 for arg in args: | 116 for arg in args: |
| 93 files.append(arg) | 117 files.append(arg) |
| 94 | 118 |
| 95 if not makeFile(options.output, options.root_prefix, files): | 119 if not makeFile(options.output, options.root_prefix, files, |
| 120 options.outer_namespace, options.inner_namespace, |
| 121 options.table_name): |
| 96 return -1 | 122 return -1 |
| 97 | 123 |
| 98 return 0 | 124 return 0 |
| 99 except Exception, inst: | 125 except Exception, inst: |
| 100 sys.stderr.write('create_resources.py exception\n') | 126 sys.stderr.write('create_resources.py exception\n') |
| 101 sys.stderr.write(str(inst)) | 127 sys.stderr.write(str(inst)) |
| 102 sys.stderr.write('\n') | 128 sys.stderr.write('\n') |
| 103 return -1 | 129 return -1 |
| 104 | 130 |
| 105 if __name__ == '__main__': | 131 if __name__ == '__main__': |
| 106 sys.exit(main(sys.argv)) | 132 sys.exit(main(sys.argv)) |
| OLD | NEW |