Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: tools/unused-grit-header.py

Issue 510623002: Fix a couple small problems with tools/unused-grit-header.py. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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(root, 'messages') 81 messages_node = FindNodeWithTag(release_node, 'messages')
82 messages = set() 82 messages = set()
83 if messages_node != None: 83 if messages_node != None:
84 messages = set(GetResourcesForNode(messages_node, grd_file, 'message')) 84 messages = set(GetResourcesForNode(messages_node, grd_file, 'message'))
85 85
86 includes_node = FindNodeWithTag(root, 'includes') 86 includes_node = FindNodeWithTag(release_node, 'includes')
87 includes = set() 87 includes = set()
88 if includes_node != None: 88 if includes_node != None:
89 includes = set(GetResourcesForNode(includes_node, grd_file, 'include')) 89 includes = set(GetResourcesForNode(includes_node, grd_file, 'include'))
90 return messages.union(includes) 90 return messages.union(includes)
91 91
92 92
93 def GetOutputFileForNode(node): 93 def GetOutputFileForNode(node):
94 """Find the output file starting from a given node. 94 """Find the output file starting from a given node.
95 95
96 Args: 96 Args:
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 return 1 173 return 1
174 grd_file = argv[1] 174 grd_file = argv[1]
175 dirs_to_scan = argv[2:] 175 dirs_to_scan = argv[2:]
176 for f in dirs_to_scan: 176 for f in dirs_to_scan:
177 if not os.path.exists(f): 177 if not os.path.exists(f):
178 print 'Error: %s does not exist' % f 178 print 'Error: %s does not exist' % f
179 return 1 179 return 1
180 180
181 tree = xml.etree.ElementTree.parse(grd_file) 181 tree = xml.etree.ElementTree.parse(grd_file)
182 grit_header = GetOutputHeaderFile(tree) 182 grit_header = GetOutputHeaderFile(tree)
183 if not grit_header:
184 print 'Error: %s does not generate any output headers.' % grit_header
185 return 1
183 resources = GetResourcesForGrdFile(tree, grd_file) 186 resources = GetResourcesForGrdFile(tree, grd_file)
184 187
185 files_with_unneeded_grit_includes = [] 188 files_with_unneeded_grit_includes = []
186 for dir_to_scan in dirs_to_scan: 189 for dir_to_scan in dirs_to_scan:
187 for root, dirs, files in os.walk(dir_to_scan): 190 for root, dirs, files in os.walk(dir_to_scan):
188 if '.git' in dirs: 191 if '.git' in dirs:
189 dirs.remove('.git') 192 dirs.remove('.git')
190 full_paths = [os.path.join(root, f) for f in files if ShouldScanFile(f)] 193 full_paths = [os.path.join(root, f) for f in files if ShouldScanFile(f)]
191 files_with_unneeded_grit_includes.extend( 194 files_with_unneeded_grit_includes.extend(
192 [f for f in full_paths 195 [f for f in full_paths
193 if not NeedsGritInclude(grit_header, resources, f)]) 196 if not NeedsGritInclude(grit_header, resources, f)])
194 if files_with_unneeded_grit_includes: 197 if files_with_unneeded_grit_includes:
195 print '\n'.join(files_with_unneeded_grit_includes) 198 print '\n'.join(files_with_unneeded_grit_includes)
196 return 2 199 return 2
197 return 0 200 return 0
198 201
199 202
200 if __name__ == '__main__': 203 if __name__ == '__main__':
201 sys.exit(main(sys.argv)) 204 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698