#!/usr/bin/env python # Copyright (c) 2013 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """Lists unused Java strings and other resources.""" from __future__ import print_function import optparse import re import subprocess import sys def GetLibraryResources(r_txt_paths): """Returns the resources packaged in a list of libraries. Args: r_txt_paths: paths to each library's generated R.txt file which lists the resources it contains. Returns: The resources in the libraries as a list of tuples (type, name). Example: [('drawable', 'arrow'), ('layout', 'month_picker'), ...] """ resources = [] for r_txt_path in r_txt_paths: with open(r_txt_path, 'r') as f: for line in f: line = line.strip() if not line: continue data_type, res_type, name, _ = line.split(None, 3) assert data_type in ('int', 'int[]') # Hide attrs, which are redundant with styleables and always appear # unused, and hide ids, which are innocuous even if unused. if res_type in ('attr', 'id'): continue resources.append((res_type, name)) return resources def GetUsedResources(source_paths, resource_types): """Returns the types and names of resources used in Java or resource files. Args: source_paths: a list of files or folders collectively containing all the Java files, resource files, and the AndroidManifest.xml. resource_types: a list of resource types to look for. Example: ['string', 'drawable'] Returns: The resources referenced by the Java and resource files as a list of tuples (type, name). Example: [('drawable', 'app_icon'), ('layout', 'month_picker'), ...] """ type_regex = '|'.join(map(re.escape, resource_types)) patterns = [ # @+drawable/id and @drawable/id r'@((\+?))(%s)/(\w+)' % type_regex, # package.R.style.id r'\b((\w+\.)*)R\.(%s)\.(\w+)' % type_regex, #