1#!/usr/bin/python
2#
3# Simple utility script to manipulate
4# certain types of strings in a file
5
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)/build/win32
9
10# Author: Fan, Chun-wei
11# Date: September 03, 2014
12
13import os
14import sys
15import re
16import string
17import argparse
18
19valid_actions = ['remove-prefix',
20                 'replace-var',
21                 'replace-str',
22                 'remove-str']
23
24def open_file(filename, mode):
25    if sys.version_info[0] < 3:
26        return open(filename, mode=mode)
27    else:
28        return open(filename, mode=mode, encoding='utf-8')
29
30def replace_multi(src, dest, replace_items):
31    with open_file(src, 'r') as s:
32        with open_file(dest, 'w') as d:
33            for line in s:
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))
39
40def replace(src, dest, instring, outstring):
41    replace_item = {instring: outstring}
42    replace_multi(src, dest, replace_item)
43
44def check_required_args(args, params):
45    for param in params:
46        if getattr(args, param, None) is None:
47            raise SystemExit('%s: error: --%s argument is required' % (__file__, param))
48
49def warn_ignored_args(args, params):
50    for param in params:
51        if getattr(args, param, None) is not None:
52            print('%s: warning: --%s argument is ignored' % (__file__, param))
53
54def main(argv):
55
56    parser = argparse.ArgumentParser(description='Process strings in a file.')
57    parser.add_argument('-a',
58                        '--action',
59                        help='Action to carry out.  Can be one of:\n'
60                             'remove-prefix\n'
61                             'replace-var\n'
62                             'replace-str\n'
63                             'remove-str',
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')
72
73    args = parser.parse_args()
74
75    input_string = ''
76    output_string = ''
77
78    # We must have action, input, output for all operations
79    check_required_args(args, ['action','input','output'])
80
81    # Build the arguments by the operation that is to be done,
82    # to be fed into replace()
83
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
90
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
97
98    # Replace a string
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
104
105    # Remove a string
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
110        output_string = ''
111
112    replace(args.input, args.output, input_string, output_string)
113
114if __name__ == '__main__':
115    sys.exit(main(sys.argv))
116