10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""Spawn a command with pipes to its stdin, stdout, and optionally stderr.
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30a8c90248264a8b26970b4473770bcc3df8515fJosh GaoThe normal os.popen(cmd, mode) call spawns a shell command and provides a
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofile interface to just the input or output of the process depending on
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaowhether mode is 'r' or 'w'.  This module provides the functions popen2(cmd)
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoand popen3(cmd) which return two or three pipes to the spawned command.
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport os
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport warnings
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaowarnings.warn("The popen2 module is deprecated.  Use the subprocess module.",
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao              DeprecationWarning, stacklevel=2)
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__all__ = ["popen2", "popen3", "popen4"]
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry:
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    MAXFD = os.sysconf('SC_OPEN_MAX')
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept (AttributeError, ValueError):
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    MAXFD = 256
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_active = []
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _cleanup():
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for inst in _active[:]:
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if inst.poll(_deadstate=sys.maxint) >= 0:
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                _active.remove(inst)
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except ValueError:
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # This can happen if two threads create a new Popen instance.
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # It's harmless that it was already removed, so ignore.
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Popen3:
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Class representing a child process.  Normally, instances are created
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    internally by the functions popen2() and popen3()."""
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    sts = -1                    # Child not completed yet
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, cmd, capturestderr=False, bufsize=-1):
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """The parameter 'cmd' is the shell command to execute in a
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sub-process.  On UNIX, 'cmd' may be a sequence, in which case arguments
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        will be passed directly to the program without shell intervention (as
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with os.spawnv()).  If 'cmd' is a string it will be passed to the shell
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (as with os.system()).   The 'capturestderr' flag, if true, specifies
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        that the object should capture standard error output of the child
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        process.  The default is false.  If the 'bufsize' parameter is
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        specified, it specifies the size of the I/O buffers to/from the child
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        process."""
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        _cleanup()
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cmd = cmd
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        p2cread, p2cwrite = os.pipe()
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        c2pread, c2pwrite = os.pipe()
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if capturestderr:
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            errout, errin = os.pipe()
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.pid = os.fork()
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.pid == 0:
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Child
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.dup2(p2cread, 0)
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.dup2(c2pwrite, 1)
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if capturestderr:
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                os.dup2(errin, 2)
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._run_child(cmd)
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        os.close(p2cread)
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        os.close(c2pwrite)
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.fromchild = os.fdopen(c2pread, 'r', bufsize)
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if capturestderr:
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.close(errin)
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.childerr = os.fdopen(errout, 'r', bufsize)
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.childerr = None
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __del__(self):
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # In case the child hasn't been waited on, check if it's done.
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.poll(_deadstate=sys.maxint)
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.sts < 0:
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if _active is not None:
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # Child is still running, keep us alive until we can wait on it.
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                _active.append(self)
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _run_child(self, cmd):
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(cmd, basestring):
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cmd = ['/bin/sh', '-c', cmd]
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        os.closerange(3, MAXFD)
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.execvp(cmd[0], cmd)
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os._exit(1)
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def poll(self, _deadstate=None):
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return the exit status of the child process if it has finished,
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        or -1 if it hasn't finished yet."""
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.sts < 0:
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pid, sts = os.waitpid(self.pid, os.WNOHANG)
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # pid will be 0 if self.pid hasn't terminated
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if pid == self.pid:
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.sts = sts
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except os.error:
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if _deadstate is not None:
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.sts = _deadstate
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.sts
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def wait(self):
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Wait for and return the exit status of the child process."""
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.sts < 0:
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pid, sts = os.waitpid(self.pid, 0)
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # This used to be a test, but it is believed to be
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # always true, so I changed it to an assertion - mvl
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            assert pid == self.pid
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.sts = sts
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.sts
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Popen4(Popen3):
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    childerr = None
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, cmd, bufsize=-1):
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        _cleanup()
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cmd = cmd
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        p2cread, p2cwrite = os.pipe()
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        c2pread, c2pwrite = os.pipe()
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.pid = os.fork()
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.pid == 0:
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Child
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.dup2(p2cread, 0)
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.dup2(c2pwrite, 1)
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            os.dup2(c2pwrite, 2)
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._run_child(cmd)
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        os.close(p2cread)
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.tochild = os.fdopen(p2cwrite, 'w', bufsize)
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        os.close(c2pwrite)
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.fromchild = os.fdopen(c2pread, 'r', bufsize)
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif sys.platform[:3] == "win" or sys.platform == "os2emx":
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Some things don't make sense on non-Unix platforms.
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    del Popen3, Popen4
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def popen2(cmd, bufsize=-1, mode='t'):
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        be a sequence, in which case arguments will be passed directly to the
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        program without shell intervention (as with os.spawnv()). If 'cmd' is a
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        string it will be passed to the shell (as with os.system()). If
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        file objects (child_stdout, child_stdin) are returned."""
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        w, r = os.popen2(cmd, mode, bufsize)
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return r, w
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def popen3(cmd, bufsize=-1, mode='t'):
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        be a sequence, in which case arguments will be passed directly to the
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        program without shell intervention (as with os.spawnv()). If 'cmd' is a
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        string it will be passed to the shell (as with os.system()). If
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        file objects (child_stdout, child_stdin, child_stderr) are returned."""
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        w, r, e = os.popen3(cmd, mode, bufsize)
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return r, w, e
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def popen4(cmd, bufsize=-1, mode='t'):
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        be a sequence, in which case arguments will be passed directly to the
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        program without shell intervention (as with os.spawnv()). If 'cmd' is a
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        string it will be passed to the shell (as with os.system()). If
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        file objects (child_stdout_stderr, child_stdin) are returned."""
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        w, r = os.popen4(cmd, mode, bufsize)
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return r, w
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoelse:
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def popen2(cmd, bufsize=-1, mode='t'):
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        be a sequence, in which case arguments will be passed directly to the
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        program without shell intervention (as with os.spawnv()). If 'cmd' is a
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        string it will be passed to the shell (as with os.system()). If
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        file objects (child_stdout, child_stdin) are returned."""
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        inst = Popen3(cmd, False, bufsize)
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return inst.fromchild, inst.tochild
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def popen3(cmd, bufsize=-1, mode='t'):
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        be a sequence, in which case arguments will be passed directly to the
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        program without shell intervention (as with os.spawnv()). If 'cmd' is a
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        string it will be passed to the shell (as with os.system()). If
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        file objects (child_stdout, child_stdin, child_stderr) are returned."""
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        inst = Popen3(cmd, True, bufsize)
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return inst.fromchild, inst.tochild, inst.childerr
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def popen4(cmd, bufsize=-1, mode='t'):
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Execute the shell command 'cmd' in a sub-process. On UNIX, 'cmd' may
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        be a sequence, in which case arguments will be passed directly to the
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        program without shell intervention (as with os.spawnv()). If 'cmd' is a
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        string it will be passed to the shell (as with os.system()). If
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'bufsize' is specified, it sets the buffer size for the I/O pipes. The
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        file objects (child_stdout_stderr, child_stdin) are returned."""
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        inst = Popen4(cmd, bufsize)
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return inst.fromchild, inst.tochild
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    __all__.extend(["Popen3", "Popen4"])
202