12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)"""Pexpect is a Python module for spawning child applications and controlling
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)them automatically. Pexpect can be used for automating interactive applications
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scripts for duplicating software package installations on different servers. It
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)can be used for automated software testing. Pexpect is in the spirit of Don
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Libes' Expect, but Pexpect is pure Python. Other Expect-like modules for Python
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)require TCL and Expect or require C extensions to be compiled. Pexpect does not
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)use C, Expect, or TCL extensions. It should work on any platform that supports
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)the standard Python pty module. The Pexpect interface focuses on ease of use so
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)that simple tasks are easy.
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)There are two main interfaces to the Pexpect system; these are the function,
1358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochrun() and the class, spawn. The spawn class is more powerful. The run()
1458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochfunction is simpler than spawn, and is good for quickly calling program. When
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)you call the run() function it executes a given program and then returns the
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)output. This is a handy replacement for os.system().
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)For example::
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    pexpect.run('ls -la')
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)The spawn class is the more powerful interface to the Pexpect system. You can
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)use this to spawn a child program then interact with it by sending input and
247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)expecting responses (waiting for patterns in the child's output).
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
267dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochFor example::
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    child = pexpect.spawn('scp foo user@example.com:.')
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    child.expect('Password:')
307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    child.sendline(mypassword)
317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)This works even for commands that ask for passwords or other input outside of
33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochthe normal stdio streams. For example, ssh reads input directly from the TTY
34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochdevice which bypasses stdin.
35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochCredits: Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett,
37eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochRobert Stone, Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids
38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvander Molen, George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin,
39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochJacques-Etienne Baudoux, Geoffrey Marshall, Francisco Lourenco, Glen Mabey,
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochKarthik Gurusamy, Fernando Perez, Corey Minyard, Jon Cohen, Guillaume
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochChazarain, Andrew Ryan, Nick Craig-Wood, Andrew Stone, Jorgen Grahn, John
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochSpiegel, Jan Grant, and Shane Kerr. Let me know if I forgot anyone.
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochPexpect is free, open source, and all that good stuff.
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochhttp://pexpect.sourceforge.net/
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochPEXPECT LICENSE
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    This license is approved by the OSI and FSF as GPL-compatible.
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        http://opensource.org/licenses/isc-license.txt
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    Copyright (c) 2012, Noah Spurrier <noah@noah.org>
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
5458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
5558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
5758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
5858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
5958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
6158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
6258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
6458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch"""
6558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)try:
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    import os
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    import sys
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    import time
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    import select
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    import string
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    import re
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    import struct
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    import resource
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    import types
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    import pty
7758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    import tty
7858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    import termios
7958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    import fcntl
8058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    import errno
8158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    import traceback
8258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    import signal
8358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochexcept ImportError as e:
8458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    raise ImportError(str(e) + """
8558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
8658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben MurdochA critical module was not found. Probably this operating system does not
8758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochsupport it. Pexpect is intended for UNIX-like operating systems.""")
8858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
8958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch__version__ = '2.6'
9058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch__revision__ = '1'
9158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'run', 'which',
9258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    'split_command_line', '__version__', '__revision__']
9358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
9458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
9558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch# Exception classes used by this module.
9658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochclass ExceptionPexpect(Exception):
9758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
9858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    """Base class for all exceptions raised by this module.
9958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    """
10058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
10158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    def __init__(self, value):
10258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
10358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        self.value = value
10458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
10558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    def __str__(self):
10658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
10758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        return str(self.value)
10858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
10958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    def get_trace(self):
11058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
11158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        """This returns an abbreviated stack trace with lines that only concern
11258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        the caller. In other words, the stack trace inside the Pexpect module
11358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        is not included. """
11458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
11558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        tblist = traceback.extract_tb(sys.exc_info()[2])
11658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        #tblist = filter(self.__filter_not_pexpect, tblist)
11758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        tblist = [item for item in tblist if self.__filter_not_pexpect(item)]
11858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        tblist = traceback.format_list(tblist)
11958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        return ''.join(tblist)
12058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
12158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    def __filter_not_pexpect(self, trace_list_item):
12258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
12358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        """This returns True if list item 0 the string 'pexpect.py' in it. """
12458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
12558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        if trace_list_item[0].find('pexpect.py') == -1:
12658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch            return True
12758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        else:
12858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch            return False
12958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
13058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
13158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochclass EOF(ExceptionPexpect):
13258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
13358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    """Raised when EOF is read from a child.
13458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    This usually means the child has exited."""
13558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
13658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
13758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdochclass TIMEOUT(ExceptionPexpect):
13858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
13958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    """Raised when a read time exceeds the timeout. """
14058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)##class TIMEOUT_PATTERN(TIMEOUT):
142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)##    """Raised when the pattern match time exceeds the timeout.
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)##    This is different than a read TIMEOUT because the child process may
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)##    give output, thus never give a TIMEOUT, but the output
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)##    may never match a pattern.
1467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch##    """
1477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch##class MAXBUFFER(ExceptionPexpect):
1487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch##    """Raised when a buffer fills before matching an expected pattern."""
1497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
150424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochdef run(command, timeout=-1, withexitstatus=False, events=None,
1527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        extra_args=None, logfile=None, cwd=None, env=None):
1537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    """
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    This function runs the given command; waits for it to finish; then
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    returns all output as a string. STDERR is included in output. If the full
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    path to the command is not given then the path is searched.
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Note that lines are terminated by CR/LF (\\r\\n) combination even on
1603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    UNIX-like systems because this is the standard for pseudottys. If you set
1613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    'withexitstatus' to true, then run will return a tuple of (command_output,
1623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    exitstatus). If 'withexitstatus' is false then this returns just
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    command_output.
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    The run() function can often be used instead of creating a spawn instance.
1660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    For example, the following code uses spawn::
1670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        from pexpect import *
1690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        child = spawn('scp foo user@example.com:.')
1707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        child.expect('(?i)password')
1717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        child.sendline(mypassword)
1727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    The previous code can be replace with the following::
1747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
175eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        from pexpect import *
176eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        run('scp foo user@example.com:.', events={'(?i)password': mypassword})
1777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    Examples
17958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    ========
18058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
18158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    Start the apache daemon on the local machine::
18258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
18358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        from pexpect import *
1847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        run("/usr/local/apache/bin/apachectl start")
1857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
18658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    Check in a file using SVN::
18758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
18858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        from pexpect import *
18958e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        run("svn ci -m 'automatic commit' my_file.py")
19058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
19158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    Run a command and capture exit status::
19258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
19358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        from pexpect import *
1947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        (command_output, exitstatus) = run('ls -l /bin', withexitstatus=1)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Tricky Examples
1977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    ===============
1987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
1997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    The following will run SSH and execute 'ls -l' on the remote machine. The
20058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    password 'secret' will be sent if the '(?i)password' pattern is ever seen::
20158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
2027dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        run("ssh username@machine.example.com 'ls -l'",
2037dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            events={'(?i)password':'secret\\n'})
2047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    This will start mencoder to rip a video from DVD. This will also display
2067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    progress ticks every 5 seconds as it runs. For example::
2077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        from pexpect import *
2097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        def print_ticks(d):
2107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            print d['event_count'],
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            events={TIMEOUT:print_ticks}, timeout=5)
2137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    The 'events' argument should be a dictionary of patterns and responses.
2157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    Whenever one of the patterns is seen in the command out run() will send the
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    associated response string. Note that you should put newlines in your
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    string if Enter is necessary. The responses may also contain callback
2187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    functions. Any callback is function that takes a dictionary as an argument.
2197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    The dictionary contains all the locals from the run() function, so you can
2207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    access the child spawn object or any other variable defined in run()
2217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    (event_count, child, and extra_args are the most useful). A callback may
2227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return True to stop the current run process otherwise run() continues until
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    the next event. A callback may also return a string which will be sent to
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    the child. 'extra_args' is not used by directly run(). It provides a way to
2257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    pass data to a callback function through run() through the locals
2267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    dictionary passed to a callback. """
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if timeout == -1:
229424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        child = spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env)
230424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    else:
231424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile,
232424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                cwd=cwd, env=env)
233424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if events is not None:
234424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        patterns = list(events.keys())
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        responses = list(events.values())
236a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    else:
2371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        # This assumes EOF or TIMEOUT will eventually cause run to terminate.
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        patterns = None
2391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        responses = None
2401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    child_result_list = []
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    event_count = 0
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    while True:
243a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        try:
244ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch            index = child.expect(patterns)
245ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch            if type(child.after) in types.StringTypes:
246ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch                child_result_list.append(child.before + child.after)
247ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch            else:
2487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                # child.after may have been a TIMEOUT or EOF,
2497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                # which we don't want appended to the list.
2507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                child_result_list.append(child.before)
2517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            if type(responses[index]) in types.StringTypes:
2527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                child.send(responses[index])
2537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            elif isinstance(responses[index], types.FunctionType):
2547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                callback_result = responses[index](locals())
255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                sys.stdout.flush()
2567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                if type(callback_result) in types.StringTypes:
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    child.send(callback_result)
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                elif callback_result:
2597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                    break
2607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            else:
261868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                raise TypeError('The callback must be a string or function.')
2627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            event_count = event_count + 1
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        except TIMEOUT as e:
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            child_result_list.append(child.before)
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            break
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        except EOF as e:
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            child_result_list.append(child.before)
268868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            break
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    child_result = ''.join(child_result_list)
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if withexitstatus:
2717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        child.close()
2727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        return (child_result, child.exitstatus)
2737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    else:
2747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        return child_result
2757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
276868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
277868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class spawn(object):
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    """This is the main class interface for Pexpect. Use this class to start
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    and control child applications. """
2810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    def __init__(self, command, args=[], timeout=30, maxread=2000,
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        searchwindowsize=None, logfile=None, cwd=None, env=None):
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        """This is the constructor. The command parameter may be a string that
2867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        includes a command and any arguments to the command. For example::
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            child = pexpect.spawn('/usr/bin/ftp')
2897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            child = pexpect.spawn('/usr/bin/ssh user@example.com')
2907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            child = pexpect.spawn('ls -latr /tmp')
291424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
292424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        You may also construct it with a list of arguments like so::
2937dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            child = pexpect.spawn('/usr/bin/ftp', [])
295424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)            child = pexpect.spawn('/usr/bin/ssh', ['user@example.com'])
296424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)            child = pexpect.spawn('ls', ['-latr', '/tmp'])
2977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
2987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        After this the child application will be created and will be ready to
2997dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        talk to. For normal use, see expect() and send() and sendline().
300868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
301868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        Remember that Pexpect does NOT interpret shell meta characters such as
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        redirect, pipe, or wild cards (>, |, or *). This is a common mistake.
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        If you want to run a command and pipe it through another command then
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        you must also start a shell. For example::
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > logs.txt"')
307a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)            child.expect(pexpect.EOF)
3080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
3090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        The second form of spawn (where you pass a list of arguments) is useful
3100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        in situations where you wish to spawn a command and pass it its own
3110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        argument list. This can make syntax more clear. For example, the
3120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        following is equivalent to the previous example::
3130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
3140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)            shell_cmd = 'ls -l | grep LOG > logs.txt'
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            child.expect(pexpect.EOF)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        The maxread attribute sets the read buffer size. This is maximum number
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        of bytes that Pexpect will try to read from a TTY at one time. Setting
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        the maxread size to 1 will turn off buffering. Setting the maxread
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        value higher may help performance in cases where large amounts of
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        output are read back from the child. This feature is useful in
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        conjunction with searchwindowsize.
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        The searchwindowsize attribute sets the how far back in the incomming
326868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        seach buffer Pexpect will search for pattern matches. Every time
327868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        Pexpect reads some data from the child it will append the data to the
328868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        incomming buffer. The default is to search from the beginning of the
329868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        imcomming buffer each time new data is read from the child. But this is
330868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        very inefficient if you are running a command that generates a large
331868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        amount of data where you want to match The searchwindowsize does not
332868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        effect the size of the incomming data buffer. You will still have
333868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        access to the full buffer after expect() returns.
334868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
335868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        The logfile member turns on or off logging. All input and output will
336868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        be copied to the given file object. Set logfile to None to stop
337868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        logging. This is the default. Set logfile to sys.stdout to echo
338868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        everything to standard output. The logfile is flushed after each write.
339868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        Example log input and output to a file::
341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
342868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            child = pexpect.spawn('some_command')
343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            fout = file('mylog.txt','w')
344868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            child.logfile = fout
345868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
346868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        Example log to stdout::
347868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            child = pexpect.spawn('some_command')
349868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            child.logfile = sys.stdout
3507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        The logfile_read and logfile_send members can be used to separately log
3527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        the input from the child and output sent to the child. Sometimes you
3537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        don't want to see everything you write to the child. You only want to
3547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        log what the child sends back. For example::
3557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            child = pexpect.spawn('some_command')
3577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)            child.logfile_read = sys.stdout
3587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        To separately log output sent to the child use logfile_send::
3607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            self.logfile_send = fout
3627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        The delaybeforesend helps overcome a weird behavior that many users
3647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        were experiencing. The typical problem was that a user would expect() a
3657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        "Password:" prompt and then immediately call sendline() to send the
3667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        password. The user would then see that their password was echoed back
3677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        to them. Passwords don't normally echo. The problem is caused by the
3687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        fact that most applications print out the "Password" prompt and then
3697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        turn off stdin echo, but if you send your password before the
3707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        application turned off echo, then you get your password echoed.
3717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        Normally this wouldn't be a problem when interacting with a human at a
3727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        real keyboard. If you introduce a slight delay just before writing then
3737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        this seems to clear up the problem. This was such a common problem for
3747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        many users that I decided that the default pexpect behavior should be
3757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        to sleep just before writing to the child application. 1/20th of a
3767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        second (50 ms) seems to be enough to clear up the problem. You can set
3777dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        delaybeforesend to 0 to return to the old behavior. Most Linux machines
3787dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        don't like this to be below 0.03. I don't know why.
3797dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3807dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        Note that spawn is clever about finding commands on your path.
3817dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        It uses the same logic that "which" uses to find executables.
3827dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
3837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        If you wish to get the exit status of the child you must call the
3847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        close() method. The exit or signal status of the child will be stored
3857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        in self.exitstatus or self.signalstatus. If the child exited normally
386424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        then exitstatus will store the exit return code and signalstatus will
387424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        be None. If the child was terminated abnormally with a signal then
388424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        signalstatus will store the signal value and exitstatus will be None.
389424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        If you need more detail you can also read the self.status member which
3907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        stores the status returned by os.waitpid. You can interpret this using
3917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG. """
3927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.STDIN_FILENO = pty.STDIN_FILENO
3947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.STDOUT_FILENO = pty.STDOUT_FILENO
3957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.STDERR_FILENO = pty.STDERR_FILENO
3967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.stdin = sys.stdin
3977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.stdout = sys.stdout
3987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.stderr = sys.stderr
3997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
4007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.searcher = None
4017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.ignorecase = False
4027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.before = None
4037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        self.after = None
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        self.match = None
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        self.match_index = None
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        self.terminated = True
407424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        self.exitstatus = None
408424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        self.signalstatus = None
409424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        # status returned by os.waitpid
410424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        self.status = None
411424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        self.flag_eof = False
412424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        self.pid = None
413424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        # the chile filedescriptor is initially closed
414424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        self.child_fd = -1
415424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        self.timeout = timeout
416a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        self.delimiter = EOF
4177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        self.logfile = logfile
4187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        # input from child (read_nonblocking)
4197dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        self.logfile_read = None
4207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        # output to send (send, sendline)
4217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        self.logfile_send = None
4227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        # max bytes to read at one time into buffer
4237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        self.maxread = maxread
4247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        # This is the read buffer. See maxread.
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        self.buffer = ''
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        # Data before searchwindowsize point is preserved, but not searched.
4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        self.searchwindowsize = searchwindowsize
428eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        # Delay used before sending data to child. Time in seconds.
429eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        # Most Linux machines don't like this to be below 0.03 (30 ms).
430eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        self.delaybeforesend = 0.05
431eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        # Used by close() to give kernel time to update process status.
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        # Time in seconds.
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        self.delayafterclose = 0.1
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        # Used by terminate() to give kernel time to update process status.
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        # Time in seconds.
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        self.delayafterterminate = 0.1
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        self.softspace = False
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        self.name = '<' + repr(self) + '>'
4397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        self.encoding = None
4407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        self.closed = True
4417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        self.cwd = cwd
4427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        self.env = env
4437dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        # This flags if we are running on irix
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        self.__irix_hack = (sys.platform.lower().find('irix') >= 0)
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        # Solaris uses internal __fork_pty(). All others use pty.fork().
4467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        if ((sys.platform.lower().find('solaris') >= 0)
4477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            or (sys.platform.lower().find('sunos5') >= 0)):
4487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            self.use_native_pty_fork = False
449eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        else:
450eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch            self.use_native_pty_fork = True
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        # Support subclasses that do not use command or args.
453424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        if command is None:
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            self.command = None
4557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch            self.args = None
456868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            self.name = '<pexpect factory incomplete>'
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        else:
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            self._spawn(command, args)
459868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
460    def __del__(self):
461
462        """This makes sure that no system resources are left open. Python only
463        garbage collects Python objects. OS file descriptors are not Python
464        objects, so they must be handled explicitly. If the child file
465        descriptor was opened outside of this class (passed to the constructor)
466        then this does not close it. """
467
468        if not self.closed:
469            # It is possible for __del__ methods to execute during the
470            # teardown of the Python VM itself. Thus self.close() may
471            # trigger an exception because os.close may be None.
472            # -- Fernando Perez
473            try:
474                self.close()
475            except:
476                pass
477
478    def __str__(self):
479
480        """This returns a human-readable string that represents the state of
481        the object. """
482
483        s = []
484        s.append(repr(self))
485        s.append('version: ' + __version__ + ' (' + __revision__ + ')')
486        s.append('command: ' + str(self.command))
487        s.append('args: ' + str(self.args))
488        s.append('searcher: ' + str(self.searcher))
489        s.append('buffer (last 100 chars): ' + str(self.buffer)[-100:])
490        s.append('before (last 100 chars): ' + str(self.before)[-100:])
491        s.append('after: ' + str(self.after))
492        s.append('match: ' + str(self.match))
493        s.append('match_index: ' + str(self.match_index))
494        s.append('exitstatus: ' + str(self.exitstatus))
495        s.append('flag_eof: ' + str(self.flag_eof))
496        s.append('pid: ' + str(self.pid))
497        s.append('child_fd: ' + str(self.child_fd))
498        s.append('closed: ' + str(self.closed))
499        s.append('timeout: ' + str(self.timeout))
500        s.append('delimiter: ' + str(self.delimiter))
501        s.append('logfile: ' + str(self.logfile))
502        s.append('logfile_read: ' + str(self.logfile_read))
503        s.append('logfile_send: ' + str(self.logfile_send))
504        s.append('maxread: ' + str(self.maxread))
505        s.append('ignorecase: ' + str(self.ignorecase))
506        s.append('searchwindowsize: ' + str(self.searchwindowsize))
507        s.append('delaybeforesend: ' + str(self.delaybeforesend))
508        s.append('delayafterclose: ' + str(self.delayafterclose))
509        s.append('delayafterterminate: ' + str(self.delayafterterminate))
510        return '\n'.join(s)
511
512    def _spawn(self, command, args=[]):
513
514        """This starts the given command in a child process. This does all the
515        fork/exec type of stuff for a pty. This is called by __init__. If args
516        is empty then command will be parsed (split on spaces) and args will be
517        set to parsed arguments. """
518
519        # The pid and child_fd of this object get set by this method.
520        # Note that it is difficult for this method to fail.
521        # You cannot detect if the child process cannot start.
522        # So the only way you can tell if the child process started
523        # or not is to try to read from the file descriptor. If you get
524        # EOF immediately then it means that the child is already dead.
525        # That may not necessarily be bad because you may have spawned a child
526        # that performs some task; creates no stdout output; and then dies.
527
528        # If command is an int type then it may represent a file descriptor.
529        if isinstance(command, type(0)):
530            raise ExceptionPexpect('Command is an int type. ' +
531                    'If this is a file descriptor then maybe you want to ' +
532                    'use fdpexpect.fdspawn which takes an existing ' +
533                    'file descriptor instead of a command string.')
534
535        if not isinstance(args, type([])):
536            raise TypeError('The argument, args, must be a list.')
537
538        if args == []:
539            self.args = split_command_line(command)
540            self.command = self.args[0]
541        else:
542            # Make a shallow copy of the args list.
543            self.args = args[:]
544            self.args.insert(0, command)
545            self.command = command
546
547        command_with_path = which(self.command)
548        if command_with_path is None:
549            raise ExceptionPexpect('The command was not found or was not ' +
550                    'executable: %s.' % self.command)
551        self.command = command_with_path
552        self.args[0] = self.command
553
554        self.name = '<' + ' '.join(self.args) + '>'
555
556        assert self.pid is None, 'The pid member must be None.'
557        assert self.command is not None, 'The command member must not be None.'
558
559        if self.use_native_pty_fork:
560            try:
561                self.pid, self.child_fd = pty.fork()
562            except OSError as e:
563                raise ExceptionPexpect('pty.fork() failed: ' + str(e))
564        else:
565            # Use internal __fork_pty
566            self.pid, self.child_fd = self.__fork_pty()
567
568        if self.pid == 0:
569            # Child
570            try:
571                # used by setwinsize()
572                self.child_fd = sys.stdout.fileno()
573                self.setwinsize(24, 80)
574            except:
575                # Some platforms do not like setwinsize (Cygwin).
576                # This will cause problem when running applications that
577                # are very picky about window size.
578                # This is a serious limitation, but not a show stopper.
579                pass
580            # Do not allow child to inherit open file descriptors from parent.
581            max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
582            for i in range(3, max_fd):
583                try:
584                    os.close(i)
585                except OSError:
586                    pass
587
588            # I don't know why this works, but ignoring SIGHUP fixes a
589            # problem when trying to start a Java daemon with sudo
590            # (specifically, Tomcat).
591            signal.signal(signal.SIGHUP, signal.SIG_IGN)
592
593            if self.cwd is not None:
594                os.chdir(self.cwd)
595            if self.env is None:
596                os.execv(self.command, self.args)
597            else:
598                os.execvpe(self.command, self.args, self.env)
599
600        # Parent
601        self.terminated = False
602        self.closed = False
603
604    def __fork_pty(self):
605
606        """This implements a substitute for the forkpty system call. This
607        should be more portable than the pty.fork() function. Specifically,
608        this should work on Solaris.
609
610        Modified 10.06.05 by Geoff Marshall: Implemented __fork_pty() method to
611        resolve the issue with Python's pty.fork() not supporting Solaris,
612        particularly ssh. Based on patch to posixmodule.c authored by Noah
613        Spurrier::
614
615            http://mail.python.org/pipermail/python-dev/2003-May/035281.html
616
617        """
618
619        parent_fd, child_fd = os.openpty()
620        if parent_fd < 0 or child_fd < 0:
621            raise ExceptionPexpect("Could not open with os.openpty().")
622
623        pid = os.fork()
624        if pid < 0:
625            raise ExceptionPexpect("Failed os.fork().")
626        elif pid == 0:
627            # Child.
628            os.close(parent_fd)
629            self.__pty_make_controlling_tty(child_fd)
630
631            os.dup2(child_fd, 0)
632            os.dup2(child_fd, 1)
633            os.dup2(child_fd, 2)
634
635            if child_fd > 2:
636                os.close(child_fd)
637        else:
638            # Parent.
639            os.close(child_fd)
640
641        return pid, parent_fd
642
643    def __pty_make_controlling_tty(self, tty_fd):
644
645        """This makes the pseudo-terminal the controlling tty. This should be
646        more portable than the pty.fork() function. Specifically, this should
647        work on Solaris. """
648
649        child_name = os.ttyname(tty_fd)
650
651        # Disconnect from controlling tty. Harmless if not already connected.
652        try:
653            fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY)
654            if fd >= 0:
655                os.close(fd)
656        except:
657            # Already disconnected. This happens if running inside cron.
658            pass
659
660        os.setsid()
661
662        # Verify we are disconnected from controlling tty
663        # by attempting to open it again.
664        try:
665            fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY)
666            if fd >= 0:
667                os.close(fd)
668                raise ExceptionPexpect('Failed to disconnect from ' +
669                    'controlling tty. It is still possible to open /dev/tty.')
670        except:
671            # Good! We are disconnected from a controlling tty.
672            pass
673
674        # Verify we can open child pty.
675        fd = os.open(child_name, os.O_RDWR)
676        if fd < 0:
677            raise ExceptionPexpect("Could not open child pty, " + child_name)
678        else:
679            os.close(fd)
680
681        # Verify we now have a controlling tty.
682        fd = os.open("/dev/tty", os.O_WRONLY)
683        if fd < 0:
684            raise ExceptionPexpect("Could not open controlling tty, /dev/tty")
685        else:
686            os.close(fd)
687
688    def fileno(self):
689
690        """This returns the file descriptor of the pty for the child.
691        """
692
693        return self.child_fd
694
695    def close(self, force=True):
696
697        """This closes the connection with the child application. Note that
698        calling close() more than once is valid. This emulates standard Python
699        behavior with files. Set force to True if you want to make sure that
700        the child is terminated (SIGKILL is sent if the child ignores SIGHUP
701        and SIGINT). """
702
703        if not self.closed:
704            self.flush()
705            os.close(self.child_fd)
706            # Give kernel time to update process status.
707            time.sleep(self.delayafterclose)
708            if self.isalive():
709                if not self.terminate(force):
710                    raise ExceptionPexpect('Could not terminate the child.')
711            self.child_fd = -1
712            self.closed = True
713            #self.pid = None
714
715    def flush(self):
716
717        """This does nothing. It is here to support the interface for a
718        File-like object. """
719
720        pass
721
722    def isatty(self):
723
724        """This returns True if the file descriptor is open and connected to a
725        tty(-like) device, else False. """
726
727        return os.isatty(self.child_fd)
728
729    def waitnoecho(self, timeout=-1):
730
731        """This waits until the terminal ECHO flag is set False. This returns
732        True if the echo mode is off. This returns False if the ECHO flag was
733        not set False before the timeout. This can be used to detect when the
734        child is waiting for a password. Usually a child application will turn
735        off echo mode when it is waiting for the user to enter a password. For
736        example, instead of expecting the "password:" prompt you can wait for
737        the child to set ECHO off::
738
739            p = pexpect.spawn('ssh user@example.com')
740            p.waitnoecho()
741            p.sendline(mypassword)
742
743        If timeout==-1 then this method will use the value in self.timeout.
744        If timeout==None then this method to block until ECHO flag is False.
745        """
746
747        if timeout == -1:
748            timeout = self.timeout
749        if timeout is not None:
750            end_time = time.time() + timeout
751        while True:
752            if not self.getecho():
753                return True
754            if timeout < 0 and timeout is not None:
755                return False
756            if timeout is not None:
757                timeout = end_time - time.time()
758            time.sleep(0.1)
759
760    def getecho(self):
761
762        """This returns the terminal echo mode. This returns True if echo is
763        on or False if echo is off. Child applications that are expecting you
764        to enter a password often set ECHO False. See waitnoecho(). """
765
766        attr = termios.tcgetattr(self.child_fd)
767        if attr[3] & termios.ECHO:
768            return True
769        return False
770
771    def setecho(self, state):
772
773        """This sets the terminal echo mode on or off. Note that anything the
774        child sent before the echo will be lost, so you should be sure that
775        your input buffer is empty before you call setecho(). For example, the
776        following will work as expected::
777
778            p = pexpect.spawn('cat') # Echo is on by default.
779            p.sendline('1234') # We expect see this twice from the child...
780            p.expect(['1234']) # ... once from the tty echo...
781            p.expect(['1234']) # ... and again from cat itself.
782            p.setecho(False) # Turn off tty echo
783            p.sendline('abcd') # We will set this only once (echoed by cat).
784            p.sendline('wxyz') # We will set this only once (echoed by cat)
785            p.expect(['abcd'])
786            p.expect(['wxyz'])
787
788        The following WILL NOT WORK because the lines sent before the setecho
789        will be lost::
790
791            p = pexpect.spawn('cat')
792            p.sendline('1234')
793            p.setecho(False) # Turn off tty echo
794            p.sendline('abcd') # We will set this only once (echoed by cat).
795            p.sendline('wxyz') # We will set this only once (echoed by cat)
796            p.expect(['1234'])
797            p.expect(['1234'])
798            p.expect(['abcd'])
799            p.expect(['wxyz'])
800        """
801
802        self.child_fd
803        attr = termios.tcgetattr(self.child_fd)
804        if state:
805            attr[3] = attr[3] | termios.ECHO
806        else:
807            attr[3] = attr[3] & ~termios.ECHO
808        # I tried TCSADRAIN and TCSAFLUSH, but
809        # these were inconsistent and blocked on some platforms.
810        # TCSADRAIN would probably be ideal if it worked.
811        termios.tcsetattr(self.child_fd, termios.TCSANOW, attr)
812
813    def read_nonblocking(self, size=1, timeout=-1):
814
815        """This reads at most size characters from the child application. It
816        includes a timeout. If the read does not complete within the timeout
817        period then a TIMEOUT exception is raised. If the end of file is read
818        then an EOF exception will be raised. If a log file was set using
819        setlog() then all data will also be written to the log file.
820
821        If timeout is None then the read may block indefinitely.
822        If timeout is -1 then the self.timeout value is used. If timeout is 0
823        then the child is polled and if there is no data immediately ready
824        then this will raise a TIMEOUT exception.
825
826        The timeout refers only to the amount of time to read at least one
827        character. This is not effected by the 'size' parameter, so if you call
828        read_nonblocking(size=100, timeout=30) and only one character is
829        available right away then one character will be returned immediately.
830        It will not wait for 30 seconds for another 99 characters to come in.
831
832        This is a wrapper around os.read(). It uses select.select() to
833        implement the timeout. """
834
835        if self.closed:
836            raise ValueError('I/O operation on closed file.')
837
838        if timeout == -1:
839            timeout = self.timeout
840
841        # Note that some systems such as Solaris do not give an EOF when
842        # the child dies. In fact, you can still try to read
843        # from the child_fd -- it will block forever or until TIMEOUT.
844        # For this case, I test isalive() before doing any reading.
845        # If isalive() is false, then I pretend that this is the same as EOF.
846        if not self.isalive():
847            # timeout of 0 means "poll"
848            r, w, e = self.__select([self.child_fd], [], [], 0)
849            if not r:
850                self.flag_eof = True
851                raise EOF('End Of File (EOF). Braindead platform.')
852        elif self.__irix_hack:
853            # Irix takes a long time before it realizes a child was terminated.
854            # FIXME So does this mean Irix systems are forced to always have
855            # FIXME a 2 second delay when calling read_nonblocking? That sucks.
856            r, w, e = self.__select([self.child_fd], [], [], 2)
857            if not r and not self.isalive():
858                self.flag_eof = True
859                raise EOF('End Of File (EOF). Slow platform.')
860
861        r, w, e = self.__select([self.child_fd], [], [], timeout)
862
863        if not r:
864            if not self.isalive():
865                # Some platforms, such as Irix, will claim that their
866                # processes are alive; timeout on the select; and
867                # then finally admit that they are not alive.
868                self.flag_eof = True
869                raise EOF('End of File (EOF). Very slow platform.')
870            else:
871                raise TIMEOUT('Timeout exceeded.')
872
873        if self.child_fd in r:
874            try:
875                s = os.read(self.child_fd, size)
876            except OSError as e:
877                # Linux does this
878                self.flag_eof = True
879                raise EOF('End Of File (EOF). Exception style platform.')
880            if s == '':
881                # BSD style
882                self.flag_eof = True
883                raise EOF('End Of File (EOF). Empty string style platform.')
884            if self.logfile is not None:
885                self.logfile.write(s)
886                self.logfile.flush()
887            if self.logfile_read is not None:
888                self.logfile_read.write(s)
889                self.logfile_read.flush()
890            return s
891
892        raise ExceptionPexpect('Reached an unexpected state.')
893
894    def read(self, size=-1):
895
896        """This reads at most "size" bytes from the file (less if the read hits
897        EOF before obtaining size bytes). If the size argument is negative or
898        omitted, read all data until EOF is reached. The bytes are returned as
899        a string object. An empty string is returned when EOF is encountered
900        immediately. """
901
902        if size == 0:
903            return ''
904        if size < 0:
905            # delimiter default is EOF
906            self.expect(self.delimiter)
907            return self.before
908
909        # I could have done this more directly by not using expect(), but
910        # I deliberately decided to couple read() to expect() so that
911        # I would catch any bugs early and ensure consistant behavior.
912        # It's a little less efficient, but there is less for me to
913        # worry about if I have to later modify read() or expect().
914        # Note, it's OK if size==-1 in the regex. That just means it
915        # will never match anything in which case we stop only on EOF.
916        cre = re.compile('.{%d}' % size, re.DOTALL)
917        # delimiter default is EOF
918        index = self.expect([cre, self.delimiter])
919        if index == 0:
920            ### FIXME self.before should be ''. Should I assert this?
921            return self.after
922        return self.before
923
924    def readline(self, size=-1):
925
926        """This reads and returns one entire line. The newline at the end of
927        line is returned as part of the string, unless the file ends without a
928        newline. An empty string is returned if EOF is encountered immediately.
929        This looks for a newline as a CR/LF pair (\\r\\n) even on UNIX because
930        this is what the pseudotty device returns. So contrary to what you may
931        expect you will receive newlines as \\r\\n.
932
933        If the size argument is 0 then an empty string is returned. In all
934        other cases the size argument is ignored, which is not standard
935        behavior for a file-like object. """
936
937        if size == 0:
938            return ''
939        # delimiter default is EOF
940        index = self.expect(['\r\n', self.delimiter])
941        if index == 0:
942            return self.before + '\r\n'
943        else:
944            return self.before
945
946    def __iter__(self):
947
948        """This is to support iterators over a file-like object.
949        """
950
951        return self
952
953    def __next__(self):
954
955        """This is to support iterators over a file-like object.
956        """
957
958        result = self.readline()
959        if result == "":
960            raise StopIteration
961        return result
962
963    def readlines(self, sizehint=-1):
964
965        """This reads until EOF using readline() and returns a list containing
966        the lines thus read. The optional 'sizehint' argument is ignored. """
967
968        lines = []
969        while True:
970            line = self.readline()
971            if not line:
972                break
973            lines.append(line)
974        return lines
975
976    def write(self, s):
977
978        """This is similar to send() except that there is no return value.
979        """
980
981        self.send(s)
982
983    def writelines(self, sequence):
984
985        """This calls write() for each element in the sequence. The sequence
986        can be any iterable object producing strings, typically a list of
987        strings. This does not add line separators There is no return value.
988        """
989
990        for s in sequence:
991            self.write(s)
992
993    def send(self, s):
994
995        """This sends a string to the child process. This returns the number of
996        bytes written. If a log file was set then the data is also written to
997        the log. """
998
999        time.sleep(self.delaybeforesend)
1000        if self.logfile is not None:
1001            self.logfile.write(s)
1002            self.logfile.flush()
1003        if self.logfile_send is not None:
1004            self.logfile_send.write(s)
1005            self.logfile_send.flush()
1006        c = os.write(self.child_fd, s.encode("utf-8"))
1007        return c
1008
1009    def sendline(self, s=''):
1010
1011        """This is like send(), but it adds a linefeed (os.linesep). This
1012        returns the number of bytes written. """
1013
1014        n = self.send(s)
1015        n = n + self.send(os.linesep)
1016        return n
1017
1018    def sendcontrol(self, char):
1019
1020        """This sends a control character to the child such as Ctrl-C or
1021        Ctrl-D. For example, to send a Ctrl-G (ASCII 7)::
1022
1023            child.sendcontrol('g')
1024
1025        See also, sendintr() and sendeof().
1026        """
1027
1028        char = char.lower()
1029        a = ord(char)
1030        if a >= 97 and a <= 122:
1031            a = a - ord('a') + 1
1032            return self.send(chr(a))
1033        d = {'@': 0, '`': 0,
1034            '[': 27, '{': 27,
1035            '\\': 28, '|': 28,
1036            ']': 29, '}': 29,
1037            '^': 30, '~': 30,
1038            '_': 31,
1039            '?': 127}
1040        if char not in d:
1041            return 0
1042        return self.send(chr(d[char]))
1043
1044    def sendeof(self):
1045
1046        """This sends an EOF to the child. This sends a character which causes
1047        the pending parent output buffer to be sent to the waiting child
1048        program without waiting for end-of-line. If it is the first character
1049        of the line, the read() in the user program returns 0, which signifies
1050        end-of-file. This means to work as expected a sendeof() has to be
1051        called at the beginning of a line. This method does not send a newline.
1052        It is the responsibility of the caller to ensure the eof is sent at the
1053        beginning of a line. """
1054
1055        ### Hmmm... how do I send an EOF?
1056        ###C  if ((m = write(pty, *buf, p - *buf)) < 0)
1057        ###C      return (errno == EWOULDBLOCK) ? n : -1;
1058        #fd = sys.stdin.fileno()
1059        #old = termios.tcgetattr(fd) # remember current state
1060        #attr = termios.tcgetattr(fd)
1061        #attr[3] = attr[3] | termios.ICANON # ICANON must be set to see EOF
1062        #try: # use try/finally to ensure state gets restored
1063        #    termios.tcsetattr(fd, termios.TCSADRAIN, attr)
1064        #    if hasattr(termios, 'CEOF'):
1065        #        os.write(self.child_fd, '%c' % termios.CEOF)
1066        #    else:
1067        #        # Silly platform does not define CEOF so assume CTRL-D
1068        #        os.write(self.child_fd, '%c' % 4)
1069        #finally: # restore state
1070        #    termios.tcsetattr(fd, termios.TCSADRAIN, old)
1071        if hasattr(termios, 'VEOF'):
1072            char = termios.tcgetattr(self.child_fd)[6][termios.VEOF]
1073        else:
1074            # platform does not define VEOF so assume CTRL-D
1075            char = chr(4)
1076        self.send(char)
1077
1078    def sendintr(self):
1079
1080        """This sends a SIGINT to the child. It does not require
1081        the SIGINT to be the first character on a line. """
1082
1083        if hasattr(termios, 'VINTR'):
1084            char = termios.tcgetattr(self.child_fd)[6][termios.VINTR]
1085        else:
1086            # platform does not define VINTR so assume CTRL-C
1087            char = chr(3)
1088        self.send(char)
1089
1090    def eof(self):
1091
1092        """This returns True if the EOF exception was ever raised.
1093        """
1094
1095        return self.flag_eof
1096
1097    def terminate(self, force=False):
1098
1099        """This forces a child process to terminate. It starts nicely with
1100        SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This
1101        returns True if the child was terminated. This returns False if the
1102        child could not be terminated. """
1103
1104        if not self.isalive():
1105            return True
1106        try:
1107            self.kill(signal.SIGHUP)
1108            time.sleep(self.delayafterterminate)
1109            if not self.isalive():
1110                return True
1111            self.kill(signal.SIGCONT)
1112            time.sleep(self.delayafterterminate)
1113            if not self.isalive():
1114                return True
1115            self.kill(signal.SIGINT)
1116            time.sleep(self.delayafterterminate)
1117            if not self.isalive():
1118                return True
1119            if force:
1120                self.kill(signal.SIGKILL)
1121                time.sleep(self.delayafterterminate)
1122                if not self.isalive():
1123                    return True
1124                else:
1125                    return False
1126            return False
1127        except OSError as e:
1128            # I think there are kernel timing issues that sometimes cause
1129            # this to happen. I think isalive() reports True, but the
1130            # process is dead to the kernel.
1131            # Make one last attempt to see if the kernel is up to date.
1132            time.sleep(self.delayafterterminate)
1133            if not self.isalive():
1134                return True
1135            else:
1136                return False
1137
1138    def wait(self):
1139
1140        """This waits until the child exits. This is a blocking call. This will
1141        not read any data from the child, so this will block forever if the
1142        child has unread output and has terminated. In other words, the child
1143        may have printed output then called exit(), but, the child is
1144        technically still alive until its output is read by the parent. """
1145
1146        if self.isalive():
1147            pid, status = os.waitpid(self.pid, 0)
1148        else:
1149            raise ExceptionPexpect('Cannot wait for dead child process.')
1150        self.exitstatus = os.WEXITSTATUS(status)
1151        if os.WIFEXITED(status):
1152            self.status = status
1153            self.exitstatus = os.WEXITSTATUS(status)
1154            self.signalstatus = None
1155            self.terminated = True
1156        elif os.WIFSIGNALED(status):
1157            self.status = status
1158            self.exitstatus = None
1159            self.signalstatus = os.WTERMSIG(status)
1160            self.terminated = True
1161        elif os.WIFSTOPPED(status):
1162            # You can't call wait() on a child process in the stopped state.
1163            raise ExceptionPexpect('Called wait() on a stopped child ' +
1164                    'process. This is not supported. Is some other ' +
1165                    'process attempting job control with our child pid?')
1166        return self.exitstatus
1167
1168    def isalive(self):
1169
1170        """This tests if the child process is running or not. This is
1171        non-blocking. If the child was terminated then this will read the
1172        exitstatus or signalstatus of the child. This returns True if the child
1173        process appears to be running or False if not. It can take literally
1174        SECONDS for Solaris to return the right status. """
1175
1176        if self.terminated:
1177            return False
1178
1179        if self.flag_eof:
1180            # This is for Linux, which requires the blocking form
1181            # of waitpid to # get status of a defunct process.
1182            # This is super-lame. The flag_eof would have been set
1183            # in read_nonblocking(), so this should be safe.
1184            waitpid_options = 0
1185        else:
1186            waitpid_options = os.WNOHANG
1187
1188        try:
1189            pid, status = os.waitpid(self.pid, waitpid_options)
1190        except OSError as e:
1191            # No child processes
1192            if e[0] == errno.ECHILD:
1193                raise ExceptionPexpect('isalive() encountered condition ' +
1194                        'where "terminated" is 0, but there was no child ' +
1195                        'process. Did someone else call waitpid() ' +
1196                        'on our process?')
1197            else:
1198                raise e
1199
1200        # I have to do this twice for Solaris.
1201        # I can't even believe that I figured this out...
1202        # If waitpid() returns 0 it means that no child process
1203        # wishes to report, and the value of status is undefined.
1204        if pid == 0:
1205            try:
1206                ### os.WNOHANG) # Solaris!
1207                pid, status = os.waitpid(self.pid, waitpid_options)
1208            except OSError as e:
1209                # This should never happen...
1210                if e[0] == errno.ECHILD:
1211                    raise ExceptionPexpect('isalive() encountered condition ' +
1212                            'that should never happen. There was no child ' +
1213                            'process. Did someone else call waitpid() ' +
1214                            'on our process?')
1215                else:
1216                    raise e
1217
1218            # If pid is still 0 after two calls to waitpid() then the process
1219            # really is alive. This seems to work on all platforms, except for
1220            # Irix which seems to require a blocking call on waitpid or select,
1221            # so I let read_nonblocking take care of this situation
1222            # (unfortunately, this requires waiting through the timeout).
1223            if pid == 0:
1224                return True
1225
1226        if pid == 0:
1227            return True
1228
1229        if os.WIFEXITED(status):
1230            self.status = status
1231            self.exitstatus = os.WEXITSTATUS(status)
1232            self.signalstatus = None
1233            self.terminated = True
1234        elif os.WIFSIGNALED(status):
1235            self.status = status
1236            self.exitstatus = None
1237            self.signalstatus = os.WTERMSIG(status)
1238            self.terminated = True
1239        elif os.WIFSTOPPED(status):
1240            raise ExceptionPexpect('isalive() encountered condition ' +
1241                    'where child process is stopped. This is not ' +
1242                    'supported. Is some other process attempting ' +
1243                    'job control with our child pid?')
1244        return False
1245
1246    def kill(self, sig):
1247
1248        """This sends the given signal to the child application. In keeping
1249        with UNIX tradition it has a misleading name. It does not necessarily
1250        kill the child unless you send the right signal. """
1251
1252        # Same as os.kill, but the pid is given for you.
1253        if self.isalive():
1254            os.kill(self.pid, sig)
1255
1256    def compile_pattern_list(self, patterns):
1257
1258        """This compiles a pattern-string or a list of pattern-strings.
1259        Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or a list of
1260        those. Patterns may also be None which results in an empty list (you
1261        might do this if waiting for an EOF or TIMEOUT condition without
1262        expecting any pattern).
1263
1264        This is used by expect() when calling expect_list(). Thus expect() is
1265        nothing more than::
1266
1267             cpl = self.compile_pattern_list(pl)
1268             return self.expect_list(cpl, timeout)
1269
1270        If you are using expect() within a loop it may be more
1271        efficient to compile the patterns first and then call expect_list().
1272        This avoid calls in a loop to compile_pattern_list()::
1273
1274             cpl = self.compile_pattern_list(my_pattern)
1275             while some_condition:
1276                ...
1277                i = self.expect_list(clp, timeout)
1278                ...
1279        """
1280
1281        if patterns is None:
1282            return []
1283        if not isinstance(patterns, list):
1284            patterns = [patterns]
1285
1286        # Allow dot to match \n
1287        compile_flags = re.DOTALL
1288        if self.ignorecase:
1289            compile_flags = compile_flags | re.IGNORECASE
1290        compiled_pattern_list = []
1291        for p in patterns:
1292            if type(p) in types.StringTypes:
1293                compiled_pattern_list.append(re.compile(p, compile_flags))
1294            elif p is EOF:
1295                compiled_pattern_list.append(EOF)
1296            elif p is TIMEOUT:
1297                compiled_pattern_list.append(TIMEOUT)
1298            elif isinstance(p, type(re.compile(''))):
1299                compiled_pattern_list.append(p)
1300            else:
1301                raise TypeError('Argument must be one of StringTypes, ' +
1302                        'EOF, TIMEOUT, SRE_Pattern, or a list of those ' +
1303                        'type. %s' % str(type(p)))
1304
1305        return compiled_pattern_list
1306
1307    def expect(self, pattern, timeout=-1, searchwindowsize=-1):
1308
1309        """This seeks through the stream until a pattern is matched. The
1310        pattern is overloaded and may take several types. The pattern can be a
1311        StringType, EOF, a compiled re, or a list of any of those types.
1312        Strings will be compiled to re types. This returns the index into the
1313        pattern list. If the pattern was not a list this returns index 0 on a
1314        successful match. This may raise exceptions for EOF or TIMEOUT. To
1315        avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to the pattern
1316        list. That will cause expect to match an EOF or TIMEOUT condition
1317        instead of raising an exception.
1318
1319        If you pass a list of patterns and more than one matches, the first
1320        match in the stream is chosen. If more than one pattern matches at that
1321        point, the leftmost in the pattern list is chosen. For example::
1322
1323            # the input is 'foobar'
1324            index = p.expect(['bar', 'foo', 'foobar'])
1325            # returns 1('foo') even though 'foobar' is a "better" match
1326
1327        Please note, however, that buffering can affect this behavior, since
1328        input arrives in unpredictable chunks. For example::
1329
1330            # the input is 'foobar'
1331            index = p.expect(['foobar', 'foo'])
1332            # returns 0('foobar') if all input is available at once,
1333            # but returs 1('foo') if parts of the final 'bar' arrive late
1334
1335        After a match is found the instance attributes 'before', 'after' and
1336        'match' will be set. You can see all the data read before the match in
1337        'before'. You can see the data that was matched in 'after'. The
1338        re.MatchObject used in the re match will be in 'match'. If an error
1339        occurred then 'before' will be set to all the data read so far and
1340        'after' and 'match' will be None.
1341
1342        If timeout is -1 then timeout will be set to the self.timeout value.
1343
1344        A list entry may be EOF or TIMEOUT instead of a string. This will
1345        catch these exceptions and return the index of the list entry instead
1346        of raising the exception. The attribute 'after' will be set to the
1347        exception type. The attribute 'match' will be None. This allows you to
1348        write code like this::
1349
1350                index = p.expect(['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])
1351                if index == 0:
1352                    do_something()
1353                elif index == 1:
1354                    do_something_else()
1355                elif index == 2:
1356                    do_some_other_thing()
1357                elif index == 3:
1358                    do_something_completely_different()
1359
1360        instead of code like this::
1361
1362                try:
1363                    index = p.expect(['good', 'bad'])
1364                    if index == 0:
1365                        do_something()
1366                    elif index == 1:
1367                        do_something_else()
1368                except EOF:
1369                    do_some_other_thing()
1370                except TIMEOUT:
1371                    do_something_completely_different()
1372
1373        These two forms are equivalent. It all depends on what you want. You
1374        can also just expect the EOF if you are waiting for all output of a
1375        child to finish. For example::
1376
1377                p = pexpect.spawn('/bin/ls')
1378                p.expect(pexpect.EOF)
1379                print p.before
1380
1381        If you are trying to optimize for speed then see expect_list().
1382        """
1383
1384        compiled_pattern_list = self.compile_pattern_list(pattern)
1385        return self.expect_list(compiled_pattern_list,
1386                timeout, searchwindowsize)
1387
1388    def expect_list(self, pattern_list, timeout=-1, searchwindowsize=-1):
1389
1390        """This takes a list of compiled regular expressions and returns the
1391        index into the pattern_list that matched the child output. The list may
1392        also contain EOF or TIMEOUT(which are not compiled regular
1393        expressions). This method is similar to the expect() method except that
1394        expect_list() does not recompile the pattern list on every call. This
1395        may help if you are trying to optimize for speed, otherwise just use
1396        the expect() method.  This is called by expect(). If timeout==-1 then
1397        the self.timeout value is used. If searchwindowsize==-1 then the
1398        self.searchwindowsize value is used. """
1399
1400        return self.expect_loop(searcher_re(pattern_list),
1401                timeout, searchwindowsize)
1402
1403    def expect_exact(self, pattern_list, timeout=-1, searchwindowsize=-1):
1404
1405        """This is similar to expect(), but uses plain string matching instead
1406        of compiled regular expressions in 'pattern_list'. The 'pattern_list'
1407        may be a string; a list or other sequence of strings; or TIMEOUT and
1408        EOF.
1409
1410        This call might be faster than expect() for two reasons: string
1411        searching is faster than RE matching and it is possible to limit the
1412        search to just the end of the input buffer.
1413
1414        This method is also useful when you don't want to have to worry about
1415        escaping regular expression characters that you want to match."""
1416
1417        if (type(pattern_list) in types.StringTypes or
1418            pattern_list in (TIMEOUT, EOF)):
1419            pattern_list = [pattern_list]
1420        return self.expect_loop(searcher_string(pattern_list),
1421                timeout, searchwindowsize)
1422
1423    def expect_loop(self, searcher, timeout=-1, searchwindowsize=-1):
1424
1425        """This is the common loop used inside expect. The 'searcher' should be
1426        an instance of searcher_re or searcher_string, which describes how and
1427        what to search for in the input.
1428
1429        See expect() for other arguments, return value and exceptions. """
1430
1431        self.searcher = searcher
1432
1433        if timeout == -1:
1434            timeout = self.timeout
1435        if timeout is not None:
1436            end_time = time.time() + timeout
1437        if searchwindowsize == -1:
1438            searchwindowsize = self.searchwindowsize
1439
1440        try:
1441            incoming = self.buffer
1442            freshlen = len(incoming)
1443            while True:
1444                # Keep reading until exception or return.
1445                index = searcher.search(incoming, freshlen, searchwindowsize)
1446                if index >= 0:
1447                    self.buffer = incoming[searcher.end:]
1448                    self.before = incoming[: searcher.start]
1449                    self.after = incoming[searcher.start: searcher.end]
1450                    self.match = searcher.match
1451                    self.match_index = index
1452                    return self.match_index
1453                # No match at this point
1454                if timeout < 0 and timeout is not None:
1455                    raise TIMEOUT('Timeout exceeded in expect_any().')
1456                # Still have time left, so read more data
1457                c = self.read_nonblocking(self.maxread, timeout)
1458                freshlen = len(c)
1459                time.sleep(0.0001)
1460                incoming = incoming + c
1461                if timeout is not None:
1462                    timeout = end_time - time.time()
1463        except EOF as e:
1464            self.buffer = ''
1465            self.before = incoming
1466            self.after = EOF
1467            index = searcher.eof_index
1468            if index >= 0:
1469                self.match = EOF
1470                self.match_index = index
1471                return self.match_index
1472            else:
1473                self.match = None
1474                self.match_index = None
1475                raise EOF(str(e) + '\n' + str(self))
1476        except TIMEOUT as e:
1477            self.buffer = incoming
1478            self.before = incoming
1479            self.after = TIMEOUT
1480            index = searcher.timeout_index
1481            if index >= 0:
1482                self.match = TIMEOUT
1483                self.match_index = index
1484                return self.match_index
1485            else:
1486                self.match = None
1487                self.match_index = None
1488                raise TIMEOUT(str(e) + '\n' + str(self))
1489        except:
1490            self.before = incoming
1491            self.after = None
1492            self.match = None
1493            self.match_index = None
1494            raise
1495
1496    def getwinsize(self):
1497
1498        """This returns the terminal window size of the child tty. The return
1499        value is a tuple of (rows, cols). """
1500
1501        TIOCGWINSZ = getattr(termios, 'TIOCGWINSZ', 1074295912)
1502        s = struct.pack('HHHH', 0, 0, 0, 0)
1503        x = fcntl.ioctl(self.fileno(), TIOCGWINSZ, s)
1504        return struct.unpack('HHHH', x)[0:2]
1505
1506    def setwinsize(self, rows, cols):
1507
1508        """This sets the terminal window size of the child tty. This will cause
1509        a SIGWINCH signal to be sent to the child. This does not change the
1510        physical window size. It changes the size reported to TTY-aware
1511        applications like vi or curses -- applications that respond to the
1512        SIGWINCH signal. """
1513
1514        # Check for buggy platforms. Some Python versions on some platforms
1515        # (notably OSF1 Alpha and RedHat 7.1) truncate the value for
1516        # termios.TIOCSWINSZ. It is not clear why this happens.
1517        # These platforms don't seem to handle the signed int very well;
1518        # yet other platforms like OpenBSD have a large negative value for
1519        # TIOCSWINSZ and they don't have a truncate problem.
1520        # Newer versions of Linux have totally different values for TIOCSWINSZ.
1521        # Note that this fix is a hack.
1522        TIOCSWINSZ = getattr(termios, 'TIOCSWINSZ', -2146929561)
1523        if TIOCSWINSZ == 2148037735:
1524            # Same bits, but with sign.
1525            TIOCSWINSZ = -2146929561
1526        # Note, assume ws_xpixel and ws_ypixel are zero.
1527        s = struct.pack('HHHH', rows, cols, 0, 0)
1528        fcntl.ioctl(self.fileno(), TIOCSWINSZ, s)
1529
1530    def interact(self, escape_character=chr(29),
1531            input_filter=None, output_filter=None):
1532
1533        """This gives control of the child process to the interactive user (the
1534        human at the keyboard). Keystrokes are sent to the child process, and
1535        the stdout and stderr output of the child process is printed. This
1536        simply echos the child stdout and child stderr to the real stdout and
1537        it echos the real stdin to the child stdin. When the user types the
1538        escape_character this method will stop. The default for
1539        escape_character is ^]. This should not be confused with ASCII 27 --
1540        the ESC character. ASCII 29 was chosen for historical merit because
1541        this is the character used by 'telnet' as the escape character. The
1542        escape_character will not be sent to the child process.
1543
1544        You may pass in optional input and output filter functions. These
1545        functions should take a string and return a string. The output_filter
1546        will be passed all the output from the child process. The input_filter
1547        will be passed all the keyboard input from the user. The input_filter
1548        is run BEFORE the check for the escape_character.
1549
1550        Note that if you change the window size of the parent the SIGWINCH
1551        signal will not be passed through to the child. If you want the child
1552        window size to change when the parent's window size changes then do
1553        something like the following example::
1554
1555            import pexpect, struct, fcntl, termios, signal, sys
1556            def sigwinch_passthrough (sig, data):
1557                s = struct.pack("HHHH", 0, 0, 0, 0)
1558                a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(),
1559                    termios.TIOCGWINSZ , s))
1560                global p
1561                p.setwinsize(a[0],a[1])
1562            # Note this 'p' global and used in sigwinch_passthrough.
1563            p = pexpect.spawn('/bin/bash')
1564            signal.signal(signal.SIGWINCH, sigwinch_passthrough)
1565            p.interact()
1566        """
1567
1568        # Flush the buffer.
1569        self.stdout.write(self.buffer)
1570        self.stdout.flush()
1571        self.buffer = ''
1572        mode = tty.tcgetattr(self.STDIN_FILENO)
1573        tty.setraw(self.STDIN_FILENO)
1574        try:
1575            self.__interact_copy(escape_character, input_filter, output_filter)
1576        finally:
1577            tty.tcsetattr(self.STDIN_FILENO, tty.TCSAFLUSH, mode)
1578
1579    def __interact_writen(self, fd, data):
1580
1581        """This is used by the interact() method.
1582        """
1583
1584        while data != '' and self.isalive():
1585            n = os.write(fd, data)
1586            data = data[n:]
1587
1588    def __interact_read(self, fd):
1589
1590        """This is used by the interact() method.
1591        """
1592
1593        return os.read(fd, 1000)
1594
1595    def __interact_copy(self, escape_character=None,
1596            input_filter=None, output_filter=None):
1597
1598        """This is used by the interact() method.
1599        """
1600
1601        while self.isalive():
1602            r, w, e = self.__select([self.child_fd, self.STDIN_FILENO], [], [])
1603            if self.child_fd in r:
1604                data = self.__interact_read(self.child_fd)
1605                if output_filter:
1606                    data = output_filter(data)
1607                if self.logfile is not None:
1608                    self.logfile.write(data)
1609                    self.logfile.flush()
1610                os.write(self.STDOUT_FILENO, data)
1611            if self.STDIN_FILENO in r:
1612                data = self.__interact_read(self.STDIN_FILENO)
1613                if input_filter:
1614                    data = input_filter(data)
1615                i = data.rfind(escape_character)
1616                if i != -1:
1617                    data = data[:i]
1618                    self.__interact_writen(self.child_fd, data)
1619                    break
1620                self.__interact_writen(self.child_fd, data)
1621
1622    def __select(self, iwtd, owtd, ewtd, timeout=None):
1623
1624        """This is a wrapper around select.select() that ignores signals. If
1625        select.select raises a select.error exception and errno is an EINTR
1626        error then it is ignored. Mainly this is used to ignore sigwinch
1627        (terminal resize). """
1628
1629        # if select() is interrupted by a signal (errno==EINTR) then
1630        # we loop back and enter the select() again.
1631        if timeout is not None:
1632            end_time = time.time() + timeout
1633        while True:
1634            try:
1635                return select.select(iwtd, owtd, ewtd, timeout)
1636            except select.error as e:
1637                if e[0] == errno.EINTR:
1638                    # if we loop back we have to subtract the
1639                    # amount of time we already waited.
1640                    if timeout is not None:
1641                        timeout = end_time - time.time()
1642                        if timeout < 0:
1643                            return([], [], [])
1644                else:
1645                    # something else caused the select.error, so
1646                    # this actually is an exception.
1647                    raise
1648
1649##############################################################################
1650# The following methods are no longer supported or allowed.
1651
1652    def setmaxread(self, maxread):
1653
1654        """This method is no longer supported or allowed. I don't like getters
1655        and setters without a good reason. """
1656
1657        raise ExceptionPexpect('This method is no longer supported ' +
1658                'or allowed. Just assign a value to the ' +
1659                'maxread member variable.')
1660
1661    def setlog(self, fileobject):
1662
1663        """This method is no longer supported or allowed.
1664        """
1665
1666        raise ExceptionPexpect('This method is no longer supported ' +
1667                'or allowed. Just assign a value to the logfile ' +
1668                'member variable.')
1669
1670##############################################################################
1671# End of spawn class
1672##############################################################################
1673
1674
1675class searcher_string(object):
1676
1677    """This is a plain string search helper for the spawn.expect_any() method.
1678    This helper class is for speed. For more powerful regex patterns
1679    see the helper class, searcher_re.
1680
1681    Attributes:
1682
1683        eof_index     - index of EOF, or -1
1684        timeout_index - index of TIMEOUT, or -1
1685
1686    After a successful match by the search() method the following attributes
1687    are available:
1688
1689        start - index into the buffer, first byte of match
1690        end   - index into the buffer, first byte after match
1691        match - the matching string itself
1692
1693    """
1694
1695    def __init__(self, strings):
1696
1697        """This creates an instance of searcher_string. This argument 'strings'
1698        may be a list; a sequence of strings; or the EOF or TIMEOUT types. """
1699
1700        self.eof_index = -1
1701        self.timeout_index = -1
1702        self._strings = []
1703        for n, s in zip(list(range(len(strings))), strings):
1704            if s is EOF:
1705                self.eof_index = n
1706                continue
1707            if s is TIMEOUT:
1708                self.timeout_index = n
1709                continue
1710            self._strings.append((n, s))
1711
1712    def __str__(self):
1713
1714        """This returns a human-readable string that represents the state of
1715        the object."""
1716
1717        ss = [(ns[0], '    %d: "%s"' % ns) for ns in self._strings]
1718        ss.append((-1, 'searcher_string:'))
1719        if self.eof_index >= 0:
1720            ss.append((self.eof_index, '    %d: EOF' % self.eof_index))
1721        if self.timeout_index >= 0:
1722            ss.append((self.timeout_index,
1723                '    %d: TIMEOUT' % self.timeout_index))
1724        ss.sort()
1725        ss = zip(*ss)[1]
1726        return '\n'.join(ss)
1727
1728    def search(self, buffer, freshlen, searchwindowsize=None):
1729
1730        """This searches 'buffer' for the first occurence of one of the search
1731        strings.  'freshlen' must indicate the number of bytes at the end of
1732        'buffer' which have not been searched before. It helps to avoid
1733        searching the same, possibly big, buffer over and over again.
1734
1735        See class spawn for the 'searchwindowsize' argument.
1736
1737        If there is a match this returns the index of that string, and sets
1738        'start', 'end' and 'match'. Otherwise, this returns -1. """
1739
1740        absurd_match = len(buffer)
1741        first_match = absurd_match
1742
1743        # 'freshlen' helps a lot here. Further optimizations could
1744        # possibly include:
1745        #
1746        # using something like the Boyer-Moore Fast String Searching
1747        # Algorithm; pre-compiling the search through a list of
1748        # strings into something that can scan the input once to
1749        # search for all N strings; realize that if we search for
1750        # ['bar', 'baz'] and the input is '...foo' we need not bother
1751        # rescanning until we've read three more bytes.
1752        #
1753        # Sadly, I don't know enough about this interesting topic. /grahn
1754
1755        for index, s in self._strings:
1756            if searchwindowsize is None:
1757                # the match, if any, can only be in the fresh data,
1758                # or at the very end of the old data
1759                offset = -(freshlen + len(s))
1760            else:
1761                # better obey searchwindowsize
1762                offset = -searchwindowsize
1763            n = buffer.find(s, offset)
1764            if n >= 0 and n < first_match:
1765                first_match = n
1766                best_index, best_match = index, s
1767        if first_match == absurd_match:
1768            return -1
1769        self.match = best_match
1770        self.start = first_match
1771        self.end = self.start + len(self.match)
1772        return best_index
1773
1774
1775class searcher_re(object):
1776
1777    """This is regular expression string search helper for the
1778    spawn.expect_any() method. This helper class is for powerful
1779    pattern matching. For speed, see the helper class, searcher_string.
1780
1781    Attributes:
1782
1783        eof_index     - index of EOF, or -1
1784        timeout_index - index of TIMEOUT, or -1
1785
1786    After a successful match by the search() method the following attributes
1787    are available:
1788
1789        start - index into the buffer, first byte of match
1790        end   - index into the buffer, first byte after match
1791        match - the re.match object returned by a succesful re.search
1792
1793    """
1794
1795    def __init__(self, patterns):
1796
1797        """This creates an instance that searches for 'patterns' Where
1798        'patterns' may be a list or other sequence of compiled regular
1799        expressions, or the EOF or TIMEOUT types."""
1800
1801        self.eof_index = -1
1802        self.timeout_index = -1
1803        self._searches = []
1804        for n, s in zip(list(range(len(patterns))), patterns):
1805            if s is EOF:
1806                self.eof_index = n
1807                continue
1808            if s is TIMEOUT:
1809                self.timeout_index = n
1810                continue
1811            self._searches.append((n, s))
1812
1813    def __str__(self):
1814
1815        """This returns a human-readable string that represents the state of
1816        the object."""
1817
1818        ss = [(n, '    %d: re.compile("%s")' %
1819            (n, str(s.pattern))) for n, s in self._searches]
1820        ss.append((-1, 'searcher_re:'))
1821        if self.eof_index >= 0:
1822            ss.append((self.eof_index, '    %d: EOF' % self.eof_index))
1823        if self.timeout_index >= 0:
1824            ss.append((self.timeout_index, '    %d: TIMEOUT' %
1825                self.timeout_index))
1826        ss.sort()
1827        ss = zip(*ss)[1]
1828        return '\n'.join(ss)
1829
1830    def search(self, buffer, freshlen, searchwindowsize=None):
1831
1832        """This searches 'buffer' for the first occurence of one of the regular
1833        expressions. 'freshlen' must indicate the number of bytes at the end of
1834        'buffer' which have not been searched before.
1835
1836        See class spawn for the 'searchwindowsize' argument.
1837
1838        If there is a match this returns the index of that string, and sets
1839        'start', 'end' and 'match'. Otherwise, returns -1."""
1840
1841        absurd_match = len(buffer)
1842        first_match = absurd_match
1843        # 'freshlen' doesn't help here -- we cannot predict the
1844        # length of a match, and the re module provides no help.
1845        if searchwindowsize is None:
1846            searchstart = 0
1847        else:
1848            searchstart = max(0, len(buffer) - searchwindowsize)
1849        for index, s in self._searches:
1850            match = s.search(buffer, searchstart)
1851            if match is None:
1852                continue
1853            n = match.start()
1854            if n < first_match:
1855                first_match = n
1856                the_match = match
1857                best_index = index
1858        if first_match == absurd_match:
1859            return -1
1860        self.start = first_match
1861        self.match = the_match
1862        self.end = self.match.end()
1863        return best_index
1864
1865
1866def which(filename):
1867
1868    """This takes a given filename; tries to find it in the environment path;
1869    then checks if it is executable. This returns the full path to the filename
1870    if found and executable. Otherwise this returns None."""
1871
1872    # Special case where filename contains an explicit path.
1873    if os.path.dirname(filename) != '':
1874        if os.access(filename, os.X_OK):
1875            return filename
1876    if 'PATH' not in os.environ or os.environ['PATH'] == '':
1877        p = os.defpath
1878    else:
1879        p = os.environ['PATH']
1880    pathlist = string.split(p, os.pathsep)
1881    for path in pathlist:
1882        ff = os.path.join(path, filename)
1883        if os.access(ff, os.X_OK):
1884            return ff
1885    return None
1886
1887
1888def split_command_line(command_line):
1889
1890    """This splits a command line into a list of arguments. It splits arguments
1891    on spaces, but handles embedded quotes, doublequotes, and escaped
1892    characters. It's impossible to do this with a regular expression, so I
1893    wrote a little state machine to parse the command line. """
1894
1895    arg_list = []
1896    arg = ''
1897
1898    # Constants to name the states we can be in.
1899    state_basic = 0
1900    state_esc = 1
1901    state_singlequote = 2
1902    state_doublequote = 3
1903    # The state when consuming whitespace between commands.
1904    state_whitespace = 4
1905    state = state_basic
1906
1907    for c in command_line:
1908        if state == state_basic or state == state_whitespace:
1909            if c == '\\':
1910                # Escape the next character
1911                state = state_esc
1912            elif c == r"'":
1913                # Handle single quote
1914                state = state_singlequote
1915            elif c == r'"':
1916                # Handle double quote
1917                state = state_doublequote
1918            elif c.isspace():
1919                # Add arg to arg_list if we aren't in the middle of whitespace.
1920                if state == state_whitespace:
1921                    # Do nothing.
1922                    None
1923                else:
1924                    arg_list.append(arg)
1925                    arg = ''
1926                    state = state_whitespace
1927            else:
1928                arg = arg + c
1929                state = state_basic
1930        elif state == state_esc:
1931            arg = arg + c
1932            state = state_basic
1933        elif state == state_singlequote:
1934            if c == r"'":
1935                state = state_basic
1936            else:
1937                arg = arg + c
1938        elif state == state_doublequote:
1939            if c == r'"':
1940                state = state_basic
1941            else:
1942                arg = arg + c
1943
1944    if arg != '':
1945        arg_list.append(arg)
1946    return arg_list
1947
1948# vi:set sr et ts=4 sw=4 ft=python :
1949