3 # Simple utility script to manipulate
4 # certain types of strings in a file
6 # This can be used in various projects where
7 # there is the need to replace strings in files,
8 # and is copied from GLib's $(srcroot)/win32
10 # Author: Fan, Chun-wei
11 # Date: September 03, 2014
19 valid_actions
= ['remove-prefix',
24 def open_file(filename
, mode
):
25 if sys
.version_info
[0] < 3:
26 return open(filename
, mode
=mode
)
28 return open(filename
, mode
=mode
, encoding
='utf-8')
30 def replace_multi(src
, dest
, replace_items
):
31 with
open_file(src
, 'r') as s
:
32 with
open_file(dest
, 'w') as d
:
34 replace_dict
= dict((re
.escape(key
), value
) \
35 for key
, value
in replace_items
.items())
36 replace_pattern
= re
.compile("|".join(replace_dict
.keys()))
37 d
.write(replace_pattern
.sub(lambda m
: \
38 replace_dict
[re
.escape(m
.group(0))], line
))
40 def replace(src
, dest
, instring
, outstring
):
41 replace_item
= {instring
: outstring
}
42 replace_multi(src
, dest
, replace_item
)
44 def check_required_args(args
, params
):
46 if getattr(args
, param
, None) is None:
47 raise SystemExit('%s: error: --%s argument is required' % (__file__
, param
))
49 def warn_ignored_args(args
, params
):
51 if getattr(args
, param
, None) is not None:
52 print('%s: warning: --%s argument is ignored' % (__file__
, param
))
56 parser
= argparse
.ArgumentParser(description
='Process strings in a file.')
57 parser
.add_argument('-a',
59 help='Action to carry out. Can be one of:\n'
64 choices
=valid_actions
)
65 parser
.add_argument('-i', '--input', help='Input file')
66 parser
.add_argument('-o', '--output', help='Output file')
67 parser
.add_argument('--instring', help='String to replace or remove')
68 parser
.add_argument('--var', help='Autotools variable name to replace')
69 parser
.add_argument('--outstring',
70 help='New String to replace specified string or variable')
71 parser
.add_argument('--removeprefix', help='Prefix of string to remove')
73 args
= parser
.parse_args()
78 # We must have action, input, output for all operations
79 check_required_args(args
, ['action','input','output'])
81 # Build the arguments by the operation that is to be done,
82 # to be fed into replace()
84 # Get rid of prefixes from a string
85 if args
.action
== 'remove-prefix':
86 check_required_args(args
, ['instring','removeprefix'])
87 warn_ignored_args(args
, ['outstring','var'])
88 input_string
= args
.removeprefix
+ args
.instring
89 output_string
= args
.instring
91 # Replace an m4-style variable (those surrounded by @...@)
92 if args
.action
== 'replace-var':
93 check_required_args(args
, ['var','outstring'])
94 warn_ignored_args(args
, ['instring','removeprefix'])
95 input_string
= '@' + args
.var
+ '@'
96 output_string
= args
.outstring
99 if args
.action
== 'replace-str':
100 check_required_args(args
, ['instring','outstring'])
101 warn_ignored_args(args
, ['var','removeprefix'])
102 input_string
= args
.instring
103 output_string
= args
.outstring
106 if args
.action
== 'remove-str':
107 check_required_args(args
, ['instring'])
108 warn_ignored_args(args
, ['var','outstring','removeprefix'])
109 input_string
= args
.instring
112 replace(args
.input, args
.output
, input_string
, output_string
)
114 if __name__
== '__main__':
115 sys
.exit(main(sys
.argv
))