From b652330f2a0fcae98a54d0a9044b9c4b2d4c872f Mon Sep 17 00:00:00 2001 From: Henrik Levkowetz Date: Sat, 6 Nov 2010 05:24:02 +0000 Subject: [PATCH] Utility function to execute external functions. - Legacy-Id: 2606 --- ietf/utils/pipe.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 ietf/utils/pipe.py diff --git a/ietf/utils/pipe.py b/ietf/utils/pipe.py new file mode 100644 index 000000000..a82a2aab9 --- /dev/null +++ b/ietf/utils/pipe.py @@ -0,0 +1,30 @@ +# Simplified interface to os.popen3() + +def pipe(cmd, str=None): + from popen2 import Popen3 as Popen + bufsize = 4096 + MAX = 65536*16 + + if str and len(str) > 4096: # XXX: Hardcoded Linux 2.4, 2.6 pipe buffer size + bufsize = len(str) + + pipe = Popen(cmd, True, bufsize) + if str: + pipe.tochild.write(str) + pipe.tochild.close() + + out = "" + err = "" + while True: + str = pipe.fromchild.read() + if str: + out += str + code = pipe.poll() + if code > -1: + err = pipe.childerr.read() + break + if len(out) >= MAX: + err = "Output exceeds %s bytes and has been truncated" + break + + return (code, out, err)