| 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, package_dir, third_party_dir, rebase, input_files): | 16 def makeResources(root_dir, input_files): |
| 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 elif package_dir and resource_file.startswith(package_dir): | |
| 25 resource_file_name = os.path.join(rebase, | |
| 26 resource_file[ len(package_dir) : ]) | |
| 27 elif third_party_dir and resource_file.startswith(third_party_dir): | |
| 28 resource_file_name = os.path.join(rebase, | |
| 29 resource_file[ len(third_party_dir) : ]) | |
| 30 else: | 24 else: |
| 31 resource_file_name = resource_file | 25 resource_file_name = resource_file |
| 32 | |
| 33 resource_url = '/%s' % resource_file_name | 26 resource_url = '/%s' % resource_file_name |
| 34 result += '// %s\n' % resource_file | 27 result += '// %s\n' % resource_file |
| 35 result += 'const char ' | 28 result += 'const char ' |
| 36 resource_name = re.sub(r'(/|\.|-)', '_', resource_file_name) + '_' | 29 resource_name = re.sub(r'(/|\.|-)', '_', resource_file_name) + '_' |
| 37 result += resource_name | 30 result += resource_name |
| 38 result += '[] = {\n ' | 31 result += '[] = {\n ' |
| 39 fileHandle = open(resource_file, 'rb') | 32 fileHandle = open(resource_file, 'rb') |
| 40 lineCounter = 0 | 33 lineCounter = 0 |
| 41 for byte in fileHandle.read(): | 34 for byte in fileHandle.read(): |
| 42 result += ' %d,' % ord(byte) | 35 result += ' %d,' % ord(byte) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 54 result += 'Resources::resource_map_entry Resources::builtin_resources_[] = ' | 47 result += 'Resources::resource_map_entry Resources::builtin_resources_[] = ' |
| 55 result += '{\n' | 48 result += '{\n' |
| 56 for res in resources: | 49 for res in resources: |
| 57 result += ' { "%s", %s, %d },\n' % res | 50 result += ' { "%s", %s, %d },\n' % res |
| 58 result += '};\n\n' | 51 result += '};\n\n' |
| 59 result += 'const intptr_t Resources::builtin_resources_count_ ' | 52 result += 'const intptr_t Resources::builtin_resources_count_ ' |
| 60 result += '= %d;\n' % len(resources) | 53 result += '= %d;\n' % len(resources) |
| 61 return result | 54 return result |
| 62 | 55 |
| 63 | 56 |
| 64 def makeFile(output_file, root_dir, package_dir, third_party_dir, rebase_dir, | 57 def makeFile(output_file, root_dir, input_files): |
| 65 input_files): | |
| 66 cc_text = ''' | 58 cc_text = ''' |
| 67 // 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 |
| 68 // 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 |
| 69 // BSD-style license that can be found in the LICENSE file. | 61 // BSD-style license that can be found in the LICENSE file. |
| 70 | 62 |
| 71 ''' % date.today().year | 63 ''' % date.today().year |
| 72 cc_text += '#include "bin/resources.h"\n\n' | 64 cc_text += '#include "bin/resources.h"\n\n' |
| 73 cc_text += 'namespace dart {\n' | 65 cc_text += 'namespace dart {\n' |
| 74 cc_text += 'namespace bin {\n' | 66 cc_text += 'namespace bin {\n' |
| 75 cc_text += makeResources(root_dir, package_dir, third_party_dir, rebase_dir, | 67 cc_text += makeResources(root_dir, input_files) |
| 76 input_files) | |
| 77 cc_text += '} // namespace bin\n} // namespace dart\n' | 68 cc_text += '} // namespace bin\n} // namespace dart\n' |
| 78 open(output_file, 'w').write(cc_text) | 69 open(output_file, 'w').write(cc_text) |
| 79 return True | 70 return True |
| 80 | 71 |
| 81 | 72 |
| 82 def main(args): | 73 def main(args): |
| 83 try: | 74 try: |
| 84 # Parse input. | 75 # Parse input. |
| 85 parser = OptionParser() | 76 parser = OptionParser() |
| 86 parser.add_option("--output", | 77 parser.add_option("--output", |
| 87 action="store", type="string", | 78 action="store", type="string", |
| 88 help="output file name") | 79 help="output file name") |
| 89 parser.add_option("--root_prefix", | 80 parser.add_option("--root_prefix", |
| 90 action="store", type="string", | 81 action="store", type="string", |
| 91 help="root directory for resources") | 82 help="root directory for resources") |
| 92 parser.add_option("--package_dir", | |
| 93 action="store", type="string", | |
| 94 help="root directory for package resources") | |
| 95 parser.add_option("--third_party_dir", | |
| 96 action="store", type="string", | |
| 97 help="root directory for third party resources") | |
| 98 parser.add_option("--rebase", | |
| 99 action="store", type="string", | |
| 100 help="base directory for package/third_party resources") | |
| 101 | 83 |
| 102 (options, args) = parser.parse_args() | 84 (options, args) = parser.parse_args() |
| 103 if not options.output: | 85 if not options.output: |
| 104 sys.stderr.write('--output not specified\n') | 86 sys.stderr.write('--output not specified\n') |
| 105 return -1 | 87 return -1 |
| 106 if len(args) == 0: | 88 if len(args) == 0: |
| 107 sys.stderr.write('No input files specified\n') | 89 sys.stderr.write('No input files specified\n') |
| 108 return -1 | 90 return -1 |
| 109 | 91 |
| 110 files = [ ] | 92 files = [ ] |
| 111 for arg in args: | 93 for arg in args: |
| 112 files.append(arg) | 94 files.append(arg) |
| 113 | 95 |
| 114 if not makeFile(options.output, options.root_prefix, options.package_dir, | 96 if not makeFile(options.output, options.root_prefix, files): |
| 115 options.third_party_dir, options.rebase, files): | |
| 116 return -1 | 97 return -1 |
| 117 | 98 |
| 118 return 0 | 99 return 0 |
| 119 except Exception, inst: | 100 except Exception, inst: |
| 120 sys.stderr.write('create_resources.py exception\n') | 101 sys.stderr.write('create_resources.py exception\n') |
| 121 sys.stderr.write(str(inst)) | 102 sys.stderr.write(str(inst)) |
| 122 sys.stderr.write('\n') | 103 sys.stderr.write('\n') |
| 123 return -1 | 104 return -1 |
| 124 | 105 |
| 125 if __name__ == '__main__': | 106 if __name__ == '__main__': |
| 126 sys.exit(main(sys.argv)) | 107 sys.exit(main(sys.argv)) |
| OLD | NEW |