Home | History | Annotate | Download | only in resources
      1 #!/usr/bin/env python
      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.
      5 
      6 
      7 import os
      8 import re
      9 import sys
     10 
     11 usage = """%s BUILDTYPE BUILDDIR
     12 
     13 BUILDTYPE: either chromium or chrome.
     14 BUILDDIR: The path to the output directory. e.g. relpath/to/out/Release
     15 
     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.
     20 
     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.
     23 """
     24 
     25 
     26 def GetResourceIdsFromRepackMessage(in_data):
     27   """Returns sorted set of resource ids that are not used from in_data.
     28   """
     29   unused_resources = set()
     30   unused_pattern = re.compile(
     31       'RePackFromDataPackStrings Removed Key: (?P<resource_id>[0-9]+)')
     32   for line in in_data:
     33     match = unused_pattern.match(line)
     34     if match:
     35       resource_id = int(match.group('resource_id'))
     36       unused_resources.add(resource_id)
     37   return sorted(unused_resources)
     38 
     39 
     40 def Main():
     41   if len(sys.argv) != 3:
     42     sys.stderr.write(usage % sys.argv[0])
     43     return 1
     44 
     45   build_type = sys.argv[1]
     46   build_dir = sys.argv[2]
     47 
     48   if build_type not in ('chromium', 'chrome'):
     49     sys.stderr.write(usage % sys.argv[0])
     50     return 1
     51 
     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)
     55     return 1
     56 
     57   if build_type == 'chromium':
     58     excluded_header = 'google_chrome_strings.h'
     59   else:
     60     excluded_header = 'chromium_strings.h'
     61   data_files = []
     62   for root, dirs, files in os.walk(generated_output_dir):
     63     if os.path.basename(root) != 'grit':
     64       continue
     65 
     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])
     70 
     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]+)$')
     74   for f in data_files:
     75     data = open(f).read()
     76     for line in data.splitlines():
     77       match = resource_pattern.match(line)
     78       if match:
     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]
     85           raise
     86         resource_id_to_name_file_map[resource_id] = (resource_name, f)
     87 
     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
     92       continue
     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))
     95   return 0
     96 
     97 
     98 if __name__ == '__main__':
     99   sys.exit(Main())
    100