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
4 # found in the LICENSE file.
11 usage
= """%s BUILDTYPE BUILDDIR
13 BUILDTYPE: either chromium or chrome.
14 BUILDDIR: The path to the output directory. e.g. relpath/to/out/Release
16 Prints out (to stdout) the sorted list of resource ids that are marked as
17 unused during the repacking process in the given build log (via stdin).
18 Additionally, attempt to print out the name of the resource and the generated
19 header file that contains the resource.
21 This script is used to print the list of resources that are not used so that
22 developers will notice and fix their .grd files.
26 def GetResourceIdsFromRepackMessage(in_data
):
27 """Returns sorted set of resource ids that are not used from in_data.
29 unused_resources
= set()
30 unused_pattern
= re
.compile(
31 'RePackFromDataPackStrings Removed Key: (?P<resource_id>[0-9]+)')
33 match
= unused_pattern
.match(line
)
35 resource_id
= int(match
.group('resource_id'))
36 unused_resources
.add(resource_id
)
37 return sorted(unused_resources
)
41 if len(sys
.argv
) != 3:
42 sys
.stderr
.write(usage
% sys
.argv
[0])
45 build_type
= sys
.argv
[1]
46 build_dir
= sys
.argv
[2]
48 if build_type
not in ('chromium', 'chrome'):
49 sys
.stderr
.write(usage
% sys
.argv
[0])
52 generated_output_dir
= os
.path
.join(build_dir
, 'gen')
53 if not os
.path
.exists(generated_output_dir
):
54 sys
.stderr
.write('Cannot find gen dir %s' % generated_output_dir
)
57 if build_type
== 'chromium':
58 excluded_header
= 'google_chrome_strings.h'
60 excluded_header
= 'chromium_strings.h'
62 for root
, dirs
, files
in os
.walk(generated_output_dir
):
63 if os
.path
.basename(root
) != 'grit':
66 header_files
= [header
for header
in files
if header
.endswith('.h')]
67 if excluded_header
in header_files
:
68 header_files
.remove(excluded_header
)
69 data_files
.extend([os
.path
.join(root
, header
) for header
in header_files
])
71 resource_id_to_name_file_map
= {}
72 resource_pattern
= re
.compile('#define (?P<resource_name>[A-Z0-9_]+).* '
73 '(?P<resource_id>[0-9]+)$')
76 for line
in data
.splitlines():
77 match
= resource_pattern
.match(line
)
79 resource_id
= int(match
.group('resource_id'))
80 resource_name
= match
.group('resource_name')
81 if resource_id
in resource_id_to_name_file_map
:
82 print 'Duplicate:', resource_id
83 print (resource_name
, f
)
84 print resource_id_to_name_file_map
[resource_id
]
86 resource_id_to_name_file_map
[resource_id
] = (resource_name
, f
)
88 unused_resources
= GetResourceIdsFromRepackMessage(sys
.stdin
)
89 for resource_id
in unused_resources
:
90 if resource_id
not in resource_id_to_name_file_map
:
91 print 'WARNING: Unknown resource id', resource_id
93 (resource_name
, filename
) = resource_id_to_name_file_map
[resource_id
]
94 sys
.stdout
.write('%d: %s in %s\n' % (resource_id
, resource_name
, filename
))
98 if __name__
== '__main__':