OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import argparse | 6 import argparse |
7 import collections | 7 import collections |
8 import hashlib | 8 import hashlib |
9 import operator | 9 import operator |
10 import os | 10 import os |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 Args: | 64 Args: |
65 resources_content: The input string to process, contains lines of the form | 65 resources_content: The input string to process, contains lines of the form |
66 "#define NAME INDEX". | 66 "#define NAME INDEX". |
67 | 67 |
68 Returns: | 68 Returns: |
69 A sorted list of |Resource| objects. | 69 A sorted list of |Resource| objects. |
70 """ | 70 """ |
71 resources = [Resource(_HashName(name), name, index) for name, index in | 71 resources = [Resource(_HashName(name), name, index) for name, index in |
72 _GetNameIndexPairsIter(resources_content)] | 72 _GetNameIndexPairsIter(resources_content)] |
73 | 73 |
74 # Deduplicate resources. Some name-index pairs appear in multiple header | |
75 # files. Unless deduplicated here, collisions will be raised in _CheckForHashC ollisions | |
jwd
2015/10/26 15:11:15
Nit: line too long.
Moe
2015/10/26 16:23:52
Done.
| |
76 resources = list(set(resources)) | |
77 | |
74 # The default |Resource| order makes |resources| sorted by the hash, then | 78 # The default |Resource| order makes |resources| sorted by the hash, then |
75 # name, then index. | 79 # name, then index. |
76 resources.sort() | 80 resources.sort() |
77 | 81 |
78 return resources | 82 return resources |
79 | 83 |
80 | 84 |
81 def _CheckForHashCollisions(sorted_resource_list): | 85 def _CheckForHashCollisions(sorted_resource_list): |
82 """Checks a sorted list of |Resource| objects for hash collisions. | 86 """Checks a sorted list of |Resource| objects for hash collisions. |
83 | 87 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 header_filename: Path to the corresponding .h. | 157 header_filename: Path to the corresponding .h. |
154 | 158 |
155 Returns: | 159 Returns: |
156 .cc file content implementing the CreateUIStringOverrider() factory. | 160 .cc file content implementing the CreateUIStringOverrider() factory. |
157 """ | 161 """ |
158 hashed_tuples = _GetResourceListFromString(resources_content) | 162 hashed_tuples = _GetResourceListFromString(resources_content) |
159 | 163 |
160 collisions = _CheckForHashCollisions(hashed_tuples) | 164 collisions = _CheckForHashCollisions(hashed_tuples) |
161 if collisions: | 165 if collisions: |
162 error_message = "\n".join( | 166 error_message = "\n".join( |
163 ["hash: %i, name: %s" % (i[0], i[1]) for i in sorted(collisions)]) | 167 ["hash: %i, name: %s" % (i.hash, i.name) for i in sorted(collisions)]) |
164 error_message = ("\nThe following names had hash collisions " | 168 error_message = ("\nThe following names had hash collisions " |
165 "(sorted by the hash value):\n%s\n" %(error_message)) | 169 "(sorted by the hash value):\n%s\n" %(error_message)) |
166 raise HashCollisionError(error_message) | 170 raise HashCollisionError(error_message) |
167 | 171 |
168 hashes_array = _GenDataArray( | 172 hashes_array = _GenDataArray( |
169 hashed_tuples, " %iU, // %s", 'kResourceHashes', 'uint32_t', | 173 hashed_tuples, " %iU, // %s", 'kResourceHashes', 'uint32_t', |
170 operator.attrgetter('hash')) | 174 operator.attrgetter('hash')) |
171 indices_array = _GenDataArray( | 175 indices_array = _GenDataArray( |
172 hashed_tuples, " %s, // %s", 'kResourceIndices', 'int', | 176 hashed_tuples, " %s, // %s", 'kResourceIndices', 'int', |
173 operator.attrgetter('index')) | 177 operator.attrgetter('index')) |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 with open(os.path.join( | 287 with open(os.path.join( |
284 arguments.output_dir, arguments.source_filename), "w") as generated_file: | 288 arguments.output_dir, arguments.source_filename), "w") as generated_file: |
285 generated_file.write(source_file_content) | 289 generated_file.write(source_file_content) |
286 with open(os.path.join( | 290 with open(os.path.join( |
287 arguments.output_dir, arguments.header_filename), "w") as generated_file: | 291 arguments.output_dir, arguments.header_filename), "w") as generated_file: |
288 generated_file.write(header_file_content) | 292 generated_file.write(header_file_content) |
289 | 293 |
290 | 294 |
291 if __name__ == '__main__': | 295 if __name__ == '__main__': |
292 sys.exit(main()) | 296 sys.exit(main()) |
OLD | NEW |