OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 # | 5 # |
6 # This python script creates a source path mapping in a C++ source file from | 6 # This python script creates a source path mapping in a C++ source file from |
7 # a C++ source template and list of dart library files. | 7 # a C++ source template and list of dart library files. |
8 | 8 |
9 import os | 9 import os |
10 import sys | 10 import sys |
11 | 11 |
12 from os.path import join | 12 from os.path import join |
13 from optparse import OptionParser | 13 from optparse import OptionParser |
14 | 14 |
15 | 15 |
| 16 def makeString(input_file): |
| 17 result = '"' |
| 18 fileHandle = open(input_file, 'rb') |
| 19 lineCounter = 0 |
| 20 for byte in fileHandle.read(): |
| 21 result += '\\x%02x' % ord(byte) |
| 22 lineCounter += 1 |
| 23 if lineCounter == 19: |
| 24 result += '"\n "' |
| 25 lineCounter = 0 |
| 26 result += '"' |
| 27 return result |
| 28 |
| 29 |
16 def makeFile(output_file, input_cc_file, include, var_name, lib_name, in_files): | 30 def makeFile(output_file, input_cc_file, include, var_name, lib_name, in_files): |
17 part_index = [ ] | 31 part_index = [ ] |
18 bootstrap_cc_text = open(input_cc_file).read() | 32 bootstrap_cc_text = open(input_cc_file).read() |
19 bootstrap_cc_text = bootstrap_cc_text.replace("{{INCLUDE}}", include) | 33 bootstrap_cc_text = bootstrap_cc_text.replace("{{INCLUDE}}", include) |
20 bootstrap_cc_text = bootstrap_cc_text.replace("{{VAR_NAME}}", var_name) | 34 bootstrap_cc_text = bootstrap_cc_text.replace("{{VAR_NAME}}", var_name) |
21 main_file_found = False | 35 main_file_found = False |
22 for string_file in in_files: | 36 for string_file in in_files: |
23 if string_file.endswith('.dart'): | 37 if string_file.endswith('.dart'): |
24 if (not main_file_found): | 38 if (not main_file_found): |
25 inpt = open(string_file, 'r') | 39 inpt = open(string_file, 'r') |
26 for line in inpt: | 40 for line in inpt: |
27 # File with library tag is the main file. | 41 # File with library tag is the main file. |
28 if line.startswith('library '): | 42 if line.startswith('library '): |
29 main_file_found = True | 43 main_file_found = True |
30 bootstrap_cc_text = bootstrap_cc_text.replace( | 44 bootstrap_cc_text = bootstrap_cc_text.replace( |
31 "{{LIBRARY_SOURCE_MAP}}", | 45 "{{LIBRARY_SOURCE_MAP}}", |
32 ' "' + lib_name + '", "' + | 46 ' "' + lib_name + '",\n "' + |
33 os.path.abspath(string_file).replace('\\', '\\\\') + '", \n') | 47 os.path.abspath(string_file).replace('\\', '\\\\') + '",\n' + |
| 48 ' ' + makeString(string_file) + ',\n') |
34 inpt.close() | 49 inpt.close() |
35 if (main_file_found): | 50 if (main_file_found): |
36 continue | 51 continue |
37 part_index.append(' "' + | 52 part_index.append(' "' + |
38 os.path.basename(string_file).replace('\\', '\\\\') + '", ') | 53 os.path.basename(string_file).replace('\\', '\\\\') + '",\n') |
39 part_index.append('"' + | 54 part_index.append(' "' + |
40 os.path.abspath(string_file).replace('\\', '\\\\') + '", \n') | 55 os.path.abspath(string_file).replace('\\', '\\\\') + '",\n') |
| 56 part_index.append(' ' + makeString(string_file) + ',\n\n') |
41 bootstrap_cc_text = bootstrap_cc_text.replace("{{LIBRARY_SOURCE_MAP}}", '') | 57 bootstrap_cc_text = bootstrap_cc_text.replace("{{LIBRARY_SOURCE_MAP}}", '') |
42 bootstrap_cc_text = bootstrap_cc_text.replace("{{PART_SOURCE_MAP}}", | 58 bootstrap_cc_text = bootstrap_cc_text.replace("{{PART_SOURCE_MAP}}", |
43 ''.join(part_index)) | 59 ''.join(part_index)) |
44 open(output_file, 'w').write(bootstrap_cc_text) | 60 open(output_file, 'w').write(bootstrap_cc_text) |
45 return True | 61 return True |
46 | 62 |
47 | 63 |
48 def main(args): | 64 def main(args): |
49 try: | 65 try: |
50 # Parse input. | 66 # Parse input. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 | 115 |
100 return 0 | 116 return 0 |
101 except Exception, inst: | 117 except Exception, inst: |
102 sys.stderr.write('gen_library_src_paths.py exception\n') | 118 sys.stderr.write('gen_library_src_paths.py exception\n') |
103 sys.stderr.write(str(inst)) | 119 sys.stderr.write(str(inst)) |
104 sys.stderr.write('\n') | 120 sys.stderr.write('\n') |
105 return -1 | 121 return -1 |
106 | 122 |
107 if __name__ == '__main__': | 123 if __name__ == '__main__': |
108 sys.exit(main(sys.argv)) | 124 sys.exit(main(sys.argv)) |
OLD | NEW |