| 1 | n/a | """Spawn a command with pipes to its stdin, stdout, and optionally stderr. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | The normal os.popen(cmd, mode) call spawns a shell command and provides a |
|---|
| 4 | n/a | file interface to just the input or output of the process depending on |
|---|
| 5 | n/a | whether mode is 'r' or 'w'. This module provides the functions popen2(cmd) |
|---|
| 6 | n/a | and popen3(cmd) which return two or three pipes to the spawned command. |
|---|
| 7 | 1 | """ |
|---|
| 8 | n/a | |
|---|
| 9 | 1 | import os |
|---|
| 10 | 1 | import sys |
|---|
| 11 | 1 | import warnings |
|---|
| 12 | 1 | warnings.warn("The popen2 module is deprecated. Use the subprocess module.", |
|---|
| 13 | 1 | DeprecationWarning, stacklevel=2) |
|---|
| 14 | n/a | |
|---|
| 15 | 1 | __all__ = ["popen2", "popen3", "popen4"] |
|---|
| 16 | n/a | |
|---|
| 17 | 1 | try: |
|---|
| 18 | 1 | MAXFD = os.sysconf('SC_OPEN_MAX') |
|---|
| 19 | 0 | except (AttributeError, ValueError): |
|---|
| 20 | 0 | MAXFD = 256 |
|---|
| 21 | n/a | |
|---|
| 22 | 1 | _active = [] |
|---|
| 23 | n/a | |
|---|
| 24 | 1 | def _cleanup(): |
|---|
| 25 | 16 | for inst in _active[:]: |
|---|
| 26 | 3 | if inst.poll(_deadstate=sys.maxint) >= 0: |
|---|
| 27 | 3 | try: |
|---|
| 28 | 3 | _active.remove(inst) |
|---|
| 29 | 0 | except ValueError: |
|---|
| 30 | n/a | # This can happen if two threads create a new Popen instance. |
|---|
| 31 | n/a | # It's harmless that it was already removed, so ignore. |
|---|
| 32 | 0 | pass |
|---|
| 33 | n/a | |
|---|
| 34 | 2 | class Popen3: |
|---|
| 35 | n/a | """Class representing a child process. Normally, instances are created |
|---|
| 36 | 1 | internally by the functions popen2() and popen3().""" |
|---|
| 37 | n/a | |
|---|
| 38 | 1 | sts = -1 # Child not completed yet |
|---|
| 39 | n/a | |
|---|
| 40 | 1 | def __init__(self, cmd, capturestderr=False, bufsize=-1): |
|---|
| 41 | n/a | """The parameter 'cmd' is the shell command to execute in a |
|---|
| 42 | n/a | sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments |
|---|
| 43 | n/a | will be passed directly to the program without shell intervention (as |
|---|
| 44 | n/a | with os.spawnv()). If 'cmd' is a string it will be passed to the shell |
|---|
| 45 | n/a | (as with os.system()). The 'capturestderr' flag, if true, specifies |
|---|
| 46 | n/a | that the object should capture standard error output of the child |
|---|
| 47 | n/a | process. The default is false. If the 'bufsize' parameter is |
|---|
| 48 | n/a | specified, it specifies the size of the I/O buffers to/from the child |
|---|
| 49 | n/a | process.""" |
|---|
| 50 | 3 | _cleanup() |
|---|
| 51 | 3 | self.cmd = cmd |
|---|
| 52 | 3 | p2cread, p2cwrite = os.pipe() |
|---|
| 53 | 3 | c2pread, c2pwrite = os.pipe() |
|---|
| 54 | 3 | if capturestderr: |
|---|
| 55 | 2 | errout, errin = os.pipe() |
|---|
| 56 | 3 | self.pid = os.fork() |
|---|
| 57 | 3 | if self.pid == 0: |
|---|
| 58 | n/a | # Child |
|---|
| 59 | 0 | os.dup2(p2cread, 0) |
|---|
| 60 | 0 | os.dup2(c2pwrite, 1) |
|---|
| 61 | 0 | if capturestderr: |
|---|
| 62 | 0 | os.dup2(errin, 2) |
|---|
| 63 | 0 | self._run_child(cmd) |
|---|
| 64 | 3 | os.close(p2cread) |
|---|
| 65 | 3 | self.tochild = os.fdopen(p2cwrite, 'w', bufsize) |
|---|
| 66 | 3 | os.close(c2pwrite) |
|---|
| 67 | 3 | self.fromchild = os.fdopen(c2pread, 'r', bufsize) |
|---|
| 68 | 3 | if capturestderr: |
|---|
| 69 | 2 | os.close(errin) |
|---|
| 70 | 2 | self.childerr = os.fdopen(errout, 'r', bufsize) |
|---|
| 71 | n/a | else: |
|---|
| 72 | 1 | self.childerr = None |
|---|
| 73 | n/a | |
|---|
| 74 | 1 | def __del__(self): |
|---|
| 75 | n/a | # In case the child hasn't been waited on, check if it's done. |
|---|
| 76 | 6 | self.poll(_deadstate=sys.maxint) |
|---|
| 77 | 6 | if self.sts < 0: |
|---|
| 78 | 3 | if _active is not None: |
|---|
| 79 | n/a | # Child is still running, keep us alive until we can wait on it. |
|---|
| 80 | 3 | _active.append(self) |
|---|
| 81 | n/a | |
|---|
| 82 | 1 | def _run_child(self, cmd): |
|---|
| 83 | 0 | if isinstance(cmd, basestring): |
|---|
| 84 | 0 | cmd = ['/bin/sh', '-c', cmd] |
|---|
| 85 | 0 | os.closerange(3, MAXFD) |
|---|
| 86 | 0 | try: |
|---|
| 87 | 0 | os.execvp(cmd[0], cmd) |
|---|
| 88 | n/a | finally: |
|---|
| 89 | 0 | os._exit(1) |
|---|
| 90 | n/a | |
|---|
| 91 | 1 | def poll(self, _deadstate=None): |
|---|
| 92 | n/a | """Return the exit status of the child process if it has finished, |
|---|
| 93 | n/a | or -1 if it hasn't finished yet.""" |
|---|
| 94 | 9 | if self.sts < 0: |
|---|
| 95 | 4 | try: |
|---|
| 96 | 4 | pid, sts = os.waitpid(self.pid, os.WNOHANG) |
|---|
| 97 | n/a | # pid will be 0 if self.pid hasn't terminated |
|---|
| 98 | 4 | if pid == self.pid: |
|---|
| 99 | 1 | self.sts = sts |
|---|
| 100 | 0 | except os.error: |
|---|
| 101 | 0 | if _deadstate is not None: |
|---|
| 102 | 0 | self.sts = _deadstate |
|---|
| 103 | 9 | return self.sts |
|---|
| 104 | n/a | |
|---|
| 105 | 1 | def wait(self): |
|---|
| 106 | n/a | """Wait for and return the exit status of the child process.""" |
|---|
| 107 | 2 | if self.sts < 0: |
|---|
| 108 | 2 | pid, sts = os.waitpid(self.pid, 0) |
|---|
| 109 | n/a | # This used to be a test, but it is believed to be |
|---|
| 110 | n/a | # always true, so I changed it to an assertion - mvl |
|---|
| 111 | 2 | assert pid == self.pid |
|---|
| 112 | 2 | self.sts = sts |
|---|
| 113 | 2 | return self.sts |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | |
|---|
| 116 | 2 | class Popen4(Popen3): |
|---|
| 117 | 1 | childerr = None |
|---|
| 118 | n/a | |
|---|
| 119 | 1 | def __init__(self, cmd, bufsize=-1): |
|---|
| 120 | 0 | _cleanup() |
|---|
| 121 | 0 | self.cmd = cmd |
|---|
| 122 | 0 | p2cread, p2cwrite = os.pipe() |
|---|
| 123 | 0 | c2pread, c2pwrite = os.pipe() |
|---|
| 124 | 0 | self.pid = os.fork() |
|---|
| 125 | 0 | if self.pid == 0: |
|---|
| 126 | n/a | # Child |
|---|
| 127 | 0 | os.dup2(p2cread, 0) |
|---|
| 128 | 0 | os.dup2(c2pwrite, 1) |
|---|
| 129 | 0 | os.dup2(c2pwrite, 2) |
|---|
| 130 | 0 | self._run_child(cmd) |
|---|
| 131 | 0 | os.close(p2cread) |
|---|
| 132 | 0 | self.tochild = os.fdopen(p2cwrite, 'w', bufsize) |
|---|
| 133 | 0 | os.close(c2pwrite) |
|---|
| 134 | 0 | self.fromchild = os.fdopen(c2pread, 'r', bufsize) |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | |
|---|
| 137 | 1 | if sys.platform[:3] == "win" or sys.platform == "os2emx": |
|---|
| 138 | n/a | # Some things don't make sense on non-Unix platforms. |
|---|
| 139 | 0 | del Popen3, Popen4 |
|---|
| 140 | n/a | |
|---|
| 141 | 0 | def popen2(cmd, bufsize=-1, mode='t'): |
|---|
| 142 | n/a | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
|---|
| 143 | n/a | be a sequence, in which case arguments will be passed directly to the |
|---|
| 144 | n/a | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
|---|
| 145 | n/a | string it will be passed to the shell (as with os.system()). If |
|---|
| 146 | n/a | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
|---|
| 147 | n/a | file objects (child_stdout, child_stdin) are returned.""" |
|---|
| 148 | 0 | w, r = os.popen2(cmd, mode, bufsize) |
|---|
| 149 | 0 | return r, w |
|---|
| 150 | n/a | |
|---|
| 151 | 0 | def popen3(cmd, bufsize=-1, mode='t'): |
|---|
| 152 | n/a | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
|---|
| 153 | n/a | be a sequence, in which case arguments will be passed directly to the |
|---|
| 154 | n/a | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
|---|
| 155 | n/a | string it will be passed to the shell (as with os.system()). If |
|---|
| 156 | n/a | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
|---|
| 157 | n/a | file objects (child_stdout, child_stdin, child_stderr) are returned.""" |
|---|
| 158 | 0 | w, r, e = os.popen3(cmd, mode, bufsize) |
|---|
| 159 | 0 | return r, w, e |
|---|
| 160 | n/a | |
|---|
| 161 | 0 | def popen4(cmd, bufsize=-1, mode='t'): |
|---|
| 162 | n/a | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
|---|
| 163 | n/a | be a sequence, in which case arguments will be passed directly to the |
|---|
| 164 | n/a | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
|---|
| 165 | n/a | string it will be passed to the shell (as with os.system()). If |
|---|
| 166 | n/a | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
|---|
| 167 | n/a | file objects (child_stdout_stderr, child_stdin) are returned.""" |
|---|
| 168 | 0 | w, r = os.popen4(cmd, mode, bufsize) |
|---|
| 169 | 0 | return r, w |
|---|
| 170 | n/a | else: |
|---|
| 171 | 1 | def popen2(cmd, bufsize=-1, mode='t'): |
|---|
| 172 | n/a | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
|---|
| 173 | n/a | be a sequence, in which case arguments will be passed directly to the |
|---|
| 174 | n/a | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
|---|
| 175 | n/a | string it will be passed to the shell (as with os.system()). If |
|---|
| 176 | n/a | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
|---|
| 177 | n/a | file objects (child_stdout, child_stdin) are returned.""" |
|---|
| 178 | 1 | inst = Popen3(cmd, False, bufsize) |
|---|
| 179 | 1 | return inst.fromchild, inst.tochild |
|---|
| 180 | n/a | |
|---|
| 181 | 1 | def popen3(cmd, bufsize=-1, mode='t'): |
|---|
| 182 | n/a | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
|---|
| 183 | n/a | be a sequence, in which case arguments will be passed directly to the |
|---|
| 184 | n/a | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
|---|
| 185 | n/a | string it will be passed to the shell (as with os.system()). If |
|---|
| 186 | n/a | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
|---|
| 187 | n/a | file objects (child_stdout, child_stdin, child_stderr) are returned.""" |
|---|
| 188 | 2 | inst = Popen3(cmd, True, bufsize) |
|---|
| 189 | 2 | return inst.fromchild, inst.tochild, inst.childerr |
|---|
| 190 | n/a | |
|---|
| 191 | 1 | def popen4(cmd, bufsize=-1, mode='t'): |
|---|
| 192 | n/a | """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may |
|---|
| 193 | n/a | be a sequence, in which case arguments will be passed directly to the |
|---|
| 194 | n/a | program without shell intervention (as with os.spawnv()). If 'cmd' is a |
|---|
| 195 | n/a | string it will be passed to the shell (as with os.system()). If |
|---|
| 196 | n/a | 'bufsize' is specified, it sets the buffer size for the I/O pipes. The |
|---|
| 197 | n/a | file objects (child_stdout_stderr, child_stdin) are returned.""" |
|---|
| 198 | 0 | inst = Popen4(cmd, bufsize) |
|---|
| 199 | 0 | return inst.fromchild, inst.tochild |
|---|
| 200 | n/a | |
|---|
| 201 | 1 | __all__.extend(["Popen3", "Popen4"]) |
|---|