diff options
author | newt@chromium.org <newt@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-11 00:33:12 +0000 |
---|---|---|
committer | newt@chromium.org <newt@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-11 00:33:12 +0000 |
commit | 702f311cb961fd0b4a2a34604c034d112c9b50bf (patch) | |
tree | 5ca9d41a4e31dee0dc57db996971075a881c7aa4 /tools/android/remove_strings.py | |
parent | c73bc873fb83e71d76a9f594e527d92edde5329c (diff) | |
download | chromium_src-702f311cb961fd0b4a2a34604c034d112c9b50bf.zip chromium_src-702f311cb961fd0b4a2a34604c034d112c9b50bf.tar.gz chromium_src-702f311cb961fd0b4a2a34604c034d112c9b50bf.tar.bz2 |
[Android] Add tools to find and remove unused Android resources.
BUG=149661
Review URL: https://codereview.chromium.org/13862017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@193520 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/android/remove_strings.py')
-rwxr-xr-x | tools/android/remove_strings.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/tools/android/remove_strings.py b/tools/android/remove_strings.py new file mode 100755 index 0000000..b8c4807 --- /dev/null +++ b/tools/android/remove_strings.py @@ -0,0 +1,49 @@ +#!/usr/bin/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. + +"""Remove strings by name from a GRD file.""" + +import optparse +import re +import sys + + +def RemoveStrings(grd_path, string_names): + """Removes strings with the given names from a GRD file. Overwrites the file. + + Args: + grd_path: path to the GRD file. + string_names: a list of string names to be removed. + """ + with open(grd_path, 'r') as f: + grd = f.read() + names_pattern = '|'.join(map(re.escape, string_names)) + pattern = r'<message [^>]*name="(%s)".*?</message>\s*' % names_pattern + grd = re.sub(pattern, '', grd, flags=re.DOTALL) + with open(grd_path, 'w') as f: + f.write(grd) + + +def ParseArgs(args): + usage = 'usage: %prog GRD_PATH...' + parser = optparse.OptionParser( + usage=usage, description='Remove strings from GRD files. Reads string ' + 'names from stdin, and removes strings with those names from the listed ' + 'GRD files.') + options, args = parser.parse_args(args=args) + if not args: + parser.error('must provide GRD_PATH argument(s)') + return args + + +def main(args=None): + grd_paths = ParseArgs(args) + strings_to_remove = filter(None, map(str.strip, sys.stdin.readlines())) + for grd_path in grd_paths: + RemoveStrings(grd_path, strings_to_remove) + + +if __name__ == '__main__': + main() |