| OLD | NEW |
| (Empty) | |
| 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 |
| 3 # BSD-style license that can be found in the LICENSE file. |
| 4 # |
| 5 # This python script creates string literals in a C++ source file from a C++ |
| 6 # source template and one or more resource files. |
| 7 |
| 8 import os |
| 9 import sys |
| 10 from os.path import join |
| 11 import time |
| 12 from optparse import OptionParser |
| 13 import re |
| 14 from datetime import date |
| 15 |
| 16 def makeResources(root_dir, input_files): |
| 17 result = '' |
| 18 resources = [] |
| 19 |
| 20 # Write each file's contents as a byte string constant. |
| 21 for resource_file in input_files: |
| 22 if root_dir and resource_file.startswith(root_dir): |
| 23 resource_file_name = resource_file[ len(root_dir) : ] |
| 24 else: |
| 25 resource_file_name = resource_file |
| 26 resource_url = '/%s' % resource_file_name |
| 27 result += '// %s\n' % resource_file |
| 28 result += 'const char ' |
| 29 resource_name = re.sub(r'(/|\.)', '_', resource_file_name) + '_' |
| 30 result += resource_name |
| 31 result += '[] = {\n ' |
| 32 fileHandle = open(resource_file, 'rb') |
| 33 lineCounter = 0 |
| 34 for byte in fileHandle.read(): |
| 35 result += ' %d,' % ord(byte) |
| 36 lineCounter += 1 |
| 37 if lineCounter == 10: |
| 38 result += '\n ' |
| 39 lineCounter = 0 |
| 40 if lineCounter != 0: |
| 41 result += '\n ' |
| 42 result += ' 0\n};\n\n' |
| 43 resources.append( |
| 44 (resource_url, resource_name, os.stat(resource_file).st_size) ); |
| 45 |
| 46 # Write the resource table. |
| 47 result += 'Resources::resource_map_entry Resources::builtin_resources_[] = ' |
| 48 result += '{\n' |
| 49 for res in resources: |
| 50 result += ' { "%s", %s, %d },\n' % res |
| 51 result += '};\n\n' |
| 52 result += 'const intptr_t Resources::builtin_resources_count_ ' |
| 53 result += '= %d;\n' % len(resources) |
| 54 return result |
| 55 |
| 56 |
| 57 def makeFile(output_file, root_dir, input_files): |
| 58 cc_text = ''' |
| 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 |
| 61 // BSD-style license that can be found in the LICENSE file. |
| 62 |
| 63 ''' % date.today().year |
| 64 cc_text += '#include "bin/resources.h"\n\n' |
| 65 cc_text += makeResources(root_dir, input_files) |
| 66 open(output_file, 'w').write(cc_text) |
| 67 return True |
| 68 |
| 69 |
| 70 def main(args): |
| 71 try: |
| 72 # Parse input. |
| 73 parser = OptionParser() |
| 74 parser.add_option("--output", |
| 75 action="store", type="string", |
| 76 help="output file name") |
| 77 parser.add_option("--root_prefix", |
| 78 action="store", type="string", |
| 79 help="root directory for resources") |
| 80 |
| 81 (options, args) = parser.parse_args() |
| 82 if not options.output: |
| 83 sys.stderr.write('--output not specified\n') |
| 84 return -1 |
| 85 if len(args) == 0: |
| 86 sys.stderr.write('No input files specified\n') |
| 87 return -1 |
| 88 |
| 89 files = [ ] |
| 90 for arg in args: |
| 91 files.append(arg) |
| 92 |
| 93 if not makeFile(options.output, options.root_prefix, files): |
| 94 return -1 |
| 95 |
| 96 return 0 |
| 97 except Exception, inst: |
| 98 sys.stderr.write('create_resources.py exception\n') |
| 99 sys.stderr.write(str(inst)) |
| 100 sys.stderr.write('\n') |
| 101 return -1 |
| 102 |
| 103 if __name__ == '__main__': |
| 104 sys.exit(main(sys.argv)) |
| OLD | NEW |