OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env 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 """A tool to scan source files for unneeded grit includes.""" | 6 """A tool to scan source files for unneeded grit includes.""" |
7 | 7 |
8 import os | 8 import os |
9 import sys | 9 import sys |
10 import xml.etree.ElementTree | 10 import xml.etree.ElementTree |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 grd_file: The file that contains the XML tree. | 71 grd_file: The file that contains the XML tree. |
72 | 72 |
73 Returns: | 73 Returns: |
74 A list of resource names. | 74 A list of resource names. |
75 """ | 75 """ |
76 root = tree.getroot() | 76 root = tree.getroot() |
77 assert root.tag == 'grit' | 77 assert root.tag == 'grit' |
78 release_node = FindNodeWithTag(root, 'release') | 78 release_node = FindNodeWithTag(root, 'release') |
79 assert release_node != None | 79 assert release_node != None |
80 | 80 |
81 messages_node = FindNodeWithTag(release_node, 'messages') | 81 resources = set() |
82 messages = set() | 82 for node_type in ('message', 'include', 'structure'): |
83 if messages_node != None: | 83 resources_node = FindNodeWithTag(release_node, node_type + 's') |
84 messages = set(GetResourcesForNode(messages_node, grd_file, 'message')) | 84 if resources_node != None: |
85 | 85 resources = resources.union( |
86 includes_node = FindNodeWithTag(release_node, 'includes') | 86 set(GetResourcesForNode(resources_node, grd_file, node_type))) |
87 includes = set() | 87 return resources |
88 if includes_node != None: | |
89 includes = set(GetResourcesForNode(includes_node, grd_file, 'include')) | |
90 return messages.union(includes) | |
91 | 88 |
92 | 89 |
93 def GetOutputFileForNode(node): | 90 def GetOutputFileForNode(node): |
94 """Find the output file starting from a given node. | 91 """Find the output file starting from a given node. |
95 | 92 |
96 Args: | 93 Args: |
97 node: The root node to scan from. | 94 node: The root node to scan from. |
98 | 95 |
99 Returns: | 96 Returns: |
100 A grit header file name. | 97 A grit header file name. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 """Return whether a file needs a given grit header or not. | 140 """Return whether a file needs a given grit header or not. |
144 | 141 |
145 Args: | 142 Args: |
146 grit_header: The grit header file name. | 143 grit_header: The grit header file name. |
147 resources: The list of resource names in grit_header. | 144 resources: The list of resource names in grit_header. |
148 filename: The file to scan. | 145 filename: The file to scan. |
149 | 146 |
150 Returns: | 147 Returns: |
151 True if the file should include the grit header. | 148 True if the file should include the grit header. |
152 """ | 149 """ |
150 # A list of special keywords that implies the file needs grit headers. | |
151 # To be more thorough, one would need to run a pre-processor. | |
152 SPECIAL_KEYWORDS = ( | |
153 'IMAGE_GRID(', # Macro in nine_image_painter_factory.h | |
tfarina
2014/08/29 17:55:54
https://chromium.googlesource.com/chromium/src/+/m
| |
154 '#include "ui_localizer_table.h"', # ui_localizer.mm | |
155 'DEFINE_RESOURCE_ID', # chrome/browser/android/resource_mapper.cc | |
156 ) | |
153 with open(filename, 'rb') as f: | 157 with open(filename, 'rb') as f: |
154 grit_header_line = grit_header + '"\n' | 158 grit_header_line = grit_header + '"\n' |
155 has_grit_header = False | 159 has_grit_header = False |
156 while True: | 160 while True: |
157 line = f.readline() | 161 line = f.readline() |
158 if not line: | 162 if not line: |
159 break | 163 break |
160 if line.endswith(grit_header_line): | 164 if line.endswith(grit_header_line): |
161 has_grit_header = True | 165 has_grit_header = True |
162 break | 166 break |
163 | 167 |
164 if not has_grit_header: | 168 if not has_grit_header: |
165 return True | 169 return True |
166 rest_of_the_file = f.read() | 170 rest_of_the_file = f.read() |
167 return any(resource in rest_of_the_file for resource in resources) | 171 return (any(resource in rest_of_the_file for resource in resources) or |
172 any(keyword in rest_of_the_file for keyword in SPECIAL_KEYWORDS)) | |
168 | 173 |
169 | 174 |
170 def main(argv): | 175 def main(argv): |
171 if len(argv) < 3: | 176 if len(argv) < 3: |
172 Usage(argv[0]) | 177 Usage(argv[0]) |
173 return 1 | 178 return 1 |
174 grd_file = argv[1] | 179 grd_file = argv[1] |
175 dirs_to_scan = argv[2:] | 180 dirs_to_scan = argv[2:] |
176 for f in dirs_to_scan: | 181 for f in dirs_to_scan: |
177 if not os.path.exists(f): | 182 if not os.path.exists(f): |
(...skipping 17 matching lines...) Expand all Loading... | |
195 [f for f in full_paths | 200 [f for f in full_paths |
196 if not NeedsGritInclude(grit_header, resources, f)]) | 201 if not NeedsGritInclude(grit_header, resources, f)]) |
197 if files_with_unneeded_grit_includes: | 202 if files_with_unneeded_grit_includes: |
198 print '\n'.join(files_with_unneeded_grit_includes) | 203 print '\n'.join(files_with_unneeded_grit_includes) |
199 return 2 | 204 return 2 |
200 return 0 | 205 return 0 |
201 | 206 |
202 | 207 |
203 if __name__ == '__main__': | 208 if __name__ == '__main__': |
204 sys.exit(main(sys.argv)) | 209 sys.exit(main(sys.argv)) |
OLD | NEW |