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