tools/resources/find_unused_resources.py
def GetUnusedResources(grd_filepath):
  """Returns a list of resources that are unused in the code.

  Prints status lines to the console because this function is quite slow.

  Args:
    grd_filepath: Path to a .grd file listing resources.

  Returns:
    A list of pairs of [resource_id, filepath] for the unused resources.
  """
  unused_resources = []
  grd_file = open(grd_filepath, 'r')
  grd_data = grd_file.read()
  print('Checking:')
  # Match the resource id and file path out of substrings like:
  # ...name="IDR_FOO_123" file="common/foo.png"...
  # by matching between the quotation marks.
  pattern = re.compile(
      r"""name="([^"]*)"  # Match resource ID between quotes.
      \s*                 # Run of whitespace, including newlines.
      file="([^"]*)"      # Match file path between quotes.""",
      re.VERBOSE)
  # Use finditer over the file contents because there may be newlines between
  # the name and file attributes.
  searched = set()
  for result in pattern.finditer(grd_data):
    # Extract the IDR resource id and file path.
    resource_id = result.group(1)
    filepath = result.group(2)
    filename = os.path.basename(filepath)
    base_resource_id = GetBaseResourceId(resource_id)

    # Do not bother repeating searches.
    key = (base_resource_id, filename)
    if key in searched:
      continue
    searched.add(key)

    # Print progress as we go along.
    print(resource_id)

    # Ensure the resource isn't used anywhere by checking both for the resource
    # id (which should appear in C++ code) and the raw filename (in case the
    # file is referenced in a script, test HTML file, etc.).
    matching_files = FindFilesWithContents(base_resource_id, filename)

    # Each file is matched once in the resource file itself. If there are no
    # other matching files, it is unused.
    if len(matching_files) == 1:
      # Give the user some happy news.
      print('Unused!')
      unused_resources.append([resource_id, filepath])

  return unused_resources
Returns a list of resources that are unused in the code.

  Prints status lines to the console because this function is quite slow.

  Args:
    grd_filepath: Path to a .grd file listing resources.

  Returns:
    A list of pairs of [resource_id, filepath] for the unused resources.
  
Returns a list of resources that are unused in the code.
Prints status lines to the console because this function is quite slow.
Returns a list of resources that are unused in the code . Prints status lines to the console because this function is quite slow .
def GetUnusedResources(grd_filepath):
  unused_resources = []
  grd_file = open(grd_filepath, 'r')
  grd_data = grd_file.read()
  print('Checking:')
  pattern = re.compile(
      r"""name="([^"]*)"  # Match resource ID between quotes.
      \s*                 # Run of whitespace, including newlines.
      file="([^"]*)"      # Match file path between quotes.""",
      re.VERBOSE)
  searched = set()
  for result in pattern.finditer(grd_data):
    resource_id = result.group(1)
    filepath = result.group(2)
    filename = os.path.basename(filepath)
    base_resource_id = GetBaseResourceId(resource_id)
    key = (base_resource_id, filename)
    if key in searched:
      continue
    searched.add(key)
    print(resource_id)
    matching_files = FindFilesWithContents(base_resource_id, filename)
    if len(matching_files) == 1:
      print('Unused!')
      unused_resources.append([resource_id, filepath])
  return unused_resources
def GetUnusedResources ( grd_filepath ) : unused_resources = [ ] grd_file = open ( grd_filepath , 'r' ) grd_data = grd_file . read ( ) print ( 'Checking:' ) pattern = re . compile ( r"""name="([^"]*)"  # Match resource ID between quotes.
      \s*                 # Run of whitespace, including newlines.
      file="([^"]*)"      # Match file path between quotes.""" , re . VERBOSE ) searched = set ( ) for result in pattern . finditer ( grd_data ) : resource_id = result . group ( 1 ) filepath = result . group ( 2 ) filename = os . path . basename ( filepath ) base_resource_id = GetBaseResourceId ( resource_id ) key = ( base_resource_id , filename ) if key in searched : continue searched . add ( key ) print ( resource_id ) matching_files = FindFilesWithContents ( base_resource_id , filename ) if len ( matching_files ) == 1 : print ( 'Unused!' ) unused_resources . append ( [ resource_id , filepath ] ) return unused_resources
Returns a list of resources that are unused in the code.
Returns a list of resources that are unused in the code .
"""Returns a list of resources that are unused in the code.

  Prints status lines to the console because this function is quite slow.

  Args:
    grd_filepath: Path to a .grd file listing resources.

  Returns:
    A list of pairs of [resource_id, filepath] for the unused resources.
  """ # Match the resource id and file path out of substrings like: # ...name="IDR_FOO_123" file="common/foo.png"... # by matching between the quotation marks. # Use finditer over the file contents because there may be newlines between # the name and file attributes. # Extract the IDR resource id and file path. # Do not bother repeating searches. # Print progress as we go along. # Ensure the resource isn't used anywhere by checking both for the resource # id (which should appear in C++ code) and the raw filename (in case the # file is referenced in a script, test HTML file, etc.). # Each file is matched once in the resource file itself. If there are no # other matching files, it is unused. # Give the user some happy news.
A list of pairs of [resource_id, filepath] for the unused resources.
A list of pairs of [ resource_id filepath ] for the unused resources .
grd_filepath
Path to a .grd file listing resources.
Path to a . grd file listing resources .