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.
6 """Remove strings by name from a GRD file."""
13 def RemoveStrings(grd_path
, string_names
):
14 """Removes strings with the given names from a GRD file. Overwrites the file.
17 grd_path: path to the GRD file.
18 string_names: a list of string names to be removed.
20 with
open(grd_path
, 'r') as f
:
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
:
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 '
35 options
, args
= parser
.parse_args(args
=args
)
37 parser
.error('must provide GRD_PATH argument(s)')
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
)
48 if __name__
== '__main__':