| 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, table_name): | 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 |
| 27 result += '// %s\n' % resource_file | 27 result += '// %s\n' % resource_file |
| 28 result += 'const char ' | 28 result += 'const char ' |
| 29 resource_name = re.sub(r'(/|\.|-)', '_', resource_file_name) + '_' | 29 resource_name = re.sub(r'(/|\.|-|\\)', '_', resource_file_name) + '_' |
| 30 result += resource_name | 30 result += resource_name |
| 31 result += '[] = {\n ' | 31 result += '[] = {\n ' |
| 32 fileHandle = open(resource_file, 'rb') | 32 fileHandle = open(resource_file, 'rb') |
| 33 lineCounter = 0 | 33 lineCounter = 0 |
| 34 for byte in fileHandle.read(): | 34 for byte in fileHandle.read(): |
| 35 result += r" '\x%02x'," % ord(byte) | 35 result += r" '\x%02x'," % ord(byte) |
| 36 lineCounter += 1 | 36 lineCounter += 1 |
| 37 if lineCounter == 10: | 37 if lineCounter == 10: |
| 38 result += '\n ' | 38 result += '\n ' |
| 39 lineCounter = 0 | 39 lineCounter = 0 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 123 |
| 124 return 0 | 124 return 0 |
| 125 except Exception, inst: | 125 except Exception, inst: |
| 126 sys.stderr.write('create_resources.py exception\n') | 126 sys.stderr.write('create_resources.py exception\n') |
| 127 sys.stderr.write(str(inst)) | 127 sys.stderr.write(str(inst)) |
| 128 sys.stderr.write('\n') | 128 sys.stderr.write('\n') |
| 129 return -1 | 129 return -1 |
| 130 | 130 |
| 131 if __name__ == '__main__': | 131 if __name__ == '__main__': |
| 132 sys.exit(main(sys.argv)) | 132 sys.exit(main(sys.argv)) |
| OLD | NEW |