OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 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 """This script searches for unused art assets listed in a .grd file. | 6 """This script searches for unused art assets listed in a .grd file. |
7 | 7 |
8 It uses git grep to look for references to the IDR resource id or the base | 8 It uses git grep to look for references to the IDR resource id or the base |
9 filename. If neither is found, the file is reported unused. | 9 filename. If neither is found, the file is reported unused. |
10 | 10 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 # Match the resource id and file path out of substrings like: | 91 # Match the resource id and file path out of substrings like: |
92 # ...name="IDR_FOO_123" file="common/foo.png"... | 92 # ...name="IDR_FOO_123" file="common/foo.png"... |
93 # by matching between the quotation marks. | 93 # by matching between the quotation marks. |
94 pattern = re.compile( | 94 pattern = re.compile( |
95 r"""name="([^"]*)" # Match resource ID between quotes. | 95 r"""name="([^"]*)" # Match resource ID between quotes. |
96 \s* # Run of whitespace, including newlines. | 96 \s* # Run of whitespace, including newlines. |
97 file="([^"]*)" # Match file path between quotes.""", | 97 file="([^"]*)" # Match file path between quotes.""", |
98 re.VERBOSE) | 98 re.VERBOSE) |
99 # Use finditer over the file contents because there may be newlines between | 99 # Use finditer over the file contents because there may be newlines between |
100 # the name and file attributes. | 100 # the name and file attributes. |
| 101 searched = set() |
101 for result in pattern.finditer(grd_data): | 102 for result in pattern.finditer(grd_data): |
102 # Extract the IDR resource id and file path. | 103 # Extract the IDR resource id and file path. |
103 resource_id = result.group(1) | 104 resource_id = result.group(1) |
104 filepath = result.group(2) | 105 filepath = result.group(2) |
105 filename = os.path.basename(filepath) | 106 filename = os.path.basename(filepath) |
| 107 base_resource_id = GetBaseResourceId(resource_id) |
| 108 |
| 109 # Do not bother repeating searches. |
| 110 key = (base_resource_id, filename) |
| 111 if key in searched: |
| 112 continue |
| 113 searched.add(key) |
| 114 |
106 # Print progress as we go along. | 115 # Print progress as we go along. |
107 print resource_id | 116 print resource_id |
| 117 |
108 # Ensure the resource isn't used anywhere by checking both for the resource | 118 # Ensure the resource isn't used anywhere by checking both for the resource |
109 # id (which should appear in C++ code) and the raw filename (in case the | 119 # id (which should appear in C++ code) and the raw filename (in case the |
110 # file is referenced in a script, test HTML file, etc.). | 120 # file is referenced in a script, test HTML file, etc.). |
111 base_resource_id = GetBaseResourceId(resource_id) | |
112 matching_files = FindFilesWithContents(base_resource_id, filename) | 121 matching_files = FindFilesWithContents(base_resource_id, filename) |
| 122 |
113 # Each file is matched once in the resource file itself. If there are no | 123 # Each file is matched once in the resource file itself. If there are no |
114 # other matching files, it is unused. | 124 # other matching files, it is unused. |
115 if len(matching_files) == 1: | 125 if len(matching_files) == 1: |
116 # Give the user some happy news. | 126 # Give the user some happy news. |
117 print 'Unused!' | 127 print 'Unused!' |
118 unused_resources.append([resource_id, filepath]) | 128 unused_resources.append([resource_id, filepath]) |
119 | 129 |
120 return unused_resources | 130 return unused_resources |
121 | 131 |
122 | 132 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 # Print a list of 'git rm' command lines to remove unused assets. | 195 # Print a list of 'git rm' command lines to remove unused assets. |
186 print | 196 print |
187 print 'Unused files:' | 197 print 'Unused files:' |
188 for resource_id, filepath in unused_resources: | 198 for resource_id, filepath in unused_resources: |
189 for directory in scale_directories: | 199 for directory in scale_directories: |
190 print 'git rm ' + os.path.join(directory, filepath) | 200 print 'git rm ' + os.path.join(directory, filepath) |
191 | 201 |
192 | 202 |
193 if __name__ == '__main__': | 203 if __name__ == '__main__': |
194 main() | 204 main() |
OLD | NEW |