Chromium Code Reviews| 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 | |
|
Alexei Svitkine (slow)
2015/10/27 15:54:31
Nit: Is this specific for chromium_ vs. google_chr
Moe
2015/10/28 00:30:38
Done.
| |
| 76 # _CheckForHashCollisions | |
|
Alexei Svitkine (slow)
2015/10/27 15:54:31
Nit: Add a .
Moe
2015/10/28 00:30:38
Done.
| |
| 77 resources = list(set(resources)) | |
| 78 | |
| 74 # The default |Resource| order makes |resources| sorted by the hash, then | 79 # The default |Resource| order makes |resources| sorted by the hash, then |
| 75 # name, then index. | 80 # name, then index. |
| 76 resources.sort() | 81 resources.sort() |
| 77 | 82 |
| 78 return resources | 83 return resources |
| 79 | 84 |
| 80 | 85 |
| 81 def _CheckForHashCollisions(sorted_resource_list): | 86 def _CheckForHashCollisions(sorted_resource_list): |
| 82 """Checks a sorted list of |Resource| objects for hash collisions. | 87 """Checks a sorted list of |Resource| objects for hash collisions. |
| 83 | 88 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 header_filename: Path to the corresponding .h. | 158 header_filename: Path to the corresponding .h. |
| 154 | 159 |
| 155 Returns: | 160 Returns: |
| 156 .cc file content implementing the CreateUIStringOverrider() factory. | 161 .cc file content implementing the CreateUIStringOverrider() factory. |
| 157 """ | 162 """ |
| 158 hashed_tuples = _GetResourceListFromString(resources_content) | 163 hashed_tuples = _GetResourceListFromString(resources_content) |
| 159 | 164 |
| 160 collisions = _CheckForHashCollisions(hashed_tuples) | 165 collisions = _CheckForHashCollisions(hashed_tuples) |
| 161 if collisions: | 166 if collisions: |
| 162 error_message = "\n".join( | 167 error_message = "\n".join( |
| 163 ["hash: %i, name: %s" % (i[0], i[1]) for i in sorted(collisions)]) | 168 ["hash: %i, name: %s" % (i.hash, i.name) for i in sorted(collisions)]) |
| 164 error_message = ("\nThe following names had hash collisions " | 169 error_message = ("\nThe following names had hash collisions " |
| 165 "(sorted by the hash value):\n%s\n" %(error_message)) | 170 "(sorted by the hash value):\n%s\n" %(error_message)) |
| 166 raise HashCollisionError(error_message) | 171 raise HashCollisionError(error_message) |
| 167 | 172 |
| 168 hashes_array = _GenDataArray( | 173 hashes_array = _GenDataArray( |
| 169 hashed_tuples, " %iU, // %s", 'kResourceHashes', 'uint32_t', | 174 hashed_tuples, " %iU, // %s", 'kResourceHashes', 'uint32_t', |
| 170 operator.attrgetter('hash')) | 175 operator.attrgetter('hash')) |
| 171 indices_array = _GenDataArray( | 176 indices_array = _GenDataArray( |
| 172 hashed_tuples, " %s, // %s", 'kResourceIndices', 'int', | 177 hashed_tuples, " %s, // %s", 'kResourceIndices', 'int', |
| 173 operator.attrgetter('index')) | 178 operator.attrgetter('index')) |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 with open(os.path.join( | 288 with open(os.path.join( |
| 284 arguments.output_dir, arguments.source_filename), "w") as generated_file: | 289 arguments.output_dir, arguments.source_filename), "w") as generated_file: |
| 285 generated_file.write(source_file_content) | 290 generated_file.write(source_file_content) |
| 286 with open(os.path.join( | 291 with open(os.path.join( |
| 287 arguments.output_dir, arguments.header_filename), "w") as generated_file: | 292 arguments.output_dir, arguments.header_filename), "w") as generated_file: |
| 288 generated_file.write(header_file_content) | 293 generated_file.write(header_file_content) |
| 289 | 294 |
| 290 | 295 |
| 291 if __name__ == '__main__': | 296 if __name__ == '__main__': |
| 292 sys.exit(main()) | 297 sys.exit(main()) |
| OLD | NEW |