Makefile: remotes: Delete obsolete remote
[sunny256-utils.git] / line_exec.py
blob3bc8dfd3cdcad578b9a8a12e849090d9a2e94190
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 """line_exec.py - Send all lines from stdin to specified programs
6 Reads from stdin and sends every line to the program(s) specified on the
7 command line.
9 File ID: 0a869b50-f45a-11e2-bb76-a088b4ddef28
10 License: GNU General Public License version 2 or later.
12 """
14 import sys
15 import subprocess
17 def exec_line(line, args):
18 for cmd in args:
19 pipe = subprocess.Popen(cmd, stdin=subprocess.PIPE,
20 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
21 (stdout, stderr) = pipe.communicate(line)
22 sys.stdout.write(stdout)
23 sys.stdout.flush()
24 sys.stderr.write(stderr)
25 sys.stderr.flush()
27 def main(args = None):
28 from optparse import OptionParser
29 parser = OptionParser()
30 parser.add_option("-A", "--after", type="string", dest="after",
31 default='',
32 help="Text string to add after every line output",
33 metavar="TEXT")
34 parser.add_option("-B", "--before", type="string", dest="before",
35 default='',
36 help="Text string to add before every line output",
37 metavar="TEXT")
38 (opt, args) = parser.parse_args()
40 fp = sys.__stdin__
41 line = fp.readline()
42 while line:
43 sys.stdout.write(opt.before)
44 exec_line(line, args)
45 sys.stdout.write(opt.after)
46 line = fp.readline()
48 if __name__ == "__main__":
49 main()