Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Spawn a command with pipes to its stdin, stdout, and optionally stderr. |
| 2 | |
| 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. |
| 7 | """ |
| 8 | |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 9 | import os |
| 10 | import sys |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 11 | |
Skip Montanaro | 57fd45e | 2002-03-12 19:48:03 +0000 | [diff] [blame] | 12 | __all__ = ["popen2", "popen3", "popen4"] |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 13 | |
Martin v. Löwis | f563c8b | 2003-10-06 21:34:33 +0000 | [diff] [blame] | 14 | try: |
| 15 | MAXFD = os.sysconf('SC_OPEN_MAX') |
| 16 | except (AttributeError, ValueError): |
| 17 | MAXFD = 256 |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 19 | _active = [] |
| 20 | |
| 21 | def _cleanup(): |
| 22 | for inst in _active[:]: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 23 | inst.poll() |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 24 | |
| 25 | class Popen3: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 26 | """Class representing a child process. Normally instances are created |
| 27 | by the factory functions popen2() and popen3().""" |
| 28 | |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 29 | sts = -1 # Child not completed yet |
| 30 | |
Fred Drake | b5aa407 | 2003-07-07 21:36:19 +0000 | [diff] [blame] | 31 | def __init__(self, cmd, capturestderr=False, bufsize=-1): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 32 | """The parameter 'cmd' is the shell command to execute in a |
| 33 | sub-process. The 'capturestderr' flag, if true, specifies that |
| 34 | the object should capture standard error output of the child process. |
| 35 | The default is false. If the 'bufsize' parameter is specified, it |
| 36 | specifies the size of the I/O buffers to/from the child process.""" |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 37 | _cleanup() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 38 | p2cread, p2cwrite = os.pipe() |
| 39 | c2pread, c2pwrite = os.pipe() |
| 40 | if capturestderr: |
| 41 | errout, errin = os.pipe() |
| 42 | self.pid = os.fork() |
| 43 | if self.pid == 0: |
| 44 | # Child |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 45 | os.dup2(p2cread, 0) |
| 46 | os.dup2(c2pwrite, 1) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 47 | if capturestderr: |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 48 | os.dup2(errin, 2) |
| 49 | self._run_child(cmd) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 50 | os.close(p2cread) |
| 51 | self.tochild = os.fdopen(p2cwrite, 'w', bufsize) |
| 52 | os.close(c2pwrite) |
| 53 | self.fromchild = os.fdopen(c2pread, 'r', bufsize) |
| 54 | if capturestderr: |
| 55 | os.close(errin) |
| 56 | self.childerr = os.fdopen(errout, 'r', bufsize) |
| 57 | else: |
| 58 | self.childerr = None |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 59 | _active.append(self) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 60 | |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 61 | def _run_child(self, cmd): |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 62 | if isinstance(cmd, basestring): |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 63 | cmd = ['/bin/sh', '-c', cmd] |
| 64 | for i in range(3, MAXFD): |
| 65 | try: |
| 66 | os.close(i) |
Skip Montanaro | 1c90d7a | 2002-03-24 20:48:26 +0000 | [diff] [blame] | 67 | except OSError: |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 68 | pass |
| 69 | try: |
| 70 | os.execvp(cmd[0], cmd) |
| 71 | finally: |
| 72 | os._exit(1) |
| 73 | |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 74 | def poll(self): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 75 | """Return the exit status of the child process if it has finished, |
| 76 | or -1 if it hasn't finished yet.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 77 | if self.sts < 0: |
| 78 | try: |
| 79 | pid, sts = os.waitpid(self.pid, os.WNOHANG) |
| 80 | if pid == self.pid: |
| 81 | self.sts = sts |
| 82 | _active.remove(self) |
| 83 | except os.error: |
| 84 | pass |
| 85 | return self.sts |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 87 | def wait(self): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 88 | """Wait for and return the exit status of the child process.""" |
Guido van Rossum | 3800ef7 | 2003-06-02 19:12:01 +0000 | [diff] [blame] | 89 | if self.sts < 0: |
| 90 | pid, sts = os.waitpid(self.pid, 0) |
| 91 | if pid == self.pid: |
| 92 | self.sts = sts |
| 93 | _active.remove(self) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 94 | return self.sts |
Guido van Rossum | caa9f23 | 1997-04-21 14:15:55 +0000 | [diff] [blame] | 95 | |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 96 | |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 97 | class Popen4(Popen3): |
| 98 | childerr = None |
| 99 | |
| 100 | def __init__(self, cmd, bufsize=-1): |
| 101 | _cleanup() |
| 102 | p2cread, p2cwrite = os.pipe() |
| 103 | c2pread, c2pwrite = os.pipe() |
| 104 | self.pid = os.fork() |
| 105 | if self.pid == 0: |
| 106 | # Child |
| 107 | os.dup2(p2cread, 0) |
| 108 | os.dup2(c2pwrite, 1) |
| 109 | os.dup2(c2pwrite, 2) |
| 110 | self._run_child(cmd) |
| 111 | os.close(p2cread) |
| 112 | self.tochild = os.fdopen(p2cwrite, 'w', bufsize) |
| 113 | os.close(c2pwrite) |
| 114 | self.fromchild = os.fdopen(c2pread, 'r', bufsize) |
| 115 | _active.append(self) |
| 116 | |
| 117 | |
Andrew MacIntyre | 5cef571 | 2002-02-24 05:32:32 +0000 | [diff] [blame] | 118 | if sys.platform[:3] == "win" or sys.platform == "os2emx": |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 119 | # Some things don't make sense on non-Unix platforms. |
Tim Peters | d215218 | 2000-10-03 23:07:13 +0000 | [diff] [blame] | 120 | del Popen3, Popen4 |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 121 | |
| 122 | def popen2(cmd, bufsize=-1, mode='t'): |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 123 | """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is |
| 124 | specified, it sets the buffer size for the I/O pipes. The file objects |
| 125 | (child_stdout, child_stdin) are returned.""" |
| 126 | w, r = os.popen2(cmd, mode, bufsize) |
| 127 | return r, w |
Guido van Rossum | caa9f23 | 1997-04-21 14:15:55 +0000 | [diff] [blame] | 128 | |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 129 | def popen3(cmd, bufsize=-1, mode='t'): |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 130 | """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is |
| 131 | specified, it sets the buffer size for the I/O pipes. The file objects |
| 132 | (child_stdout, child_stdin, child_stderr) are returned.""" |
| 133 | w, r, e = os.popen3(cmd, mode, bufsize) |
| 134 | return r, w, e |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 135 | |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 136 | def popen4(cmd, bufsize=-1, mode='t'): |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 137 | """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is |
| 138 | specified, it sets the buffer size for the I/O pipes. The file objects |
| 139 | (child_stdout_stderr, child_stdin) are returned.""" |
| 140 | w, r = os.popen4(cmd, mode, bufsize) |
| 141 | return r, w |
| 142 | else: |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 143 | def popen2(cmd, bufsize=-1, mode='t'): |
| 144 | """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is |
| 145 | specified, it sets the buffer size for the I/O pipes. The file objects |
| 146 | (child_stdout, child_stdin) are returned.""" |
Fred Drake | b5aa407 | 2003-07-07 21:36:19 +0000 | [diff] [blame] | 147 | inst = Popen3(cmd, False, bufsize) |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 148 | return inst.fromchild, inst.tochild |
| 149 | |
| 150 | def popen3(cmd, bufsize=-1, mode='t'): |
| 151 | """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is |
| 152 | specified, it sets the buffer size for the I/O pipes. The file objects |
| 153 | (child_stdout, child_stdin, child_stderr) are returned.""" |
Fred Drake | b5aa407 | 2003-07-07 21:36:19 +0000 | [diff] [blame] | 154 | inst = Popen3(cmd, True, bufsize) |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 155 | return inst.fromchild, inst.tochild, inst.childerr |
| 156 | |
| 157 | def popen4(cmd, bufsize=-1, mode='t'): |
| 158 | """Execute the shell command 'cmd' in a sub-process. If 'bufsize' is |
| 159 | specified, it sets the buffer size for the I/O pipes. The file objects |
| 160 | (child_stdout_stderr, child_stdin) are returned.""" |
| 161 | inst = Popen4(cmd, bufsize) |
| 162 | return inst.fromchild, inst.tochild |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 163 | |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 164 | __all__.extend(["Popen3", "Popen4"]) |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 166 | def _test(): |
Tim Peters | 84f28db | 2000-08-20 05:57:36 +0000 | [diff] [blame] | 167 | cmd = "cat" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 168 | teststr = "ab cd\n" |
Tim Peters | 84f28db | 2000-08-20 05:57:36 +0000 | [diff] [blame] | 169 | if os.name == "nt": |
| 170 | cmd = "more" |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 171 | # "more" doesn't act the same way across Windows flavors, |
| 172 | # sometimes adding an extra newline at the start or the |
| 173 | # end. So we strip whitespace off both ends for comparison. |
| 174 | expected = teststr.strip() |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 175 | print "testing popen2..." |
Tim Peters | 84f28db | 2000-08-20 05:57:36 +0000 | [diff] [blame] | 176 | r, w = popen2(cmd) |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 177 | w.write(teststr) |
| 178 | w.close() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 179 | got = r.read() |
| 180 | if got.strip() != expected: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 181 | raise ValueError("wrote %r read %r" % (teststr, got)) |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 182 | print "testing popen3..." |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 183 | try: |
Tim Peters | 84f28db | 2000-08-20 05:57:36 +0000 | [diff] [blame] | 184 | r, w, e = popen3([cmd]) |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 185 | except: |
Tim Peters | 84f28db | 2000-08-20 05:57:36 +0000 | [diff] [blame] | 186 | r, w, e = popen3(cmd) |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 187 | w.write(teststr) |
| 188 | w.close() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 189 | got = r.read() |
| 190 | if got.strip() != expected: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 191 | raise ValueError("wrote %r read %r" % (teststr, got)) |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 192 | got = e.read() |
| 193 | if got: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 194 | raise ValueError("unexected %r on stderr" % (got,)) |
Guido van Rossum | 068d572 | 1999-04-20 12:27:31 +0000 | [diff] [blame] | 195 | for inst in _active[:]: |
| 196 | inst.wait() |
Tim Peters | 3620857 | 2000-09-01 20:38:55 +0000 | [diff] [blame] | 197 | if _active: |
| 198 | raise ValueError("_active not empty") |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 199 | print "All OK" |
| 200 | |
| 201 | if __name__ == '__main__': |
| 202 | _test() |