Home | History | Annotate | Download | only in android
      1 #!/usr/bin/python
      2 # Copyright (c) 2013 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 """Remove strings by name from a GRD file."""
      7 
      8 import optparse
      9 import re
     10 import sys
     11 
     12 
     13 def RemoveStrings(grd_path, string_names):
     14   """Removes strings with the given names from a GRD file. Overwrites the file.
     15 
     16   Args:
     17     grd_path: path to the GRD file.
     18     string_names: a list of string names to be removed.
     19   """
     20   with open(grd_path, 'r') as f:
     21     grd = f.read()
     22   names_pattern = '|'.join(map(re.escape, string_names))
     23   pattern = r'<message [^>]*name="(%s)".*?</message>\s*' % names_pattern
     24   grd = re.sub(pattern, '', grd, flags=re.DOTALL)
     25   with open(grd_path, 'w') as f:
     26     f.write(grd)
     27 
     28 
     29 def ParseArgs(args):
     30   usage = 'usage: %prog GRD_PATH...'
     31   parser = optparse.OptionParser(
     32       usage=usage, description='Remove strings from GRD files. Reads string '
     33       'names from stdin, and removes strings with those names from the listed '
     34       'GRD files.')
     35   options, args = parser.parse_args(args=args)
     36   if not args:
     37     parser.error('must provide GRD_PATH argument(s)')
     38   return args
     39 
     40 
     41 def main(args=None):
     42   grd_paths = ParseArgs(args)
     43   strings_to_remove = filter(None, map(str.strip, sys.stdin.readlines()))
     44   for grd_path in grd_paths:
     45     RemoveStrings(grd_path, strings_to_remove)
     46 
     47 
     48 if __name__ == '__main__':
     49   main()
     50