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 |
Neal Norwitz | 42dd86b | 2007-05-11 06:57:33 +0000 | [diff] [blame] | 11 | import warnings |
| 12 | warnings.warn("The popen2 module is deprecated. Use the subprocess module.", |
| 13 | DeprecationWarning, stacklevel=2) |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 14 | |
Skip Montanaro | 57fd45e | 2002-03-12 19:48:03 +0000 | [diff] [blame] | 15 | __all__ = ["popen2", "popen3", "popen4"] |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 16 | |
Martin v. Löwis | f563c8b | 2003-10-06 21:34:33 +0000 | [diff] [blame] | 17 | try: |
| 18 | MAXFD = os.sysconf('SC_OPEN_MAX') |
| 19 | except (AttributeError, ValueError): |
| 20 | MAXFD = 256 |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 22 | _active = [] |
| 23 | |
| 24 | def _cleanup(): |
| 25 | for inst in _active[:]: |
Martin v. Löwis | 478c82d | 2006-03-24 08:14:54 +0000 | [diff] [blame] | 26 | if inst.poll(_deadstate=sys.maxint) >= 0: |
| 27 | try: |
| 28 | _active.remove(inst) |
| 29 | except ValueError: |
| 30 | # This can happen if two threads create a new Popen instance. |
| 31 | # It's harmless that it was already removed, so ignore. |
| 32 | pass |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 33 | |
| 34 | class Popen3: |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 35 | """Class representing a child process. Normally instances are created |
| 36 | by the factory functions popen2() and popen3().""" |
| 37 | |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 38 | sts = -1 # Child not completed yet |
| 39 | |
Fred Drake | b5aa407 | 2003-07-07 21:36:19 +0000 | [diff] [blame] | 40 | def __init__(self, cmd, capturestderr=False, bufsize=-1): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 41 | """The parameter 'cmd' is the shell command to execute in a |
Johannes Gijsbers | 9fc9789 | 2004-10-11 18:12:20 +0000 | [diff] [blame] | 42 | sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments |
| 43 | will be passed directly to the program without shell intervention (as |
| 44 | with os.spawnv()). If 'cmd' is a string it will be passed to the shell |
| 45 | (as with os.system()). The 'capturestderr' flag, if true, specifies |
| 46 | that the object should capture standard error output of the child |
| 47 | process. The default is false. If the 'bufsize' parameter is |
| 48 | specified, it specifies the size of the I/O buffers to/from the child |
| 49 | process.""" |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 50 | _cleanup() |
Martin v. Löwis | bd8dbab | 2006-03-23 18:18:35 +0000 | [diff] [blame] | 51 | self.cmd = cmd |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 52 | p2cread, p2cwrite = os.pipe() |
| 53 | c2pread, c2pwrite = os.pipe() |
| 54 | if capturestderr: |
| 55 | errout, errin = os.pipe() |
| 56 | self.pid = os.fork() |
| 57 | if self.pid == 0: |
| 58 | # Child |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 59 | os.dup2(p2cread, 0) |
| 60 | os.dup2(c2pwrite, 1) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 61 | if capturestderr: |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 62 | os.dup2(errin, 2) |
| 63 | self._run_child(cmd) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 64 | os.close(p2cread) |
| 65 | self.tochild = os.fdopen(p2cwrite, 'w', bufsize) |
| 66 | os.close(c2pwrite) |
| 67 | self.fromchild = os.fdopen(c2pread, 'r', bufsize) |
| 68 | if capturestderr: |
| 69 | os.close(errin) |
| 70 | self.childerr = os.fdopen(errout, 'r', bufsize) |
| 71 | else: |
| 72 | self.childerr = None |
Martin v. Löwis | 478c82d | 2006-03-24 08:14:54 +0000 | [diff] [blame] | 73 | |
| 74 | def __del__(self): |
| 75 | # In case the child hasn't been waited on, check if it's done. |
| 76 | self.poll(_deadstate=sys.maxint) |
| 77 | if self.sts < 0: |
Georg Brandl | 4085f14 | 2006-07-21 17:36:31 +0000 | [diff] [blame] | 78 | if _active is not None: |
Georg Brandl | cfecd59 | 2006-05-25 18:44:09 +0000 | [diff] [blame] | 79 | # Child is still running, keep us alive until we can wait on it. |
| 80 | _active.append(self) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 81 | |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 82 | def _run_child(self, cmd): |
Walter Dörwald | 65230a2 | 2002-06-03 15:58:32 +0000 | [diff] [blame] | 83 | if isinstance(cmd, basestring): |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 84 | cmd = ['/bin/sh', '-c', cmd] |
Peter Astrand | ff355f1 | 2006-06-22 20:21:26 +0000 | [diff] [blame] | 85 | for i in xrange(3, MAXFD): |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 86 | try: |
| 87 | os.close(i) |
Skip Montanaro | 1c90d7a | 2002-03-24 20:48:26 +0000 | [diff] [blame] | 88 | except OSError: |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 89 | pass |
| 90 | try: |
| 91 | os.execvp(cmd[0], cmd) |
| 92 | finally: |
| 93 | os._exit(1) |
| 94 | |
Martin v. Löwis | 478c82d | 2006-03-24 08:14:54 +0000 | [diff] [blame] | 95 | def poll(self, _deadstate=None): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 96 | """Return the exit status of the child process if it has finished, |
| 97 | or -1 if it hasn't finished yet.""" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 98 | if self.sts < 0: |
| 99 | try: |
| 100 | pid, sts = os.waitpid(self.pid, os.WNOHANG) |
Martin v. Löwis | b95caff | 2006-03-24 08:26:26 +0000 | [diff] [blame] | 101 | # pid will be 0 if self.pid hasn't terminated |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 102 | if pid == self.pid: |
| 103 | self.sts = sts |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 104 | except os.error: |
Martin v. Löwis | 478c82d | 2006-03-24 08:14:54 +0000 | [diff] [blame] | 105 | if _deadstate is not None: |
| 106 | self.sts = _deadstate |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 107 | return self.sts |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 109 | def wait(self): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 110 | """Wait for and return the exit status of the child process.""" |
Guido van Rossum | 3800ef7 | 2003-06-02 19:12:01 +0000 | [diff] [blame] | 111 | if self.sts < 0: |
| 112 | pid, sts = os.waitpid(self.pid, 0) |
Martin v. Löwis | b95caff | 2006-03-24 08:26:26 +0000 | [diff] [blame] | 113 | # This used to be a test, but it is believed to be |
| 114 | # always true, so I changed it to an assertion - mvl |
| 115 | assert pid == self.pid |
| 116 | self.sts = sts |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 117 | return self.sts |
Guido van Rossum | caa9f23 | 1997-04-21 14:15:55 +0000 | [diff] [blame] | 118 | |
Fred Drake | 31f182e | 2000-08-28 17:20:05 +0000 | [diff] [blame] | 119 | |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 120 | class Popen4(Popen3): |
| 121 | childerr = None |
| 122 | |
| 123 | def __init__(self, cmd, bufsize=-1): |
| 124 | _cleanup() |
Martin v. Löwis | 478c82d | 2006-03-24 08:14:54 +0000 | [diff] [blame] | 125 | self.cmd = cmd |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 126 | p2cread, p2cwrite = os.pipe() |
| 127 | c2pread, c2pwrite = os.pipe() |
| 128 | self.pid = os.fork() |
| 129 | if self.pid == 0: |
| 130 | # Child |
| 131 | os.dup2(p2cread, 0) |
| 132 | os.dup2(c2pwrite, 1) |
| 133 | os.dup2(c2pwrite, 2) |
| 134 | self._run_child(cmd) |
| 135 | os.close(p2cread) |
| 136 | self.tochild = os.fdopen(p2cwrite, 'w', bufsize) |
| 137 | os.close(c2pwrite) |
| 138 | self.fromchild = os.fdopen(c2pread, 'r', bufsize) |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 139 | |
| 140 | |
Andrew MacIntyre | 5cef571 | 2002-02-24 05:32:32 +0000 | [diff] [blame] | 141 | if sys.platform[:3] == "win" or sys.platform == "os2emx": |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 142 | # Some things don't make sense on non-Unix platforms. |
Tim Peters | d215218 | 2000-10-03 23:07:13 +0000 | [diff] [blame] | 143 | del Popen3, Popen4 |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 144 | |
| 145 | def popen2(cmd, bufsize=-1, mode='t'): |
Johannes Gijsbers | 9fc9789 | 2004-10-11 18:12:20 +0000 | [diff] [blame] | 146 | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
| 147 | be a sequence, in which case arguments will be passed directly to the |
| 148 | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
| 149 | string it will be passed to the shell (as with os.system()). If |
| 150 | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
| 151 | file objects (child_stdout, child_stdin) are returned.""" |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 152 | w, r = os.popen2(cmd, mode, bufsize) |
| 153 | return r, w |
Guido van Rossum | caa9f23 | 1997-04-21 14:15:55 +0000 | [diff] [blame] | 154 | |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 155 | def popen3(cmd, bufsize=-1, mode='t'): |
Johannes Gijsbers | 9fc9789 | 2004-10-11 18:12:20 +0000 | [diff] [blame] | 156 | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
| 157 | be a sequence, in which case arguments will be passed directly to the |
| 158 | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
| 159 | string it will be passed to the shell (as with os.system()). If |
| 160 | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
| 161 | file objects (child_stdout, child_stdin, child_stderr) are returned.""" |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 162 | w, r, e = os.popen3(cmd, mode, bufsize) |
| 163 | return r, w, e |
Fredrik Lundh | bb7eeff | 2000-07-09 17:59:32 +0000 | [diff] [blame] | 164 | |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 165 | def popen4(cmd, bufsize=-1, mode='t'): |
Johannes Gijsbers | 9fc9789 | 2004-10-11 18:12:20 +0000 | [diff] [blame] | 166 | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
| 167 | be a sequence, in which case arguments will be passed directly to the |
| 168 | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
| 169 | string it will be passed to the shell (as with os.system()). If |
| 170 | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
| 171 | file objects (child_stdout_stderr, child_stdin) are returned.""" |
Fredrik Lundh | 9ac81f6 | 2000-07-09 23:35:24 +0000 | [diff] [blame] | 172 | w, r = os.popen4(cmd, mode, bufsize) |
| 173 | return r, w |
| 174 | else: |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 175 | def popen2(cmd, bufsize=-1, mode='t'): |
Johannes Gijsbers | 9fc9789 | 2004-10-11 18:12:20 +0000 | [diff] [blame] | 176 | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
| 177 | be a sequence, in which case arguments will be passed directly to the |
| 178 | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
| 179 | string it will be passed to the shell (as with os.system()). If |
| 180 | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
| 181 | file objects (child_stdout, child_stdin) are returned.""" |
Fred Drake | b5aa407 | 2003-07-07 21:36:19 +0000 | [diff] [blame] | 182 | inst = Popen3(cmd, False, bufsize) |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 183 | return inst.fromchild, inst.tochild |
| 184 | |
| 185 | def popen3(cmd, bufsize=-1, mode='t'): |
Johannes Gijsbers | 9fc9789 | 2004-10-11 18:12:20 +0000 | [diff] [blame] | 186 | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
| 187 | be a sequence, in which case arguments will be passed directly to the |
| 188 | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
| 189 | string it will be passed to the shell (as with os.system()). If |
| 190 | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
| 191 | file objects (child_stdout, child_stdin, child_stderr) are returned.""" |
Fred Drake | b5aa407 | 2003-07-07 21:36:19 +0000 | [diff] [blame] | 192 | inst = Popen3(cmd, True, bufsize) |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 193 | return inst.fromchild, inst.tochild, inst.childerr |
| 194 | |
| 195 | def popen4(cmd, bufsize=-1, mode='t'): |
Johannes Gijsbers | 9fc9789 | 2004-10-11 18:12:20 +0000 | [diff] [blame] | 196 | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
| 197 | be a sequence, in which case arguments will be passed directly to the |
| 198 | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
| 199 | string it will be passed to the shell (as with os.system()). If |
| 200 | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
| 201 | file objects (child_stdout_stderr, child_stdin) are returned.""" |
Fred Drake | d75e63a | 2000-09-28 19:07:53 +0000 | [diff] [blame] | 202 | inst = Popen4(cmd, bufsize) |
| 203 | return inst.fromchild, inst.tochild |
Guido van Rossum | 0357d02 | 1997-08-11 03:27:24 +0000 | [diff] [blame] | 204 | |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 205 | __all__.extend(["Popen3", "Popen4"]) |