"""Spawn a command with pipes to its stdin, stdout, and optionally stderr.
The normal os.popen(cmd, mode) call spawns a shell command and provides a
file interface to just the input or output of the process depending on
whether mode is 'r' or 'w'. This module provides the functions popen2(cmd)
and popen3(cmd) which return two or three pipes to the spawned command.
__all__
= ["popen2", "popen3", "popen4"]
MAXFD
= os
.sysconf('SC_OPEN_MAX')
except (AttributeError, ValueError):
"""Class representing a child process. Normally instances are created
by the factory functions popen2() and popen3()."""
sts
= -1 # Child not completed yet
def __init__(self
, cmd
, capturestderr
=False, bufsize
=-1):
"""The parameter 'cmd' is the shell command to execute in a
sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments
will be passed directly to the program without shell intervention (as
with os.spawnv()). If 'cmd' is a string it will be passed to the shell
(as with os.system()). The 'capturestderr' flag, if true, specifies
that the object should capture standard error output of the child
process. The default is false. If the 'bufsize' parameter is
specified, it specifies the size of the I/O buffers to/from the child
p2cread
, p2cwrite
= os
.pipe()
c2pread
, c2pwrite
= os
.pipe()
errout
, errin
= os
.pipe()
self
.tochild
= os
.fdopen(p2cwrite
, 'w', bufsize
)
self
.fromchild
= os
.fdopen(c2pread
, 'r', bufsize
)
self
.childerr
= os
.fdopen(errout
, 'r', bufsize
)
def _run_child(self
, cmd
):
if isinstance(cmd
, basestring
):
cmd
= ['/bin/sh', '-c', cmd
]
for i
in range(3, MAXFD
):
"""Return the exit status of the child process if it has finished,
or -1 if it hasn't finished yet."""
pid
, sts
= os
.waitpid(self
.pid
, os
.WNOHANG
)
"""Wait for and return the exit status of the child process."""
pid
, sts
= os
.waitpid(self
.pid
, 0)
def __init__(self
, cmd
, bufsize
=-1):
p2cread
, p2cwrite
= os
.pipe()
c2pread
, c2pwrite
= os
.pipe()
self
.tochild
= os
.fdopen(p2cwrite
, 'w', bufsize
)
self
.fromchild
= os
.fdopen(c2pread
, 'r', bufsize
)
if sys
.platform
[:3] == "win" or sys
.platform
== "os2emx":
# Some things don't make sense on non-Unix platforms.
def popen2(cmd
, bufsize
=-1, mode
='t'):
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
be a sequence, in which case arguments will be passed directly to the
program without shell intervention (as with os.spawnv()). If 'cmd' is a
string it will be passed to the shell (as with os.system()). If
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
file objects (child_stdout, child_stdin) are returned."""
w
, r
= os
.popen2(cmd
, mode
, bufsize
)
def popen3(cmd
, bufsize
=-1, mode
='t'):
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
be a sequence, in which case arguments will be passed directly to the
program without shell intervention (as with os.spawnv()). If 'cmd' is a
string it will be passed to the shell (as with os.system()). If
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
file objects (child_stdout, child_stdin, child_stderr) are returned."""
w
, r
, e
= os
.popen3(cmd
, mode
, bufsize
)
def popen4(cmd
, bufsize
=-1, mode
='t'):
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
be a sequence, in which case arguments will be passed directly to the
program without shell intervention (as with os.spawnv()). If 'cmd' is a
string it will be passed to the shell (as with os.system()). If
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
file objects (child_stdout_stderr, child_stdin) are returned."""
w
, r
= os
.popen4(cmd
, mode
, bufsize
)
def popen2(cmd
, bufsize
=-1, mode
='t'):
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
be a sequence, in which case arguments will be passed directly to the
program without shell intervention (as with os.spawnv()). If 'cmd' is a
string it will be passed to the shell (as with os.system()). If
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
file objects (child_stdout, child_stdin) are returned."""
inst
= Popen3(cmd
, False, bufsize
)
return inst
.fromchild
, inst
.tochild
def popen3(cmd
, bufsize
=-1, mode
='t'):
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
be a sequence, in which case arguments will be passed directly to the
program without shell intervention (as with os.spawnv()). If 'cmd' is a
string it will be passed to the shell (as with os.system()). If
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
file objects (child_stdout, child_stdin, child_stderr) are returned."""
inst
= Popen3(cmd
, True, bufsize
)
return inst
.fromchild
, inst
.tochild
, inst
.childerr
def popen4(cmd
, bufsize
=-1, mode
='t'):
"""Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
be a sequence, in which case arguments will be passed directly to the
program without shell intervention (as with os.spawnv()). If 'cmd' is a
string it will be passed to the shell (as with os.system()). If
'bufsize' is specified, it sets the buffer size for the I/O pipes. The
file objects (child_stdout_stderr, child_stdin) are returned."""
inst
= Popen4(cmd
, bufsize
)
return inst
.fromchild
, inst
.tochild
__all__
.extend(["Popen3", "Popen4"])
# "more" doesn't act the same way across Windows flavors,
# sometimes adding an extra newline at the start or the
# end. So we strip whitespace off both ends for comparison.
expected
= teststr
.strip()
print "testing popen2..."
if got
.strip() != expected
:
raise ValueError("wrote %r read %r" % (teststr
, got
))
print "testing popen3..."
if got
.strip() != expected
:
raise ValueError("wrote %r read %r" % (teststr
, got
))
raise ValueError("unexpected %r on stderr" % (got
,))
raise ValueError("_active not empty")
if __name__
== '__main__':