| 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): |
| 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 += ' %d,' % ord(byte) | 35 result += ' %d,' % 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 98 |
| 99 return 0 | 99 return 0 |
| 100 except Exception, inst: | 100 except Exception, inst: |
| 101 sys.stderr.write('create_resources.py exception\n') | 101 sys.stderr.write('create_resources.py exception\n') |
| 102 sys.stderr.write(str(inst)) | 102 sys.stderr.write(str(inst)) |
| 103 sys.stderr.write('\n') | 103 sys.stderr.write('\n') |
| 104 return -1 | 104 return -1 |
| 105 | 105 |
| 106 if __name__ == '__main__': | 106 if __name__ == '__main__': |
| 107 sys.exit(main(sys.argv)) | 107 sys.exit(main(sys.argv)) |
| OLD | NEW |