1"""Pexpect is a Python module for spawning child applications and controlling
2them automatically. Pexpect can be used for automating interactive applications
3such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup
4scripts for duplicating software package installations on different servers. It
5can be used for automated software testing. Pexpect is in the spirit of Don
6Libes' Expect, but Pexpect is pure Python. Other Expect-like modules for Python
7require TCL and Expect or require C extensions to be compiled. Pexpect does not
8use C, Expect, or TCL extensions. It should work on any platform that supports
9the standard Python pty module. The Pexpect interface focuses on ease of use so
10that simple tasks are easy.
11
12There are two main interfaces to Pexpect -- the function, run() and the class,
13spawn. You can call the run() function to execute a command and return the
14output. This is a handy replacement for os.system().
15
16For example::
17
18    pexpect.run('ls -la')
19
20The more powerful interface is the spawn class. You can use this to spawn an
21external child command and then interact with the child by sending lines and
22expecting responses.
23
24For example::
25
26    child = pexpect.spawn('scp foo myname@host.example.com:.')
27    child.expect ('Password:')
28    child.sendline (mypassword)
29
30This works even for commands that ask for passwords or other input outside of
31the normal stdio streams.
32
33Credits: Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett,
34Robert Stone, Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids
35vander Molen, George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin,
36Jacques-Etienne Baudoux, Geoffrey Marshall, Francisco Lourenco, Glen Mabey,
37Karthik Gurusamy, Fernando Perez, Corey Minyard, Jon Cohen, Guillaume
38Chazarain, Andrew Ryan, Nick Craig-Wood, Andrew Stone, Jorgen Grahn, John
39Spiegel, Jan Grant (Let me know if I forgot anyone.)
40
41Free, open source, and all that good stuff.
42
43Permission is hereby granted, free of charge, to any person obtaining a copy of
44this software and associated documentation files (the "Software"), to deal in
45the Software without restriction, including without limitation the rights to
46use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
47of the Software, and to permit persons to whom the Software is furnished to do
48so, subject to the following conditions:
49
50The above copyright notice and this permission notice shall be included in all
51copies or substantial portions of the Software.
52
53THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
56AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
57LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
59SOFTWARE.
60
61Pexpect Copyright (c) 2008 Noah Spurrier
62http://pexpect.sourceforge.net/
63
64$Id: pexpect.py 516 2008-05-23 20:46:01Z noah $
65"""
66
67try:
68    import os, sys, time
69    import select
70    import string
71    import re
72    import struct
73    import resource
74    import types
75    import pty
76    import tty
77    import termios
78    import fcntl
79    import errno
80    import traceback
81    import signal
82except ImportError, e:
83    raise ImportError (str(e) + """
84
85A critical module was not found. Probably this operating system does not
86support it. Pexpect is intended for UNIX-like operating systems.""")
87
88__version__ = '2.4'
89__revision__ = '$Revision: 516 $'
90__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'run', 'which',
91    'split_command_line', '__version__', '__revision__']
92
93# Exception classes used by this module.
94class ExceptionPexpect(Exception):
95
96    """Base class for all exceptions raised by this module.
97    """
98
99    def __init__(self, value):
100
101        self.value = value
102
103    def __str__(self):
104
105        return str(self.value)
106
107    def get_trace(self):
108
109        """This returns an abbreviated stack trace with lines that only concern
110        the caller. In other words, the stack trace inside the Pexpect module
111        is not included. """
112
113        tblist = traceback.extract_tb(sys.exc_info()[2])
114        #tblist = filter(self.__filter_not_pexpect, tblist)
115        tblist = [item for item in tblist if self.__filter_not_pexpect(item)]
116        tblist = traceback.format_list(tblist)
117        return ''.join(tblist)
118
119    def __filter_not_pexpect(self, trace_list_item):
120
121        """This returns True if list item 0 the string 'pexpect.py' in it. """
122
123        if trace_list_item[0].find('pexpect.py') == -1:
124            return True
125        else:
126            return False
127
128class EOF(ExceptionPexpect):
129
130    """Raised when EOF is read from a child. This usually means the child has exited."""
131
132class TIMEOUT(ExceptionPexpect):
133
134    """Raised when a read time exceeds the timeout. """
135
136##class TIMEOUT_PATTERN(TIMEOUT):
137##    """Raised when the pattern match time exceeds the timeout.
138##    This is different than a read TIMEOUT because the child process may
139##    give output, thus never give a TIMEOUT, but the output
140##    may never match a pattern.
141##    """
142##class MAXBUFFER(ExceptionPexpect):
143##    """Raised when a scan buffer fills before matching an expected pattern."""
144
145def run (command, timeout=-1, withexitstatus=False, events=None, extra_args=None, logfile=None, cwd=None, env=None):
146
147    """
148    This function runs the given command; waits for it to finish; then
149    returns all output as a string. STDERR is included in output. If the full
150    path to the command is not given then the path is searched.
151
152    Note that lines are terminated by CR/LF (\\r\\n) combination even on
153    UNIX-like systems because this is the standard for pseudo ttys. If you set
154    'withexitstatus' to true, then run will return a tuple of (command_output,
155    exitstatus). If 'withexitstatus' is false then this returns just
156    command_output.
157
158    The run() function can often be used instead of creating a spawn instance.
159    For example, the following code uses spawn::
160
161        from pexpect import *
162        child = spawn('scp foo myname@host.example.com:.')
163        child.expect ('(?i)password')
164        child.sendline (mypassword)
165
166    The previous code can be replace with the following::
167
168        from pexpect import *
169        run ('scp foo myname@host.example.com:.', events={'(?i)password': mypassword})
170
171    Examples
172    ========
173
174    Start the apache daemon on the local machine::
175
176        from pexpect import *
177        run ("/usr/local/apache/bin/apachectl start")
178
179    Check in a file using SVN::
180
181        from pexpect import *
182        run ("svn ci -m 'automatic commit' my_file.py")
183
184    Run a command and capture exit status::
185
186        from pexpect import *
187        (command_output, exitstatus) = run ('ls -l /bin', withexitstatus=1)
188
189    Tricky Examples
190    ===============
191
192    The following will run SSH and execute 'ls -l' on the remote machine. The
193    password 'secret' will be sent if the '(?i)password' pattern is ever seen::
194
195        run ("ssh username@machine.example.com 'ls -l'", events={'(?i)password':'secret\\n'})
196
197    This will start mencoder to rip a video from DVD. This will also display
198    progress ticks every 5 seconds as it runs. For example::
199
200        from pexpect import *
201        def print_ticks(d):
202            print d['event_count'],
203        run ("mencoder dvd://1 -o video.avi -oac copy -ovc copy", events={TIMEOUT:print_ticks}, timeout=5)
204
205    The 'events' argument should be a dictionary of patterns and responses.
206    Whenever one of the patterns is seen in the command out run() will send the
207    associated response string. Note that you should put newlines in your
208    string if Enter is necessary. The responses may also contain callback
209    functions. Any callback is function that takes a dictionary as an argument.
210    The dictionary contains all the locals from the run() function, so you can
211    access the child spawn object or any other variable defined in run()
212    (event_count, child, and extra_args are the most useful). A callback may
213    return True to stop the current run process otherwise run() continues until
214    the next event. A callback may also return a string which will be sent to
215    the child. 'extra_args' is not used by directly run(). It provides a way to
216    pass data to a callback function through run() through the locals
217    dictionary passed to a callback. """
218
219    if timeout == -1:
220        child = spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env)
221    else:
222        child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile, cwd=cwd, env=env)
223    if events is not None:
224        patterns = events.keys()
225        responses = events.values()
226    else:
227        patterns=None # We assume that EOF or TIMEOUT will save us.
228        responses=None
229    child_result_list = []
230    event_count = 0
231    while 1:
232        try:
233            index = child.expect (patterns)
234            if type(child.after) in types.StringTypes:
235                child_result_list.append(child.before + child.after)
236            else: # child.after may have been a TIMEOUT or EOF, so don't cat those.
237                child_result_list.append(child.before)
238            if type(responses[index]) in types.StringTypes:
239                child.send(responses[index])
240            elif type(responses[index]) is types.FunctionType:
241                callback_result = responses[index](locals())
242                sys.stdout.flush()
243                if type(callback_result) in types.StringTypes:
244                    child.send(callback_result)
245                elif callback_result:
246                    break
247            else:
248                raise TypeError ('The callback must be a string or function type.')
249            event_count = event_count + 1
250        except TIMEOUT, e:
251            child_result_list.append(child.before)
252            break
253        except EOF, e:
254            child_result_list.append(child.before)
255            break
256    child_result = ''.join(child_result_list)
257    if withexitstatus:
258        child.close()
259        return (child_result, child.exitstatus)
260    else:
261        return child_result
262
263class spawn (object):
264
265    """This is the main class interface for Pexpect. Use this class to start
266    and control child applications. """
267
268    def __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None, cwd=None, env=None):
269
270        """This is the constructor. The command parameter may be a string that
271        includes a command and any arguments to the command. For example::
272
273            child = pexpect.spawn ('/usr/bin/ftp')
274            child = pexpect.spawn ('/usr/bin/ssh user@example.com')
275            child = pexpect.spawn ('ls -latr /tmp')
276
277        You may also construct it with a list of arguments like so::
278
279            child = pexpect.spawn ('/usr/bin/ftp', [])
280            child = pexpect.spawn ('/usr/bin/ssh', ['user@example.com'])
281            child = pexpect.spawn ('ls', ['-latr', '/tmp'])
282
283        After this the child application will be created and will be ready to
284        talk to. For normal use, see expect() and send() and sendline().
285
286        Remember that Pexpect does NOT interpret shell meta characters such as
287        redirect, pipe, or wild cards (>, |, or *). This is a common mistake.
288        If you want to run a command and pipe it through another command then
289        you must also start a shell. For example::
290
291            child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > log_list.txt"')
292            child.expect(pexpect.EOF)
293
294        The second form of spawn (where you pass a list of arguments) is useful
295        in situations where you wish to spawn a command and pass it its own
296        argument list. This can make syntax more clear. For example, the
297        following is equivalent to the previous example::
298
299            shell_cmd = 'ls -l | grep LOG > log_list.txt'
300            child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])
301            child.expect(pexpect.EOF)
302
303        The maxread attribute sets the read buffer size. This is maximum number
304        of bytes that Pexpect will try to read from a TTY at one time. Setting
305        the maxread size to 1 will turn off buffering. Setting the maxread
306        value higher may help performance in cases where large amounts of
307        output are read back from the child. This feature is useful in
308        conjunction with searchwindowsize.
309
310        The searchwindowsize attribute sets the how far back in the incomming
311        seach buffer Pexpect will search for pattern matches. Every time
312        Pexpect reads some data from the child it will append the data to the
313        incomming buffer. The default is to search from the beginning of the
314        imcomming buffer each time new data is read from the child. But this is
315        very inefficient if you are running a command that generates a large
316        amount of data where you want to match The searchwindowsize does not
317        effect the size of the incomming data buffer. You will still have
318        access to the full buffer after expect() returns.
319
320        The logfile member turns on or off logging. All input and output will
321        be copied to the given file object. Set logfile to None to stop
322        logging. This is the default. Set logfile to sys.stdout to echo
323        everything to standard output. The logfile is flushed after each write.
324
325        Example log input and output to a file::
326
327            child = pexpect.spawn('some_command')
328            fout = file('mylog.txt','w')
329            child.logfile = fout
330
331        Example log to stdout::
332
333            child = pexpect.spawn('some_command')
334            child.logfile = sys.stdout
335
336        The logfile_read and logfile_send members can be used to separately log
337        the input from the child and output sent to the child. Sometimes you
338        don't want to see everything you write to the child. You only want to
339        log what the child sends back. For example::
340
341            child = pexpect.spawn('some_command')
342            child.logfile_read = sys.stdout
343
344        To separately log output sent to the child use logfile_send::
345
346            self.logfile_send = fout
347
348        The delaybeforesend helps overcome a weird behavior that many users
349        were experiencing. The typical problem was that a user would expect() a
350        "Password:" prompt and then immediately call sendline() to send the
351        password. The user would then see that their password was echoed back
352        to them. Passwords don't normally echo. The problem is caused by the
353        fact that most applications print out the "Password" prompt and then
354        turn off stdin echo, but if you send your password before the
355        application turned off echo, then you get your password echoed.
356        Normally this wouldn't be a problem when interacting with a human at a
357        real keyboard. If you introduce a slight delay just before writing then
358        this seems to clear up the problem. This was such a common problem for
359        many users that I decided that the default pexpect behavior should be
360        to sleep just before writing to the child application. 1/20th of a
361        second (50 ms) seems to be enough to clear up the problem. You can set
362        delaybeforesend to 0 to return to the old behavior. Most Linux machines
363        don't like this to be below 0.03. I don't know why.
364
365        Note that spawn is clever about finding commands on your path.
366        It uses the same logic that "which" uses to find executables.
367
368        If you wish to get the exit status of the child you must call the
369        close() method. The exit or signal status of the child will be stored
370        in self.exitstatus or self.signalstatus. If the child exited normally
371        then exitstatus will store the exit return code and signalstatus will
372        be None. If the child was terminated abnormally with a signal then
373        signalstatus will store the signal value and exitstatus will be None.
374        If you need more detail you can also read the self.status member which
375        stores the status returned by os.waitpid. You can interpret this using
376        os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG. """
377
378        self.STDIN_FILENO = pty.STDIN_FILENO
379        self.STDOUT_FILENO = pty.STDOUT_FILENO
380        self.STDERR_FILENO = pty.STDERR_FILENO
381        self.stdin = sys.stdin
382        self.stdout = sys.stdout
383        self.stderr = sys.stderr
384
385        self.searcher = None
386        self.ignorecase = False
387        self.before = None
388        self.after = None
389        self.match = None
390        self.match_index = None
391        self.terminated = True
392        self.exitstatus = None
393        self.signalstatus = None
394        self.status = None # status returned by os.waitpid
395        self.flag_eof = False
396        self.pid = None
397        self.child_fd = -1 # initially closed
398        self.timeout = timeout
399        self.delimiter = EOF
400        self.logfile = logfile
401        self.logfile_read = None # input from child (read_nonblocking)
402        self.logfile_send = None # output to send (send, sendline)
403        self.maxread = maxread # max bytes to read at one time into buffer
404        self.buffer = '' # This is the read buffer. See maxread.
405        self.searchwindowsize = searchwindowsize # Anything before searchwindowsize point is preserved, but not searched.
406        # Most Linux machines don't like delaybeforesend to be below 0.03 (30 ms).
407        self.delaybeforesend = 0.05 # Sets sleep time used just before sending data to child. Time in seconds.
408        self.delayafterclose = 0.1 # Sets delay in close() method to allow kernel time to update process status. Time in seconds.
409        self.delayafterterminate = 0.1 # Sets delay in terminate() method to allow kernel time to update process status. Time in seconds.
410        self.softspace = False # File-like object.
411        self.name = '<' + repr(self) + '>' # File-like object.
412        self.encoding = None # File-like object.
413        self.closed = True # File-like object.
414        self.cwd = cwd
415        self.env = env
416        self.__irix_hack = (sys.platform.lower().find('irix')>=0) # This flags if we are running on irix
417        # Solaris uses internal __fork_pty(). All others use pty.fork().
418        if (sys.platform.lower().find('solaris')>=0) or (sys.platform.lower().find('sunos5')>=0):
419            self.use_native_pty_fork = False
420        else:
421            self.use_native_pty_fork = True
422
423
424        # allow dummy instances for subclasses that may not use command or args.
425        if command is None:
426            self.command = None
427            self.args = None
428            self.name = '<pexpect factory incomplete>'
429        else:
430            self._spawn (command, args)
431
432    def __del__(self):
433
434        """This makes sure that no system resources are left open. Python only
435        garbage collects Python objects. OS file descriptors are not Python
436        objects, so they must be handled explicitly. If the child file
437        descriptor was opened outside of this class (passed to the constructor)
438        then this does not close it. """
439
440        if not self.closed:
441            # It is possible for __del__ methods to execute during the
442            # teardown of the Python VM itself. Thus self.close() may
443            # trigger an exception because os.close may be None.
444            # -- Fernando Perez
445            try:
446                self.close()
447            except:
448                pass
449
450    def __str__(self):
451
452        """This returns a human-readable string that represents the state of
453        the object. """
454
455        s = []
456        s.append(repr(self))
457        s.append('version: ' + __version__ + ' (' + __revision__ + ')')
458        s.append('command: ' + str(self.command))
459        s.append('args: ' + str(self.args))
460        s.append('searcher: ' + str(self.searcher))
461        s.append('buffer (last 100 chars): ' + str(self.buffer)[-100:])
462        s.append('before (last 100 chars): ' + str(self.before)[-100:])
463        s.append('after: ' + str(self.after))
464        s.append('match: ' + str(self.match))
465        s.append('match_index: ' + str(self.match_index))
466        s.append('exitstatus: ' + str(self.exitstatus))
467        s.append('flag_eof: ' + str(self.flag_eof))
468        s.append('pid: ' + str(self.pid))
469        s.append('child_fd: ' + str(self.child_fd))
470        s.append('closed: ' + str(self.closed))
471        s.append('timeout: ' + str(self.timeout))
472        s.append('delimiter: ' + str(self.delimiter))
473        s.append('logfile: ' + str(self.logfile))
474        s.append('logfile_read: ' + str(self.logfile_read))
475        s.append('logfile_send: ' + str(self.logfile_send))
476        s.append('maxread: ' + str(self.maxread))
477        s.append('ignorecase: ' + str(self.ignorecase))
478        s.append('searchwindowsize: ' + str(self.searchwindowsize))
479        s.append('delaybeforesend: ' + str(self.delaybeforesend))
480        s.append('delayafterclose: ' + str(self.delayafterclose))
481        s.append('delayafterterminate: ' + str(self.delayafterterminate))
482        return '\n'.join(s)
483
484    def _spawn(self,command,args=[]):
485
486        """This starts the given command in a child process. This does all the
487        fork/exec type of stuff for a pty. This is called by __init__. If args
488        is empty then command will be parsed (split on spaces) and args will be
489        set to parsed arguments. """
490
491        # The pid and child_fd of this object get set by this method.
492        # Note that it is difficult for this method to fail.
493        # You cannot detect if the child process cannot start.
494        # So the only way you can tell if the child process started
495        # or not is to try to read from the file descriptor. If you get
496        # EOF immediately then it means that the child is already dead.
497        # That may not necessarily be bad because you may haved spawned a child
498        # that performs some task; creates no stdout output; and then dies.
499
500        # If command is an int type then it may represent a file descriptor.
501        if type(command) == type(0):
502            raise ExceptionPexpect ('Command is an int type. If this is a file descriptor then maybe you want to use fdpexpect.fdspawn which takes an existing file descriptor instead of a command string.')
503
504        if type (args) != type([]):
505            raise TypeError ('The argument, args, must be a list.')
506
507        if args == []:
508            self.args = split_command_line(command)
509            self.command = self.args[0]
510        else:
511            self.args = args[:] # work with a copy
512            self.args.insert (0, command)
513            self.command = command
514
515        command_with_path = which(self.command)
516        if command_with_path is None:
517            raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
518        self.command = command_with_path
519        self.args[0] = self.command
520
521        self.name = '<' + ' '.join (self.args) + '>'
522
523        assert self.pid is None, 'The pid member should be None.'
524        assert self.command is not None, 'The command member should not be None.'
525
526        if self.use_native_pty_fork:
527            try:
528                self.pid, self.child_fd = pty.fork()
529            except OSError, e:
530                raise ExceptionPexpect('Error! pty.fork() failed: ' + str(e))
531        else: # Use internal __fork_pty
532            self.pid, self.child_fd = self.__fork_pty()
533
534        if self.pid == 0: # Child
535            try:
536                self.child_fd = sys.stdout.fileno() # used by setwinsize()
537                self.setwinsize(24, 80)
538            except:
539                # Some platforms do not like setwinsize (Cygwin).
540                # This will cause problem when running applications that
541                # are very picky about window size.
542                # This is a serious limitation, but not a show stopper.
543                pass
544            # Do not allow child to inherit open file descriptors from parent.
545            max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
546            for i in range (3, max_fd):
547                try:
548                    os.close (i)
549                except OSError:
550                    pass
551
552            # I don't know why this works, but ignoring SIGHUP fixes a
553            # problem when trying to start a Java daemon with sudo
554            # (specifically, Tomcat).
555            signal.signal(signal.SIGHUP, signal.SIG_IGN)
556
557            if self.cwd is not None:
558                os.chdir(self.cwd)
559            if self.env is None:
560                os.execv(self.command, self.args)
561            else:
562                os.execvpe(self.command, self.args, self.env)
563
564        # Parent
565        self.terminated = False
566        self.closed = False
567
568    def __fork_pty(self):
569
570        """This implements a substitute for the forkpty system call. This
571        should be more portable than the pty.fork() function. Specifically,
572        this should work on Solaris.
573
574        Modified 10.06.05 by Geoff Marshall: Implemented __fork_pty() method to
575        resolve the issue with Python's pty.fork() not supporting Solaris,
576        particularly ssh. Based on patch to posixmodule.c authored by Noah
577        Spurrier::
578
579            http://mail.python.org/pipermail/python-dev/2003-May/035281.html
580
581        """
582
583        parent_fd, child_fd = os.openpty()
584        if parent_fd < 0 or child_fd < 0:
585            raise ExceptionPexpect, "Error! Could not open pty with os.openpty()."
586
587        pid = os.fork()
588        if pid < 0:
589            raise ExceptionPexpect, "Error! Failed os.fork()."
590        elif pid == 0:
591            # Child.
592            os.close(parent_fd)
593            self.__pty_make_controlling_tty(child_fd)
594
595            os.dup2(child_fd, 0)
596            os.dup2(child_fd, 1)
597            os.dup2(child_fd, 2)
598
599            if child_fd > 2:
600                os.close(child_fd)
601        else:
602            # Parent.
603            os.close(child_fd)
604
605        return pid, parent_fd
606
607    def __pty_make_controlling_tty(self, tty_fd):
608
609        """This makes the pseudo-terminal the controlling tty. This should be
610        more portable than the pty.fork() function. Specifically, this should
611        work on Solaris. """
612
613        child_name = os.ttyname(tty_fd)
614
615        # Disconnect from controlling tty if still connected.
616        try:
617            fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY);
618            if fd >= 0:
619                os.close(fd)
620        except:
621            # We are already disconnected. Perhaps we are running inside cron.
622            pass
623
624        os.setsid()
625
626        # Verify we are disconnected from controlling tty
627        try:
628            fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY);
629            if fd >= 0:
630                os.close(fd)
631                raise ExceptionPexpect, "Error! We are not disconnected from a controlling tty."
632        except:
633            # Good! We are disconnected from a controlling tty.
634            pass
635
636        # Verify we can open child pty.
637        fd = os.open(child_name, os.O_RDWR);
638        if fd < 0:
639            raise ExceptionPexpect, "Error! Could not open child pty, " + child_name
640        else:
641            os.close(fd)
642
643        # Verify we now have a controlling tty.
644        fd = os.open("/dev/tty", os.O_WRONLY)
645        if fd < 0:
646            raise ExceptionPexpect, "Error! Could not open controlling tty, /dev/tty"
647        else:
648            os.close(fd)
649
650    def fileno (self):   # File-like object.
651
652        """This returns the file descriptor of the pty for the child.
653        """
654
655        return self.child_fd
656
657    def close (self, force=True):   # File-like object.
658
659        """This closes the connection with the child application. Note that
660        calling close() more than once is valid. This emulates standard Python
661        behavior with files. Set force to True if you want to make sure that
662        the child is terminated (SIGKILL is sent if the child ignores SIGHUP
663        and SIGINT). """
664
665        if not self.closed:
666            self.flush()
667            os.close (self.child_fd)
668            time.sleep(self.delayafterclose) # Give kernel time to update process status.
669            if self.isalive():
670                if not self.terminate(force):
671                    raise ExceptionPexpect ('close() could not terminate the child using terminate()')
672            self.child_fd = -1
673            self.closed = True
674            #self.pid = None
675
676    def flush (self):   # File-like object.
677
678        """This does nothing. It is here to support the interface for a
679        File-like object. """
680
681        pass
682
683    def isatty (self):   # File-like object.
684
685        """This returns True if the file descriptor is open and connected to a
686        tty(-like) device, else False. """
687
688        return os.isatty(self.child_fd)
689
690    def waitnoecho (self, timeout=-1):
691
692        """This waits until the terminal ECHO flag is set False. This returns
693        True if the echo mode is off. This returns False if the ECHO flag was
694        not set False before the timeout. This can be used to detect when the
695        child is waiting for a password. Usually a child application will turn
696        off echo mode when it is waiting for the user to enter a password. For
697        example, instead of expecting the "password:" prompt you can wait for
698        the child to set ECHO off::
699
700            p = pexpect.spawn ('ssh user@example.com')
701            p.waitnoecho()
702            p.sendline(mypassword)
703
704        If timeout is None then this method to block forever until ECHO flag is
705        False.
706
707        """
708
709        if timeout == -1:
710            timeout = self.timeout
711        if timeout is not None:
712            end_time = time.time() + timeout
713        while True:
714            if not self.getecho():
715                return True
716            if timeout < 0 and timeout is not None:
717                return False
718            if timeout is not None:
719                timeout = end_time - time.time()
720            time.sleep(0.1)
721
722    def getecho (self):
723
724        """This returns the terminal echo mode. This returns True if echo is
725        on or False if echo is off. Child applications that are expecting you
726        to enter a password often set ECHO False. See waitnoecho(). """
727
728        attr = termios.tcgetattr(self.child_fd)
729        if attr[3] & termios.ECHO:
730            return True
731        return False
732
733    def setecho (self, state):
734
735        """This sets the terminal echo mode on or off. Note that anything the
736        child sent before the echo will be lost, so you should be sure that
737        your input buffer is empty before you call setecho(). For example, the
738        following will work as expected::
739
740            p = pexpect.spawn('cat')
741            p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).
742            p.expect (['1234'])
743            p.expect (['1234'])
744            p.setecho(False) # Turn off tty echo
745            p.sendline ('abcd') # We will set this only once (echoed by cat).
746            p.sendline ('wxyz') # We will set this only once (echoed by cat)
747            p.expect (['abcd'])
748            p.expect (['wxyz'])
749
750        The following WILL NOT WORK because the lines sent before the setecho
751        will be lost::
752
753            p = pexpect.spawn('cat')
754            p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).
755            p.setecho(False) # Turn off tty echo
756            p.sendline ('abcd') # We will set this only once (echoed by cat).
757            p.sendline ('wxyz') # We will set this only once (echoed by cat)
758            p.expect (['1234'])
759            p.expect (['1234'])
760            p.expect (['abcd'])
761            p.expect (['wxyz'])
762        """
763
764        self.child_fd
765        attr = termios.tcgetattr(self.child_fd)
766        if state:
767            attr[3] = attr[3] | termios.ECHO
768        else:
769            attr[3] = attr[3] & ~termios.ECHO
770        # I tried TCSADRAIN and TCSAFLUSH, but these were inconsistent
771        # and blocked on some platforms. TCSADRAIN is probably ideal if it worked.
772        termios.tcsetattr(self.child_fd, termios.TCSANOW, attr)
773
774    def read_nonblocking (self, size = 1, timeout = -1):
775
776        """This reads at most size characters from the child application. It
777        includes a timeout. If the read does not complete within the timeout
778        period then a TIMEOUT exception is raised. If the end of file is read
779        then an EOF exception will be raised. If a log file was set using
780        setlog() then all data will also be written to the log file.
781
782        If timeout is None then the read may block indefinitely. If timeout is -1
783        then the self.timeout value is used. If timeout is 0 then the child is
784        polled and if there was no data immediately ready then this will raise
785        a TIMEOUT exception.
786
787        The timeout refers only to the amount of time to read at least one
788        character. This is not effected by the 'size' parameter, so if you call
789        read_nonblocking(size=100, timeout=30) and only one character is
790        available right away then one character will be returned immediately.
791        It will not wait for 30 seconds for another 99 characters to come in.
792
793        This is a wrapper around os.read(). It uses select.select() to
794        implement the timeout. """
795
796        if self.closed:
797            raise ValueError ('I/O operation on closed file in read_nonblocking().')
798
799        if timeout == -1:
800            timeout = self.timeout
801
802        # Note that some systems such as Solaris do not give an EOF when
803        # the child dies. In fact, you can still try to read
804        # from the child_fd -- it will block forever or until TIMEOUT.
805        # For this case, I test isalive() before doing any reading.
806        # If isalive() is false, then I pretend that this is the same as EOF.
807        if not self.isalive():
808            r,w,e = self.__select([self.child_fd], [], [], 0) # timeout of 0 means "poll"
809            if not r:
810                self.flag_eof = True
811                raise EOF ('End Of File (EOF) in read_nonblocking(). Braindead platform.')
812        elif self.__irix_hack:
813            # This is a hack for Irix. It seems that Irix requires a long delay before checking isalive.
814            # This adds a 2 second delay, but only when the child is terminated.
815            r, w, e = self.__select([self.child_fd], [], [], 2)
816            if not r and not self.isalive():
817                self.flag_eof = True
818                raise EOF ('End Of File (EOF) in read_nonblocking(). Pokey platform.')
819
820        r,w,e = self.__select([self.child_fd], [], [], timeout)
821
822        if not r:
823            if not self.isalive():
824                # Some platforms, such as Irix, will claim that their processes are alive;
825                # then timeout on the select; and then finally admit that they are not alive.
826                self.flag_eof = True
827                raise EOF ('End of File (EOF) in read_nonblocking(). Very pokey platform.')
828            else:
829                raise TIMEOUT ('Timeout exceeded in read_nonblocking().')
830
831        if self.child_fd in r:
832            try:
833                s = os.read(self.child_fd, size)
834            except OSError, e: # Linux does this
835                self.flag_eof = True
836                raise EOF ('End Of File (EOF) in read_nonblocking(). Exception style platform.')
837            if s == '': # BSD style
838                self.flag_eof = True
839                raise EOF ('End Of File (EOF) in read_nonblocking(). Empty string style platform.')
840
841            if self.logfile is not None:
842                self.logfile.write (s)
843                self.logfile.flush()
844            if self.logfile_read is not None:
845                self.logfile_read.write (s)
846                self.logfile_read.flush()
847
848            return s
849
850        raise ExceptionPexpect ('Reached an unexpected state in read_nonblocking().')
851
852    def read (self, size = -1):   # File-like object.
853
854        """This reads at most "size" bytes from the file (less if the read hits
855        EOF before obtaining size bytes). If the size argument is negative or
856        omitted, read all data until EOF is reached. The bytes are returned as
857        a string object. An empty string is returned when EOF is encountered
858        immediately. """
859
860        if size == 0:
861            return ''
862        if size < 0:
863            self.expect (self.delimiter) # delimiter default is EOF
864            return self.before
865
866        # I could have done this more directly by not using expect(), but
867        # I deliberately decided to couple read() to expect() so that
868        # I would catch any bugs early and ensure consistant behavior.
869        # It's a little less efficient, but there is less for me to
870        # worry about if I have to later modify read() or expect().
871        # Note, it's OK if size==-1 in the regex. That just means it
872        # will never match anything in which case we stop only on EOF.
873        cre = re.compile('.{%d}' % size, re.DOTALL)
874        index = self.expect ([cre, self.delimiter]) # delimiter default is EOF
875        if index == 0:
876            return self.after ### self.before should be ''. Should I assert this?
877        return self.before
878
879    def readline (self, size = -1):    # File-like object.
880
881        """This reads and returns one entire line. A trailing newline is kept
882        in the string, but may be absent when a file ends with an incomplete
883        line. Note: This readline() looks for a \\r\\n pair even on UNIX
884        because this is what the pseudo tty device returns. So contrary to what
885        you may expect you will receive the newline as \\r\\n. An empty string
886        is returned when EOF is hit immediately. Currently, the size argument is
887        mostly ignored, so this behavior is not standard for a file-like
888        object. If size is 0 then an empty string is returned. """
889
890        if size == 0:
891            return ''
892        index = self.expect (['\r\n', self.delimiter]) # delimiter default is EOF
893        if index == 0:
894            return self.before + '\r\n'
895        else:
896            return self.before
897
898    def __iter__ (self):    # File-like object.
899
900        """This is to support iterators over a file-like object.
901        """
902
903        return self
904
905    def next (self):    # File-like object.
906
907        """This is to support iterators over a file-like object.
908        """
909
910        result = self.readline()
911        if result == "":
912            raise StopIteration
913        return result
914
915    def readlines (self, sizehint = -1):    # File-like object.
916
917        """This reads until EOF using readline() and returns a list containing
918        the lines thus read. The optional "sizehint" argument is ignored. """
919
920        lines = []
921        while True:
922            line = self.readline()
923            if not line:
924                break
925            lines.append(line)
926        return lines
927
928    def write(self, s):   # File-like object.
929
930        """This is similar to send() except that there is no return value.
931        """
932
933        self.send (s)
934
935    def writelines (self, sequence):   # File-like object.
936
937        """This calls write() for each element in the sequence. The sequence
938        can be any iterable object producing strings, typically a list of
939        strings. This does not add line separators There is no return value.
940        """
941
942        for s in sequence:
943            self.write (s)
944
945    def send(self, s):
946
947        """This sends a string to the child process. This returns the number of
948        bytes written. If a log file was set then the data is also written to
949        the log. """
950
951        time.sleep(self.delaybeforesend)
952        if self.logfile is not None:
953            self.logfile.write (s)
954            self.logfile.flush()
955        if self.logfile_send is not None:
956            self.logfile_send.write (s)
957            self.logfile_send.flush()
958        c = os.write(self.child_fd, s)
959        return c
960
961    def sendline(self, s=''):
962
963        """This is like send(), but it adds a line feed (os.linesep). This
964        returns the number of bytes written. """
965
966        n = self.send(s)
967        n = n + self.send (os.linesep)
968        return n
969
970    def sendcontrol(self, char):
971
972        """This sends a control character to the child such as Ctrl-C or
973        Ctrl-D. For example, to send a Ctrl-G (ASCII 7)::
974
975            child.sendcontrol('g')
976
977        See also, sendintr() and sendeof().
978        """
979
980        char = char.lower()
981        a = ord(char)
982        if a>=97 and a<=122:
983            a = a - ord('a') + 1
984            return self.send (chr(a))
985        d = {'@':0, '`':0,
986            '[':27, '{':27,
987            '\\':28, '|':28,
988            ']':29, '}': 29,
989            '^':30, '~':30,
990            '_':31,
991            '?':127}
992        if char not in d:
993            return 0
994        return self.send (chr(d[char]))
995
996    def sendeof(self):
997
998        """This sends an EOF to the child. This sends a character which causes
999        the pending parent output buffer to be sent to the waiting child
1000        program without waiting for end-of-line. If it is the first character
1001        of the line, the read() in the user program returns 0, which signifies
1002        end-of-file. This means to work as expected a sendeof() has to be
1003        called at the beginning of a line. This method does not send a newline.
1004        It is the responsibility of the caller to ensure the eof is sent at the
1005        beginning of a line. """
1006
1007        ### Hmmm... how do I send an EOF?
1008        ###C  if ((m = write(pty, *buf, p - *buf)) < 0)
1009        ###C      return (errno == EWOULDBLOCK) ? n : -1;
1010        #fd = sys.stdin.fileno()
1011        #old = termios.tcgetattr(fd) # remember current state
1012        #attr = termios.tcgetattr(fd)
1013        #attr[3] = attr[3] | termios.ICANON # ICANON must be set to recognize EOF
1014        #try: # use try/finally to ensure state gets restored
1015        #    termios.tcsetattr(fd, termios.TCSADRAIN, attr)
1016        #    if hasattr(termios, 'CEOF'):
1017        #        os.write (self.child_fd, '%c' % termios.CEOF)
1018        #    else:
1019        #        # Silly platform does not define CEOF so assume CTRL-D
1020        #        os.write (self.child_fd, '%c' % 4)
1021        #finally: # restore state
1022        #    termios.tcsetattr(fd, termios.TCSADRAIN, old)
1023        if hasattr(termios, 'VEOF'):
1024            char = termios.tcgetattr(self.child_fd)[6][termios.VEOF]
1025        else:
1026            # platform does not define VEOF so assume CTRL-D
1027            char = chr(4)
1028        self.send(char)
1029
1030    def sendintr(self):
1031
1032        """This sends a SIGINT to the child. It does not require
1033        the SIGINT to be the first character on a line. """
1034
1035        if hasattr(termios, 'VINTR'):
1036            char = termios.tcgetattr(self.child_fd)[6][termios.VINTR]
1037        else:
1038            # platform does not define VINTR so assume CTRL-C
1039            char = chr(3)
1040        self.send (char)
1041
1042    def eof (self):
1043
1044        """This returns True if the EOF exception was ever raised.
1045        """
1046
1047        return self.flag_eof
1048
1049    def terminate(self, force=False):
1050
1051        """This forces a child process to terminate. It starts nicely with
1052        SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This
1053        returns True if the child was terminated. This returns False if the
1054        child could not be terminated. """
1055
1056        if not self.isalive():
1057            return True
1058        try:
1059            self.kill(signal.SIGHUP)
1060            time.sleep(self.delayafterterminate)
1061            if not self.isalive():
1062                return True
1063            self.kill(signal.SIGCONT)
1064            time.sleep(self.delayafterterminate)
1065            if not self.isalive():
1066                return True
1067            self.kill(signal.SIGINT)
1068            time.sleep(self.delayafterterminate)
1069            if not self.isalive():
1070                return True
1071            if force:
1072                self.kill(signal.SIGKILL)
1073                time.sleep(self.delayafterterminate)
1074                if not self.isalive():
1075                    return True
1076                else:
1077                    return False
1078            return False
1079        except OSError, e:
1080            # I think there are kernel timing issues that sometimes cause
1081            # this to happen. I think isalive() reports True, but the
1082            # process is dead to the kernel.
1083            # Make one last attempt to see if the kernel is up to date.
1084            time.sleep(self.delayafterterminate)
1085            if not self.isalive():
1086                return True
1087            else:
1088                return False
1089
1090    def wait(self):
1091
1092        """This waits until the child exits. This is a blocking call. This will
1093        not read any data from the child, so this will block forever if the
1094        child has unread output and has terminated. In other words, the child
1095        may have printed output then called exit(); but, technically, the child
1096        is still alive until its output is read. """
1097
1098        if self.isalive():
1099            pid, status = os.waitpid(self.pid, 0)
1100        else:
1101            raise ExceptionPexpect ('Cannot wait for dead child process.')
1102        self.exitstatus = os.WEXITSTATUS(status)
1103        if os.WIFEXITED (status):
1104            self.status = status
1105            self.exitstatus = os.WEXITSTATUS(status)
1106            self.signalstatus = None
1107            self.terminated = True
1108        elif os.WIFSIGNALED (status):
1109            self.status = status
1110            self.exitstatus = None
1111            self.signalstatus = os.WTERMSIG(status)
1112            self.terminated = True
1113        elif os.WIFSTOPPED (status):
1114            raise ExceptionPexpect ('Wait was called for a child process that is stopped. This is not supported. Is some other process attempting job control with our child pid?')
1115        return self.exitstatus
1116
1117    def isalive(self):
1118
1119        """This tests if the child process is running or not. This is
1120        non-blocking. If the child was terminated then this will read the
1121        exitstatus or signalstatus of the child. This returns True if the child
1122        process appears to be running or False if not. It can take literally
1123        SECONDS for Solaris to return the right status. """
1124
1125        if self.terminated:
1126            return False
1127
1128        if self.flag_eof:
1129            # This is for Linux, which requires the blocking form of waitpid to get
1130            # status of a defunct process. This is super-lame. The flag_eof would have
1131            # been set in read_nonblocking(), so this should be safe.
1132            waitpid_options = 0
1133        else:
1134            waitpid_options = os.WNOHANG
1135
1136        try:
1137            pid, status = os.waitpid(self.pid, waitpid_options)
1138        except OSError, e: # No child processes
1139            if e[0] == errno.ECHILD:
1140                raise ExceptionPexpect ('isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?')
1141            else:
1142                raise e
1143
1144        # I have to do this twice for Solaris. I can't even believe that I figured this out...
1145        # If waitpid() returns 0 it means that no child process wishes to
1146        # report, and the value of status is undefined.
1147        if pid == 0:
1148            try:
1149                pid, status = os.waitpid(self.pid, waitpid_options) ### os.WNOHANG) # Solaris!
1150            except OSError, e: # This should never happen...
1151                if e[0] == errno.ECHILD:
1152                    raise ExceptionPexpect ('isalive() encountered condition that should never happen. There was no child process. Did someone else call waitpid() on our process?')
1153                else:
1154                    raise e
1155
1156            # If pid is still 0 after two calls to waitpid() then
1157            # the process really is alive. This seems to work on all platforms, except
1158            # for Irix which seems to require a blocking call on waitpid or select, so I let read_nonblocking
1159            # take care of this situation (unfortunately, this requires waiting through the timeout).
1160            if pid == 0:
1161                return True
1162
1163        if pid == 0:
1164            return True
1165
1166        if os.WIFEXITED (status):
1167            self.status = status
1168            self.exitstatus = os.WEXITSTATUS(status)
1169            self.signalstatus = None
1170            self.terminated = True
1171        elif os.WIFSIGNALED (status):
1172            self.status = status
1173            self.exitstatus = None
1174            self.signalstatus = os.WTERMSIG(status)
1175            self.terminated = True
1176        elif os.WIFSTOPPED (status):
1177            raise ExceptionPexpect ('isalive() encountered condition where child process is stopped. This is not supported. Is some other process attempting job control with our child pid?')
1178        return False
1179
1180    def kill(self, sig):
1181
1182        """This sends the given signal to the child application. In keeping
1183        with UNIX tradition it has a misleading name. It does not necessarily
1184        kill the child unless you send the right signal. """
1185
1186        # Same as os.kill, but the pid is given for you.
1187        if self.isalive():
1188            os.kill(self.pid, sig)
1189
1190    def compile_pattern_list(self, patterns):
1191
1192        """This compiles a pattern-string or a list of pattern-strings.
1193        Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or a list of
1194        those. Patterns may also be None which results in an empty list (you
1195        might do this if waiting for an EOF or TIMEOUT condition without
1196        expecting any pattern).
1197
1198        This is used by expect() when calling expect_list(). Thus expect() is
1199        nothing more than::
1200
1201             cpl = self.compile_pattern_list(pl)
1202             return self.expect_list(cpl, timeout)
1203
1204        If you are using expect() within a loop it may be more
1205        efficient to compile the patterns first and then call expect_list().
1206        This avoid calls in a loop to compile_pattern_list()::
1207
1208             cpl = self.compile_pattern_list(my_pattern)
1209             while some_condition:
1210                ...
1211                i = self.expect_list(clp, timeout)
1212                ...
1213        """
1214
1215        if patterns is None:
1216            return []
1217        if type(patterns) is not types.ListType:
1218            patterns = [patterns]
1219
1220        compile_flags = re.DOTALL # Allow dot to match \n
1221        if self.ignorecase:
1222            compile_flags = compile_flags | re.IGNORECASE
1223        compiled_pattern_list = []
1224        for p in patterns:
1225            if type(p) in types.StringTypes:
1226                compiled_pattern_list.append(re.compile(p, compile_flags))
1227            elif p is EOF:
1228                compiled_pattern_list.append(EOF)
1229            elif p is TIMEOUT:
1230                compiled_pattern_list.append(TIMEOUT)
1231            elif type(p) is type(re.compile('')):
1232                compiled_pattern_list.append(p)
1233            else:
1234                raise TypeError ('Argument must be one of StringTypes, EOF, TIMEOUT, SRE_Pattern, or a list of those type. %s' % str(type(p)))
1235
1236        return compiled_pattern_list
1237
1238    def expect(self, pattern, timeout = -1, searchwindowsize=-1):
1239
1240        """This seeks through the stream until a pattern is matched. The
1241        pattern is overloaded and may take several types. The pattern can be a
1242        StringType, EOF, a compiled re, or a list of any of those types.
1243        Strings will be compiled to re types. This returns the index into the
1244        pattern list. If the pattern was not a list this returns index 0 on a
1245        successful match. This may raise exceptions for EOF or TIMEOUT. To
1246        avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to the pattern
1247        list. That will cause expect to match an EOF or TIMEOUT condition
1248        instead of raising an exception.
1249
1250        If you pass a list of patterns and more than one matches, the first match
1251        in the stream is chosen. If more than one pattern matches at that point,
1252        the leftmost in the pattern list is chosen. For example::
1253
1254            # the input is 'foobar'
1255            index = p.expect (['bar', 'foo', 'foobar'])
1256            # returns 1 ('foo') even though 'foobar' is a "better" match
1257
1258        Please note, however, that buffering can affect this behavior, since
1259        input arrives in unpredictable chunks. For example::
1260
1261            # the input is 'foobar'
1262            index = p.expect (['foobar', 'foo'])
1263            # returns 0 ('foobar') if all input is available at once,
1264            # but returs 1 ('foo') if parts of the final 'bar' arrive late
1265
1266        After a match is found the instance attributes 'before', 'after' and
1267        'match' will be set. You can see all the data read before the match in
1268        'before'. You can see the data that was matched in 'after'. The
1269        re.MatchObject used in the re match will be in 'match'. If an error
1270        occurred then 'before' will be set to all the data read so far and
1271        'after' and 'match' will be None.
1272
1273        If timeout is -1 then timeout will be set to the self.timeout value.
1274
1275        A list entry may be EOF or TIMEOUT instead of a string. This will
1276        catch these exceptions and return the index of the list entry instead
1277        of raising the exception. The attribute 'after' will be set to the
1278        exception type. The attribute 'match' will be None. This allows you to
1279        write code like this::
1280
1281                index = p.expect (['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])
1282                if index == 0:
1283                    do_something()
1284                elif index == 1:
1285                    do_something_else()
1286                elif index == 2:
1287                    do_some_other_thing()
1288                elif index == 3:
1289                    do_something_completely_different()
1290
1291        instead of code like this::
1292
1293                try:
1294                    index = p.expect (['good', 'bad'])
1295                    if index == 0:
1296                        do_something()
1297                    elif index == 1:
1298                        do_something_else()
1299                except EOF:
1300                    do_some_other_thing()
1301                except TIMEOUT:
1302                    do_something_completely_different()
1303
1304        These two forms are equivalent. It all depends on what you want. You
1305        can also just expect the EOF if you are waiting for all output of a
1306        child to finish. For example::
1307
1308                p = pexpect.spawn('/bin/ls')
1309                p.expect (pexpect.EOF)
1310                print p.before
1311
1312        If you are trying to optimize for speed then see expect_list().
1313        """
1314
1315        compiled_pattern_list = self.compile_pattern_list(pattern)
1316        return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
1317
1318    def expect_list(self, pattern_list, timeout = -1, searchwindowsize = -1):
1319
1320        """This takes a list of compiled regular expressions and returns the
1321        index into the pattern_list that matched the child output. The list may
1322        also contain EOF or TIMEOUT (which are not compiled regular
1323        expressions). This method is similar to the expect() method except that
1324        expect_list() does not recompile the pattern list on every call. This
1325        may help if you are trying to optimize for speed, otherwise just use
1326        the expect() method.  This is called by expect(). If timeout==-1 then
1327        the self.timeout value is used. If searchwindowsize==-1 then the
1328        self.searchwindowsize value is used. """
1329
1330        return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize)
1331
1332    def expect_exact(self, pattern_list, timeout = -1, searchwindowsize = -1):
1333
1334        """This is similar to expect(), but uses plain string matching instead
1335        of compiled regular expressions in 'pattern_list'. The 'pattern_list'
1336        may be a string; a list or other sequence of strings; or TIMEOUT and
1337        EOF.
1338
1339        This call might be faster than expect() for two reasons: string
1340        searching is faster than RE matching and it is possible to limit the
1341        search to just the end of the input buffer.
1342
1343        This method is also useful when you don't want to have to worry about
1344        escaping regular expression characters that you want to match."""
1345
1346        if type(pattern_list) in types.StringTypes or pattern_list in (TIMEOUT, EOF):
1347            pattern_list = [pattern_list]
1348        return self.expect_loop(searcher_string(pattern_list), timeout, searchwindowsize)
1349
1350    def expect_loop(self, searcher, timeout = -1, searchwindowsize = -1):
1351
1352        """This is the common loop used inside expect. The 'searcher' should be
1353        an instance of searcher_re or searcher_string, which describes how and what
1354        to search for in the input.
1355
1356        See expect() for other arguments, return value and exceptions. """
1357
1358        self.searcher = searcher
1359
1360        if timeout == -1:
1361            timeout = self.timeout
1362        if timeout is not None:
1363            end_time = time.time() + timeout
1364        if searchwindowsize == -1:
1365            searchwindowsize = self.searchwindowsize
1366
1367        try:
1368            incoming = self.buffer
1369            freshlen = len(incoming)
1370            while True: # Keep reading until exception or return.
1371                index = searcher.search(incoming, freshlen, searchwindowsize)
1372                if index >= 0:
1373                    self.buffer = incoming[searcher.end : ]
1374                    self.before = incoming[ : searcher.start]
1375                    self.after = incoming[searcher.start : searcher.end]
1376                    self.match = searcher.match
1377                    self.match_index = index
1378                    return self.match_index
1379                # No match at this point
1380                if timeout < 0 and timeout is not None:
1381                    raise TIMEOUT ('Timeout exceeded in expect_any().')
1382                # Still have time left, so read more data
1383                c = self.read_nonblocking (self.maxread, timeout)
1384                freshlen = len(c)
1385                time.sleep (0.0001)
1386                incoming = incoming + c
1387                if timeout is not None:
1388                    timeout = end_time - time.time()
1389        except EOF, e:
1390            self.buffer = ''
1391            self.before = incoming
1392            self.after = EOF
1393            index = searcher.eof_index
1394            if index >= 0:
1395                self.match = EOF
1396                self.match_index = index
1397                return self.match_index
1398            else:
1399                self.match = None
1400                self.match_index = None
1401                raise EOF (str(e) + '\n' + str(self))
1402        except TIMEOUT, e:
1403            self.buffer = incoming
1404            self.before = incoming
1405            self.after = TIMEOUT
1406            index = searcher.timeout_index
1407            if index >= 0:
1408                self.match = TIMEOUT
1409                self.match_index = index
1410                return self.match_index
1411            else:
1412                self.match = None
1413                self.match_index = None
1414                raise TIMEOUT (str(e) + '\n' + str(self))
1415        except:
1416            self.before = incoming
1417            self.after = None
1418            self.match = None
1419            self.match_index = None
1420            raise
1421
1422    def getwinsize(self):
1423
1424        """This returns the terminal window size of the child tty. The return
1425        value is a tuple of (rows, cols). """
1426
1427        TIOCGWINSZ = getattr(termios, 'TIOCGWINSZ', 1074295912L)
1428        s = struct.pack('HHHH', 0, 0, 0, 0)
1429        x = fcntl.ioctl(self.fileno(), TIOCGWINSZ, s)
1430        return struct.unpack('HHHH', x)[0:2]
1431
1432    def setwinsize(self, r, c):
1433
1434        """This sets the terminal window size of the child tty. This will cause
1435        a SIGWINCH signal to be sent to the child. This does not change the
1436        physical window size. It changes the size reported to TTY-aware
1437        applications like vi or curses -- applications that respond to the
1438        SIGWINCH signal. """
1439
1440        # Check for buggy platforms. Some Python versions on some platforms
1441        # (notably OSF1 Alpha and RedHat 7.1) truncate the value for
1442        # termios.TIOCSWINSZ. It is not clear why this happens.
1443        # These platforms don't seem to handle the signed int very well;
1444        # yet other platforms like OpenBSD have a large negative value for
1445        # TIOCSWINSZ and they don't have a truncate problem.
1446        # Newer versions of Linux have totally different values for TIOCSWINSZ.
1447        # Note that this fix is a hack.
1448        TIOCSWINSZ = getattr(termios, 'TIOCSWINSZ', -2146929561)
1449        if TIOCSWINSZ == 2148037735L: # L is not required in Python >= 2.2.
1450            TIOCSWINSZ = -2146929561 # Same bits, but with sign.
1451        # Note, assume ws_xpixel and ws_ypixel are zero.
1452        s = struct.pack('HHHH', r, c, 0, 0)
1453        fcntl.ioctl(self.fileno(), TIOCSWINSZ, s)
1454
1455    def interact(self, escape_character = chr(29), input_filter = None, output_filter = None):
1456
1457        """This gives control of the child process to the interactive user (the
1458        human at the keyboard). Keystrokes are sent to the child process, and
1459        the stdout and stderr output of the child process is printed. This
1460        simply echos the child stdout and child stderr to the real stdout and
1461        it echos the real stdin to the child stdin. When the user types the
1462        escape_character this method will stop. The default for
1463        escape_character is ^]. This should not be confused with ASCII 27 --
1464        the ESC character. ASCII 29 was chosen for historical merit because
1465        this is the character used by 'telnet' as the escape character. The
1466        escape_character will not be sent to the child process.
1467
1468        You may pass in optional input and output filter functions. These
1469        functions should take a string and return a string. The output_filter
1470        will be passed all the output from the child process. The input_filter
1471        will be passed all the keyboard input from the user. The input_filter
1472        is run BEFORE the check for the escape_character.
1473
1474        Note that if you change the window size of the parent the SIGWINCH
1475        signal will not be passed through to the child. If you want the child
1476        window size to change when the parent's window size changes then do
1477        something like the following example::
1478
1479            import pexpect, struct, fcntl, termios, signal, sys
1480            def sigwinch_passthrough (sig, data):
1481                s = struct.pack("HHHH", 0, 0, 0, 0)
1482                a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ , s))
1483                global p
1484                p.setwinsize(a[0],a[1])
1485            p = pexpect.spawn('/bin/bash') # Note this is global and used in sigwinch_passthrough.
1486            signal.signal(signal.SIGWINCH, sigwinch_passthrough)
1487            p.interact()
1488        """
1489
1490        # Flush the buffer.
1491        self.stdout.write (self.buffer)
1492        self.stdout.flush()
1493        self.buffer = ''
1494        mode = tty.tcgetattr(self.STDIN_FILENO)
1495        tty.setraw(self.STDIN_FILENO)
1496        try:
1497            self.__interact_copy(escape_character, input_filter, output_filter)
1498        finally:
1499            tty.tcsetattr(self.STDIN_FILENO, tty.TCSAFLUSH, mode)
1500
1501    def __interact_writen(self, fd, data):
1502
1503        """This is used by the interact() method.
1504        """
1505
1506        while data != '' and self.isalive():
1507            n = os.write(fd, data)
1508            data = data[n:]
1509
1510    def __interact_read(self, fd):
1511
1512        """This is used by the interact() method.
1513        """
1514
1515        return os.read(fd, 1000)
1516
1517    def __interact_copy(self, escape_character = None, input_filter = None, output_filter = None):
1518
1519        """This is used by the interact() method.
1520        """
1521
1522        while self.isalive():
1523            r,w,e = self.__select([self.child_fd, self.STDIN_FILENO], [], [])
1524            if self.child_fd in r:
1525                data = self.__interact_read(self.child_fd)
1526                if output_filter: data = output_filter(data)
1527                if self.logfile is not None:
1528                    self.logfile.write (data)
1529                    self.logfile.flush()
1530                os.write(self.STDOUT_FILENO, data)
1531            if self.STDIN_FILENO in r:
1532                data = self.__interact_read(self.STDIN_FILENO)
1533                if input_filter: data = input_filter(data)
1534                i = data.rfind(escape_character)
1535                if i != -1:
1536                    data = data[:i]
1537                    self.__interact_writen(self.child_fd, data)
1538                    break
1539                self.__interact_writen(self.child_fd, data)
1540
1541    def __select (self, iwtd, owtd, ewtd, timeout=None):
1542
1543        """This is a wrapper around select.select() that ignores signals. If
1544        select.select raises a select.error exception and errno is an EINTR
1545        error then it is ignored. Mainly this is used to ignore sigwinch
1546        (terminal resize). """
1547
1548        # if select() is interrupted by a signal (errno==EINTR) then
1549        # we loop back and enter the select() again.
1550        if timeout is not None:
1551            end_time = time.time() + timeout
1552        while True:
1553            try:
1554                return select.select (iwtd, owtd, ewtd, timeout)
1555            except select.error, e:
1556                if e[0] == errno.EINTR:
1557                    # if we loop back we have to subtract the amount of time we already waited.
1558                    if timeout is not None:
1559                        timeout = end_time - time.time()
1560                        if timeout < 0:
1561                            return ([],[],[])
1562                else: # something else caused the select.error, so this really is an exception
1563                    raise
1564
1565##############################################################################
1566# The following methods are no longer supported or allowed.
1567
1568    def setmaxread (self, maxread):
1569
1570        """This method is no longer supported or allowed. I don't like getters
1571        and setters without a good reason. """
1572
1573        raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the maxread member variable.')
1574
1575    def setlog (self, fileobject):
1576
1577        """This method is no longer supported or allowed.
1578        """
1579
1580        raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the logfile member variable.')
1581
1582##############################################################################
1583# End of spawn class
1584##############################################################################
1585
1586class searcher_string (object):
1587
1588    """This is a plain string search helper for the spawn.expect_any() method.
1589
1590    Attributes:
1591
1592        eof_index     - index of EOF, or -1
1593        timeout_index - index of TIMEOUT, or -1
1594
1595    After a successful match by the search() method the following attributes
1596    are available:
1597
1598        start - index into the buffer, first byte of match
1599        end   - index into the buffer, first byte after match
1600        match - the matching string itself
1601    """
1602
1603    def __init__(self, strings):
1604
1605        """This creates an instance of searcher_string. This argument 'strings'
1606        may be a list; a sequence of strings; or the EOF or TIMEOUT types. """
1607
1608        self.eof_index = -1
1609        self.timeout_index = -1
1610        self._strings = []
1611        for n, s in zip(range(len(strings)), strings):
1612            if s is EOF:
1613                self.eof_index = n
1614                continue
1615            if s is TIMEOUT:
1616                self.timeout_index = n
1617                continue
1618            self._strings.append((n, s))
1619
1620    def __str__(self):
1621
1622        """This returns a human-readable string that represents the state of
1623        the object."""
1624
1625        ss =  [ (ns[0],'    %d: "%s"' % ns) for ns in self._strings ]
1626        ss.append((-1,'searcher_string:'))
1627        if self.eof_index >= 0:
1628            ss.append ((self.eof_index,'    %d: EOF' % self.eof_index))
1629        if self.timeout_index >= 0:
1630            ss.append ((self.timeout_index,'    %d: TIMEOUT' % self.timeout_index))
1631        ss.sort()
1632        ss = zip(*ss)[1]
1633        return '\n'.join(ss)
1634
1635    def search(self, buffer, freshlen, searchwindowsize=None):
1636
1637        """This searches 'buffer' for the first occurence of one of the search
1638        strings.  'freshlen' must indicate the number of bytes at the end of
1639        'buffer' which have not been searched before. It helps to avoid
1640        searching the same, possibly big, buffer over and over again.
1641
1642        See class spawn for the 'searchwindowsize' argument.
1643
1644        If there is a match this returns the index of that string, and sets
1645        'start', 'end' and 'match'. Otherwise, this returns -1. """
1646
1647        absurd_match = len(buffer)
1648        first_match = absurd_match
1649
1650        # 'freshlen' helps a lot here. Further optimizations could
1651        # possibly include:
1652        #
1653        # using something like the Boyer-Moore Fast String Searching
1654        # Algorithm; pre-compiling the search through a list of
1655        # strings into something that can scan the input once to
1656        # search for all N strings; realize that if we search for
1657        # ['bar', 'baz'] and the input is '...foo' we need not bother
1658        # rescanning until we've read three more bytes.
1659        #
1660        # Sadly, I don't know enough about this interesting topic. /grahn
1661
1662        for index, s in self._strings:
1663            if searchwindowsize is None:
1664                # the match, if any, can only be in the fresh data,
1665                # or at the very end of the old data
1666                offset = -(freshlen+len(s))
1667            else:
1668                # better obey searchwindowsize
1669                offset = -searchwindowsize
1670            n = buffer.find(s, offset)
1671            if n >= 0 and n < first_match:
1672                first_match = n
1673                best_index, best_match = index, s
1674        if first_match == absurd_match:
1675            return -1
1676        self.match = best_match
1677        self.start = first_match
1678        self.end = self.start + len(self.match)
1679        return best_index
1680
1681class searcher_re (object):
1682
1683    """This is regular expression string search helper for the
1684    spawn.expect_any() method.
1685
1686    Attributes:
1687
1688        eof_index     - index of EOF, or -1
1689        timeout_index - index of TIMEOUT, or -1
1690
1691    After a successful match by the search() method the following attributes
1692    are available:
1693
1694        start - index into the buffer, first byte of match
1695        end   - index into the buffer, first byte after match
1696        match - the re.match object returned by a succesful re.search
1697
1698    """
1699
1700    def __init__(self, patterns):
1701
1702        """This creates an instance that searches for 'patterns' Where
1703        'patterns' may be a list or other sequence of compiled regular
1704        expressions, or the EOF or TIMEOUT types."""
1705
1706        self.eof_index = -1
1707        self.timeout_index = -1
1708        self._searches = []
1709        for n, s in zip(range(len(patterns)), patterns):
1710            if s is EOF:
1711                self.eof_index = n
1712                continue
1713            if s is TIMEOUT:
1714                self.timeout_index = n
1715                continue
1716            self._searches.append((n, s))
1717
1718    def __str__(self):
1719
1720        """This returns a human-readable string that represents the state of
1721        the object."""
1722
1723        ss =  [ (n,'    %d: re.compile("%s")' % (n,str(s.pattern))) for n,s in self._searches]
1724        ss.append((-1,'searcher_re:'))
1725        if self.eof_index >= 0:
1726            ss.append ((self.eof_index,'    %d: EOF' % self.eof_index))
1727        if self.timeout_index >= 0:
1728            ss.append ((self.timeout_index,'    %d: TIMEOUT' % self.timeout_index))
1729        ss.sort()
1730        ss = zip(*ss)[1]
1731        return '\n'.join(ss)
1732
1733    def search(self, buffer, freshlen, searchwindowsize=None):
1734
1735        """This searches 'buffer' for the first occurence of one of the regular
1736        expressions. 'freshlen' must indicate the number of bytes at the end of
1737        'buffer' which have not been searched before.
1738
1739        See class spawn for the 'searchwindowsize' argument.
1740
1741        If there is a match this returns the index of that string, and sets
1742        'start', 'end' and 'match'. Otherwise, returns -1."""
1743
1744        absurd_match = len(buffer)
1745        first_match = absurd_match
1746        # 'freshlen' doesn't help here -- we cannot predict the
1747        # length of a match, and the re module provides no help.
1748        if searchwindowsize is None:
1749            searchstart = 0
1750        else:
1751            searchstart = max(0, len(buffer)-searchwindowsize)
1752        for index, s in self._searches:
1753            match = s.search(buffer, searchstart)
1754            if match is None:
1755                continue
1756            n = match.start()
1757            if n < first_match:
1758                first_match = n
1759                the_match = match
1760                best_index = index
1761        if first_match == absurd_match:
1762            return -1
1763        self.start = first_match
1764        self.match = the_match
1765        self.end = self.match.end()
1766        return best_index
1767
1768def which (filename):
1769
1770    """This takes a given filename; tries to find it in the environment path;
1771    then checks if it is executable. This returns the full path to the filename
1772    if found and executable. Otherwise this returns None."""
1773
1774    # Special case where filename already contains a path.
1775    if os.path.dirname(filename) != '':
1776        if os.access (filename, os.X_OK):
1777            return filename
1778
1779    if not os.environ.has_key('PATH') or os.environ['PATH'] == '':
1780        p = os.defpath
1781    else:
1782        p = os.environ['PATH']
1783
1784    # Oddly enough this was the one line that made Pexpect
1785    # incompatible with Python 1.5.2.
1786    #pathlist = p.split (os.pathsep)
1787    pathlist = string.split (p, os.pathsep)
1788
1789    for path in pathlist:
1790        f = os.path.join(path, filename)
1791        if os.access(f, os.X_OK):
1792            return f
1793    return None
1794
1795def split_command_line(command_line):
1796
1797    """This splits a command line into a list of arguments. It splits arguments
1798    on spaces, but handles embedded quotes, doublequotes, and escaped
1799    characters. It's impossible to do this with a regular expression, so I
1800    wrote a little state machine to parse the command line. """
1801
1802    arg_list = []
1803    arg = ''
1804
1805    # Constants to name the states we can be in.
1806    state_basic = 0
1807    state_esc = 1
1808    state_singlequote = 2
1809    state_doublequote = 3
1810    state_whitespace = 4 # The state of consuming whitespace between commands.
1811    state = state_basic
1812
1813    for c in command_line:
1814        if state == state_basic or state == state_whitespace:
1815            if c == '\\': # Escape the next character
1816                state = state_esc
1817            elif c == r"'": # Handle single quote
1818                state = state_singlequote
1819            elif c == r'"': # Handle double quote
1820                state = state_doublequote
1821            elif c.isspace():
1822                # Add arg to arg_list if we aren't in the middle of whitespace.
1823                if state == state_whitespace:
1824                    None # Do nothing.
1825                else:
1826                    arg_list.append(arg)
1827                    arg = ''
1828                    state = state_whitespace
1829            else:
1830                arg = arg + c
1831                state = state_basic
1832        elif state == state_esc:
1833            arg = arg + c
1834            state = state_basic
1835        elif state == state_singlequote:
1836            if c == r"'":
1837                state = state_basic
1838            else:
1839                arg = arg + c
1840        elif state == state_doublequote:
1841            if c == r'"':
1842                state = state_basic
1843            else:
1844                arg = arg + c
1845
1846    if arg != '':
1847        arg_list.append(arg)
1848    return arg_list
1849
1850# vi:ts=4:sw=4:expandtab:ft=python:
1851