+++ /dev/null
-"""Spawn a command with pipes to its stdin, stdout, and optionally stderr.\r
-\r
-The normal os.popen(cmd, mode) call spawns a shell command and provides a\r
-file interface to just the input or output of the process depending on\r
-whether mode is 'r' or 'w'. This module provides the functions popen2(cmd)\r
-and popen3(cmd) which return two or three pipes to the spawned command.\r
-"""\r
-\r
-import os\r
-import sys\r
-import warnings\r
-warnings.warn("The popen2 module is deprecated. Use the subprocess module.",\r
- DeprecationWarning, stacklevel=2)\r
-\r
-__all__ = ["popen2", "popen3", "popen4"]\r
-\r
-try:\r
- MAXFD = os.sysconf('SC_OPEN_MAX')\r
-except (AttributeError, ValueError):\r
- MAXFD = 256\r
-\r
-_active = []\r
-\r
-def _cleanup():\r
- for inst in _active[:]:\r
- if inst.poll(_deadstate=sys.maxint) >= 0:\r
- try:\r
- _active.remove(inst)\r
- except ValueError:\r
- # This can happen if two threads create a new Popen instance.\r
- # It's harmless that it was already removed, so ignore.\r
- pass\r
-\r
-class Popen3:\r
- """Class representing a child process. Normally, instances are created\r
- internally by the functions popen2() and popen3()."""\r
-\r
- sts = -1 # Child not completed yet\r
-\r
- def __init__(self, cmd, capturestderr=False, bufsize=-1):\r
- """The parameter 'cmd' is the shell command to execute in a\r
- sub-process. On UNIX, 'cmd' may be a sequence, in which case arguments\r
- will be passed directly to the program without shell intervention (as\r
- with os.spawnv()). If 'cmd' is a string it will be passed to the shell\r
- (as with os.system()). The 'capturestderr' flag, if true, specifies\r
- that the object should capture standard error output of the child\r
- process. The default is false. If the 'bufsize' parameter is\r
- specified, it specifies the size of the I/O buffers to/from the child\r
- process."""\r
- _cleanup()\r
- self.cmd = cmd\r
- p2cread, p2cwrite = os.pipe()\r
- c2pread, c2pwrite = os.pipe()\r
- if capturestderr:\r
- errout, errin = os.pipe()\r
- self.pid = os.fork()\r
- if self.pid == 0:\r
- # Child\r
- os.dup2(p2cread, 0)\r
- os.dup2(c2pwrite, 1)\r
- if capturestderr:\r
- os.dup2(errin, 2)\r
- self._run_child(cmd)\r
- os.close(p2cread)\r
- self.tochild = os.fdopen(p2cwrite, 'w', bufsize)\r
- os.close(c2pwrite)\r
- self.fromchild = os.fdopen(c2pread, 'r', bufsize)\r
- if capturestderr:\r
- os.close(errin)\r
- self.childerr = os.fdopen(errout, 'r', bufsize)\r
- else:\r
- self.childerr = None\r
-\r
- def __del__(self):\r
- # In case the child hasn't been waited on, check if it's done.\r
- self.poll(_deadstate=sys.maxint)\r
- if self.sts < 0:\r
- if _active is not None:\r
- # Child is still running, keep us alive until we can wait on it.\r
- _active.append(self)\r
-\r
- def _run_child(self, cmd):\r
- if isinstance(cmd, basestring):\r
- cmd = ['/bin/sh', '-c', cmd]\r
- os.closerange(3, MAXFD)\r
- try:\r
- os.execvp(cmd[0], cmd)\r
- finally:\r
- os._exit(1)\r
-\r
- def poll(self, _deadstate=None):\r
- """Return the exit status of the child process if it has finished,\r
- or -1 if it hasn't finished yet."""\r
- if self.sts < 0:\r
- try:\r
- pid, sts = os.waitpid(self.pid, os.WNOHANG)\r
- # pid will be 0 if self.pid hasn't terminated\r
- if pid == self.pid:\r
- self.sts = sts\r
- except os.error:\r
- if _deadstate is not None:\r
- self.sts = _deadstate\r
- return self.sts\r
-\r
- def wait(self):\r
- """Wait for and return the exit status of the child process."""\r
- if self.sts < 0:\r
- pid, sts = os.waitpid(self.pid, 0)\r
- # This used to be a test, but it is believed to be\r
- # always true, so I changed it to an assertion - mvl\r
- assert pid == self.pid\r
- self.sts = sts\r
- return self.sts\r
-\r
-\r
-class Popen4(Popen3):\r
- childerr = None\r
-\r
- def __init__(self, cmd, bufsize=-1):\r
- _cleanup()\r
- self.cmd = cmd\r
- p2cread, p2cwrite = os.pipe()\r
- c2pread, c2pwrite = os.pipe()\r
- self.pid = os.fork()\r
- if self.pid == 0:\r
- # Child\r
- os.dup2(p2cread, 0)\r
- os.dup2(c2pwrite, 1)\r
- os.dup2(c2pwrite, 2)\r
- self._run_child(cmd)\r
- os.close(p2cread)\r
- self.tochild = os.fdopen(p2cwrite, 'w', bufsize)\r
- os.close(c2pwrite)\r
- self.fromchild = os.fdopen(c2pread, 'r', bufsize)\r
-\r
-\r
-if sys.platform[:3] == "win" or sys.platform == "os2emx":\r
- # Some things don't make sense on non-Unix platforms.\r
- del Popen3, Popen4\r
-\r
- def popen2(cmd, bufsize=-1, mode='t'):\r
- """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
- be a sequence, in which case arguments will be passed directly to the\r
- program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
- string it will be passed to the shell (as with os.system()). If\r
- 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
- file objects (child_stdout, child_stdin) are returned."""\r
- w, r = os.popen2(cmd, mode, bufsize)\r
- return r, w\r
-\r
- def popen3(cmd, bufsize=-1, mode='t'):\r
- """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
- be a sequence, in which case arguments will be passed directly to the\r
- program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
- string it will be passed to the shell (as with os.system()). If\r
- 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
- file objects (child_stdout, child_stdin, child_stderr) are returned."""\r
- w, r, e = os.popen3(cmd, mode, bufsize)\r
- return r, w, e\r
-\r
- def popen4(cmd, bufsize=-1, mode='t'):\r
- """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
- be a sequence, in which case arguments will be passed directly to the\r
- program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
- string it will be passed to the shell (as with os.system()). If\r
- 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
- file objects (child_stdout_stderr, child_stdin) are returned."""\r
- w, r = os.popen4(cmd, mode, bufsize)\r
- return r, w\r
-else:\r
- def popen2(cmd, bufsize=-1, mode='t'):\r
- """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
- be a sequence, in which case arguments will be passed directly to the\r
- program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
- string it will be passed to the shell (as with os.system()). If\r
- 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
- file objects (child_stdout, child_stdin) are returned."""\r
- inst = Popen3(cmd, False, bufsize)\r
- return inst.fromchild, inst.tochild\r
-\r
- def popen3(cmd, bufsize=-1, mode='t'):\r
- """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
- be a sequence, in which case arguments will be passed directly to the\r
- program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
- string it will be passed to the shell (as with os.system()). If\r
- 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
- file objects (child_stdout, child_stdin, child_stderr) are returned."""\r
- inst = Popen3(cmd, True, bufsize)\r
- return inst.fromchild, inst.tochild, inst.childerr\r
-\r
- def popen4(cmd, bufsize=-1, mode='t'):\r
- """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may\r
- be a sequence, in which case arguments will be passed directly to the\r
- program without shell intervention (as with os.spawnv()). If 'cmd' is a\r
- string it will be passed to the shell (as with os.system()). If\r
- 'bufsize' is specified, it sets the buffer size for the I/O pipes. The\r
- file objects (child_stdout_stderr, child_stdin) are returned."""\r
- inst = Popen4(cmd, bufsize)\r
- return inst.fromchild, inst.tochild\r
-\r
- __all__.extend(["Popen3", "Popen4"])\r