1 """Spawn a command with pipes to its stdin, stdout, and optionally stderr.
3 The normal os.popen(cmd, mode) call spawns a shell command and provides a
4 file interface to just the input or output of the process depending on
5 whether mode is 'r' or 'w'. This module provides the functions popen2(cmd)
6 and popen3(cmd) which return two or three pipes to the spawned command.
12 MAXFD
= 256 # Max number of file descriptors (os.getdtablesize()???)
17 for inst
in _active
[:]:
21 """Class representing a child process. Normally instances are created
22 by the factory functions popen2() and popen3()."""
24 sts
= -1 # Child not completed yet
26 def __init__(self
, cmd
, capturestderr
=0, bufsize
=-1):
27 """The parameter 'cmd' is the shell command to execute in a
28 sub-process. The 'capturestderr' flag, if true, specifies that
29 the object should capture standard error output of the child process.
30 The default is false. If the 'bufsize' parameter is specified, it
31 specifies the size of the I/O buffers to/from the child process."""
33 p2cread
, p2cwrite
= os
.pipe()
34 c2pread
, c2pwrite
= os
.pipe()
36 errout
, errin
= os
.pipe()
46 self
.tochild
= os
.fdopen(p2cwrite
, 'w', bufsize
)
48 self
.fromchild
= os
.fdopen(c2pread
, 'r', bufsize
)
51 self
.childerr
= os
.fdopen(errout
, 'r', bufsize
)
56 def _run_child(self
, cmd
):
57 if type(cmd
) == type(''):
58 cmd
= ['/bin/sh', '-c', cmd
]
59 for i
in range(3, MAXFD
):
65 os
.execvp(cmd
[0], cmd
)
70 """Return the exit status of the child process if it has finished,
71 or -1 if it hasn't finished yet."""
74 pid
, sts
= os
.waitpid(self
.pid
, os
.WNOHANG
)
83 """Wait for and return the exit status of the child process."""
84 pid
, sts
= os
.waitpid(self
.pid
, 0)
94 def __init__(self
, cmd
, bufsize
=-1):
96 p2cread
, p2cwrite
= os
.pipe()
97 c2pread
, c2pwrite
= os
.pipe()
106 self
.tochild
= os
.fdopen(p2cwrite
, 'w', bufsize
)
108 self
.fromchild
= os
.fdopen(c2pread
, 'r', bufsize
)
112 if sys
.platform
[:3] == "win":
113 # Some things don't make sense on non-Unix platforms.
116 def popen2(cmd
, bufsize
=-1, mode
='t'):
117 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
118 specified, it sets the buffer size for the I/O pipes. The file objects
119 (child_stdout, child_stdin) are returned."""
120 w
, r
= os
.popen2(cmd
, mode
, bufsize
)
123 def popen3(cmd
, bufsize
=-1, mode
='t'):
124 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
125 specified, it sets the buffer size for the I/O pipes. The file objects
126 (child_stdout, child_stdin, child_stderr) are returned."""
127 w
, r
, e
= os
.popen3(cmd
, mode
, bufsize
)
130 def popen4(cmd
, bufsize
=-1, mode
='t'):
131 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
132 specified, it sets the buffer size for the I/O pipes. The file objects
133 (child_stdout_stderr, child_stdin) are returned."""
134 w
, r
= os
.popen4(cmd
, mode
, bufsize
)
137 def popen2(cmd
, bufsize
=-1, mode
='t'):
138 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
139 specified, it sets the buffer size for the I/O pipes. The file objects
140 (child_stdout, child_stdin) are returned."""
141 inst
= Popen3(cmd
, 0, bufsize
)
142 return inst
.fromchild
, inst
.tochild
144 def popen3(cmd
, bufsize
=-1, mode
='t'):
145 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
146 specified, it sets the buffer size for the I/O pipes. The file objects
147 (child_stdout, child_stdin, child_stderr) are returned."""
148 inst
= Popen3(cmd
, 1, bufsize
)
149 return inst
.fromchild
, inst
.tochild
, inst
.childerr
151 def popen4(cmd
, bufsize
=-1, mode
='t'):
152 """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is
153 specified, it sets the buffer size for the I/O pipes. The file objects
154 (child_stdout_stderr, child_stdin) are returned."""
155 inst
= Popen4(cmd
, bufsize
)
156 return inst
.fromchild
, inst
.tochild
164 # "more" doesn't act the same way across Windows flavors,
165 # sometimes adding an extra newline at the start or the
166 # end. So we strip whitespace off both ends for comparison.
167 expected
= teststr
.strip()
168 print "testing popen2..."
173 if got
.strip() != expected
:
174 raise ValueError("wrote %s read %s" % (`teststr`
, `got`
))
175 print "testing popen3..."
177 r
, w
, e
= popen3([cmd
])
179 r
, w
, e
= popen3(cmd
)
183 if got
.strip() != expected
:
184 raise ValueError("wrote %s read %s" % (`teststr`
, `got`
))
187 raise ValueError("unexected %s on stderr" % `got`
)
188 for inst
in _active
[:]:
191 raise ValueError("_active not empty")
194 if __name__
== '__main__':