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