]> TLD Linux GIT Repositories - tld-builder.git/blob - TLD_Builder/pipeutil.py
- more python3 fixes, dropped python2 support
[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 from io import StringIO
7
8 def rw_pipe(buf_, infd, outfd):
9     buf = StringIO()
10     buf.write(buf_.read())
11     ret = StringIO()
12     pos = 0
13     rd_fin = 0
14     wr_fin = 0
15     buf.seek(pos)
16     while not (rd_fin and wr_fin):
17         if wr_fin:
18             o = []
19         else:
20             o = [infd]
21         if rd_fin:
22             i = []
23         else:
24             i = [outfd]
25         i, o, e = select.select(i, o, [])
26         if i != []:
27             s = os.read(outfd.fileno(), 1000)
28             if s == "":
29                 rd_fin = 1
30             ret.write(s)
31         if o != []:
32             buf.seek(pos)
33             s = buf.read(1000)
34             if s == "":
35                 infd.close()
36                 wr_fin = 1
37             else:
38                 cnt = os.write(infd.fileno(), s)
39                 pos += cnt
40     outfd.close()
41     ret.seek(0)
42     return ret