Skip to content

Commit b652330

Browse files
committed
Utility function to execute external functions.
- Legacy-Id: 2606
1 parent 443f417 commit b652330

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

ietf/utils/pipe.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Simplified interface to os.popen3()
2+
3+
def pipe(cmd, str=None):
4+
from popen2 import Popen3 as Popen
5+
bufsize = 4096
6+
MAX = 65536*16
7+
8+
if str and len(str) > 4096: # XXX: Hardcoded Linux 2.4, 2.6 pipe buffer size
9+
bufsize = len(str)
10+
11+
pipe = Popen(cmd, True, bufsize)
12+
if str:
13+
pipe.tochild.write(str)
14+
pipe.tochild.close()
15+
16+
out = ""
17+
err = ""
18+
while True:
19+
str = pipe.fromchild.read()
20+
if str:
21+
out += str
22+
code = pipe.poll()
23+
if code > -1:
24+
err = pipe.childerr.read()
25+
break
26+
if len(out) >= MAX:
27+
err = "Output exceeds %s bytes and has been truncated"
28+
break
29+
30+
return (code, out, err)

0 commit comments

Comments
 (0)