/run_sed
#! | 42 lines | 31 code | 11 blank | 0 comment | 0 complexity | 3e93c7b68164425210907273c5e2a019 MD5 | raw file
1#!/usr/bin/env python2.6
2
3"""Run sed from a script, but do it as though it is being used in a Makefile.
4
5The difference between this and its use in the makefile itself is that here we
6don't escape $ to $$.
7"""
8
9from __future__ import print_function, division
10
11import os
12import sys
13
14from optparse import OptionParser
15from util import template
16
17def main(opts, args):
18 ns = template.Template()
19
20 sed_args = ["sed"]
21 text = ns.expand_file(args[0],
22 transform_after=ns.xfunc_sed_lines_to_args())
23 sed_args.extend(text.split('\n'))
24 sed_args.extend(args[1:])
25
26 if opts.show_cmd:
27 import pprint
28 pprint.pprint(sed_args, stream=sys.stderr)
29
30 # Ensure that sed still works with international characters (and doesn't just
31 # hang)
32 os.putenv("LANG", "C")
33 with os.popen(" ".join(sed_args)) as f:
34 print(f.read())
35
36if __name__ == '__main__':
37 op = OptionParser()
38 op.add_option("-c", "--show_cmd", dest="show_cmd", default=False,
39 action="store_true", help="show sed script")
40
41 opts, args = op.parse_args()
42 main(opts, args)