]> TLD Linux GIT Repositories - tld-builder.git/blob - TLD_Builder/pipeutil.py
a3e78c9a1d7be8ff7b819a12d58c43c0ea5fdbe2
[tld-builder.git] / TLD_Builder / pipeutil.py
1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3 import select
4 import os
5 import sys
6 if sys.version_info[0] == 2:
7     import StringIO
8 else:
9     from io import StringIO
10
11 def rw_pipe(buf_, infd, outfd):
12     buf = StringIO()
13     buf.write(buf_.read())
14     ret = StringIO()
15     pos = 0
16     rd_fin = 0
17     wr_fin = 0
18     buf.seek(pos)
19     while not (rd_fin and wr_fin):
20         if wr_fin:
21             o = []
22         else:
23             o = [infd]
24         if rd_fin:
25             i = []
26         else:
27             i = [outfd]
28         i, o, e = select.select(i, o, [])
29         if i != []:
30             s = os.read(outfd.fileno(), 1000)
31             if s == "":
32                 rd_fin = 1
33             ret.write(s)
34         if o != []:
35             buf.seek(pos)
36             s = buf.read(1000)
37             if s == "":
38                 infd.close()
39                 wr_fin = 1
40             else:
41                 cnt = os.write(infd.fileno(), s)
42                 pos += cnt
43     outfd.close()
44     ret.seek(0)
45     return ret