-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe
executable file
·70 lines (55 loc) · 1.91 KB
/
pipe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
import sys, os, argparse, glob, json, urllib2
from collections import OrderedDict
LOGFILE = sys.stderr
IN_STREAM = sys.stdin
OUT_STREAM = sys.stdout
pipepath="./pipefile"
# parse the command line arguments
parser = argparse.ArgumentParser()
# need to revise to make 'write' and 'read' mutually exclusive options ...
parser.add_argument("-w","--write", help="write to pipe file")
parser.add_argument("-r","--read", help="read from pipe file")
# verbose
parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity")
args = parser.parse_args()
# process the input
try:
if(args.write):
try:
#should check to see if there's already a pipefile
pipepath=args.write
os.mkfifo(pipepath)
except OSError:
pass
wp = open(pipepath,'w')
for line in IN_STREAM:
wp.write(line)
wp.close()
if(args.read):
pipepath=args.read
rp=open(pipepath,'r')
response=rp.read()
rp.close()
#now parse the named pipe contents, assuming there's a JSON packet on each line
#note that there's some funkiness in how named pipes work; had to account for an extra blank line at the end of the pipe
splitResponse=response.split("\n")
OUT_STREAM.write('{')
index=0
iMax=len(splitResponse)
i=0
while (i<(iMax-1)): # the (iMax-1) here is because of the final blank line in the named pipe
#print index
index=index+1
OUT_STREAM.write(splitResponse[i])
if (i<(iMax-2)):
OUT_STREAM.write(",")
i=i+1
OUT_STREAM.write("}")
OUT_STREAM.close()
except Exception, exc:
LOGFILE.write("ERROR: %s" % exc)
finally:
LOGFILE.close()
IN_STREAM.close()
# should we perform a sys.stdout.close() here too?