move pcucontrol package into pcucontrol module.
[pcucontrol.git] / pcucontrol / transports / ssh / pexpect.py
1 """Pexpect is a Python module for spawning child applications and controlling
2 them automatically. Pexpect can be used for automating interactive applications
3 such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup
4 scripts for duplicating software package installations on different servers. It
5 can be used for automated software testing. Pexpect is in the spirit of Don
6 Libes' Expect, but Pexpect is pure Python. Other Expect-like modules for Python
7 require TCL and Expect or require C extensions to be compiled. Pexpect does not
8 use C, Expect, or TCL extensions. It should work on any platform that supports
9 the standard Python pty module. The Pexpect interface focuses on ease of use so
10 that simple tasks are easy.
11
12 There are two main interfaces to Pexpect -- the function, run() and the class,
13 spawn. You can call the run() function to execute a command and return the
14 output. This is a handy replacement for os.system().
15
16 For example:
17     pexpect.run('ls -la')
18
19 The more powerful interface is the spawn class. You can use this to spawn an
20 external child command and then interact with the child by sending lines and
21 expecting responses.
22
23 For example:
24     child = pexpect.spawn('scp foo myname@host.example.com:.')
25     child.expect ('Password:')
26     child.sendline (mypassword)
27
28 This works even for commands that ask for passwords or other input outside of
29 the normal stdio streams.
30
31 Credits:
32 Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett, Robert Stone,
33 Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids vander Molen,
34 George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin,
35 Geoffrey Marshall, Francisco Lourenco, Glen Mabey, Karthik Gurusamy,
36 Fernando Perez 
37 (Let me know if I forgot anyone.)
38
39 Free, open source, and all that good stuff.
40
41 Permission is hereby granted, free of charge, to any person obtaining a copy of
42 this software and associated documentation files (the "Software"), to deal in
43 the Software without restriction, including without limitation the rights to
44 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
45 of the Software, and to permit persons to whom the Software is furnished to do
46 so, subject to the following conditions:
47
48 The above copyright notice and this permission notice shall be included in all
49 copies or substantial portions of the Software.
50
51 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
52 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
53 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
54 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
55 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
56 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
57 SOFTWARE.
58
59 Pexpect Copyright (c) 2006 Noah Spurrier
60 http://pexpect.sourceforge.net/
61
62 $Revision: 395 $
63 $Date: 2006-05-31 20:07:18 -0700 (Wed, 31 May 2006) $
64 """
65 try:
66     import os, sys, time
67     import select
68     import string
69     import re
70     import struct
71     import resource
72     import types
73     import pty
74     import tty
75     import termios
76     import fcntl
77     import errno
78     import traceback
79     import signal
80 except ImportError, e:
81     raise ImportError (str(e) + """
82 A critical module was not found. Probably this operating system does not support it.
83 Pexpect is intended for UNIX-like operating systems.""")
84
85 __version__ = '2.1'
86 __revision__ = '$Revision: 395 $'
87 __all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'run', 'which', 'split_command_line',
88     '__version__', '__revision__']
89
90 # Exception classes used by this module.
91 class ExceptionPexpect(Exception):
92     """Base class for all exceptions raised by this module.
93     """
94     def __init__(self, value):
95         self.value = value
96     def __str__(self):
97         return str(self.value)
98     def get_trace(self):
99         """This returns an abbreviated stack trace with lines that only concern the caller.
100         In other words, the stack trace inside the Pexpect module is not included.
101         """
102         tblist = traceback.extract_tb(sys.exc_info()[2])
103         tblist = filter(self.__filter_not_pexpect, tblist)
104         tblist = traceback.format_list(tblist)
105         return ''.join(tblist)
106     def __filter_not_pexpect(self, trace_list_item):
107         if trace_list_item[0].find('pexpect.py') == -1:
108             return True
109         else:
110             return False
111 class EOF(ExceptionPexpect):
112     """Raised when EOF is read from a child.
113     """
114 class TIMEOUT(ExceptionPexpect):
115     """Raised when a read time exceeds the timeout.
116     """
117 ##class TIMEOUT_PATTERN(TIMEOUT):
118 ##    """Raised when the pattern match time exceeds the timeout.
119 ##    This is different than a read TIMEOUT because the child process may
120 ##    give output, thus never give a TIMEOUT, but the output
121 ##    may never match a pattern.
122 ##    """
123 ##class MAXBUFFER(ExceptionPexpect):
124 ##    """Raised when a scan buffer fills before matching an expected pattern."""
125
126 def run (command, timeout=-1, withexitstatus=False, events=None, extra_args=None, logfile=None):
127     """This function runs the given command; waits for it to finish;
128     then returns all output as a string. STDERR is included in output.
129     If the full path to the command is not given then the path is searched.
130
131     Note that lines are terminated by CR/LF (\\r\\n) combination
132     even on UNIX-like systems because this is the standard for pseudo ttys.
133     If you set withexitstatus to true, then run will return a tuple of
134     (command_output, exitstatus). If withexitstatus is false then this
135     returns just command_output.
136
137     The run() function can often be used instead of creating a spawn instance.
138     For example, the following code uses spawn:
139         from pexpect import *
140         child = spawn('scp foo myname@host.example.com:.')
141         child.expect ('(?i)password')
142         child.sendline (mypassword)
143     The previous code can be replace with the following, which you may
144     or may not find simpler:
145         from pexpect import *
146         run ('scp foo myname@host.example.com:.', events={'(?i)password': mypassword})
147
148     Examples:
149     Start the apache daemon on the local machine:
150         from pexpect import *
151         run ("/usr/local/apache/bin/apachectl start")
152     Check in a file using SVN:
153         from pexpect import *
154         run ("svn ci -m 'automatic commit' my_file.py")
155     Run a command and capture exit status:
156         from pexpect import *
157         (command_output, exitstatus) = run ('ls -l /bin', withexitstatus=1)
158
159     Tricky Examples:   
160     The following will run SSH and execute 'ls -l' on the remote machine.
161     The password 'secret' will be sent if the '(?i)password' pattern is ever seen.
162         run ("ssh username@machine.example.com 'ls -l'", events={'(?i)password':'secret\n'})
163
164     This will start mencoder to rip a video from DVD. This will also display
165     progress ticks every 5 seconds as it runs.
166         from pexpect import *
167         def print_ticks(d):
168             print d['event_count'],
169         run ("mencoder dvd://1 -o video.avi -oac copy -ovc copy", events={TIMEOUT:print_ticks}, timeout=5)
170
171     The 'events' argument should be a dictionary of patterns and responses.
172     Whenever one of the patterns is seen in the command out
173     run() will send the associated response string. Note that you should
174     put newlines in your string if Enter is necessary.
175     The responses may also contain callback functions.
176     Any callback is function that takes a dictionary as an argument.
177     The dictionary contains all the locals from the run() function, so
178     you can access the child spawn object or any other variable defined
179     in run() (event_count, child, and extra_args are the most useful).
180     A callback may return True to stop the current run process otherwise
181     run() continues until the next event.
182     A callback may also return a string which will be sent to the child.
183     'extra_args' is not used by directly run(). It provides a way to pass data to
184     a callback function through run() through the locals dictionary passed to a callback.
185     """
186     if timeout == -1:
187         child = spawn(command, maxread=2000, logfile=logfile)
188     else:
189         child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile)
190     if events is not None:
191         patterns = events.keys()
192         responses = events.values()
193     else:
194         patterns=None # We assume that EOF or TIMEOUT will save us.
195         responses=None
196     child_result_list = []
197     event_count = 0
198     while 1:
199         try:
200             index = child.expect (patterns)
201             if type(child.after) is types.StringType:
202                 child_result_list.append(child.before + child.after)
203             else: # child.after may have been a TIMEOUT or EOF, so don't cat those.
204                 child_result_list.append(child.before)
205             if type(responses[index]) is types.StringType:
206                 child.send(responses[index])
207             elif type(responses[index]) is types.FunctionType:
208                 callback_result = responses[index](locals())
209                 sys.stdout.flush()
210                 if type(callback_result) is types.StringType:
211                     child.send(callback_result)
212                 elif callback_result:
213                     break
214             else:
215                 raise TypeError ('The callback must be a string or function type.')
216             event_count = event_count + 1
217         except TIMEOUT, e:
218             child_result_list.append(child.before)
219             break
220         except EOF, e:
221             child_result_list.append(child.before)
222             break
223     child_result = ''.join(child_result_list)
224     if withexitstatus:
225         child.close()
226         return (child_result, child.exitstatus)
227     else:
228         return child_result
229
230 class spawn (object):
231     """This is the main class interface for Pexpect.
232     Use this class to start and control child applications.
233     """
234
235     def __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None, env=None):
236         """This is the constructor. The command parameter may be a string
237         that includes a command and any arguments to the command. For example:
238             p = pexpect.spawn ('/usr/bin/ftp')
239             p = pexpect.spawn ('/usr/bin/ssh user@example.com')
240             p = pexpect.spawn ('ls -latr /tmp')
241         You may also construct it with a list of arguments like so:
242             p = pexpect.spawn ('/usr/bin/ftp', [])
243             p = pexpect.spawn ('/usr/bin/ssh', ['user@example.com'])
244             p = pexpect.spawn ('ls', ['-latr', '/tmp'])
245         After this the child application will be created and
246         will be ready to talk to. For normal use, see expect() and 
247         send() and sendline().
248
249         The maxread attribute sets the read buffer size.
250         This is maximum number of bytes that Pexpect will try to read
251         from a TTY at one time.
252         Seeting the maxread size to 1 will turn off buffering.
253         Setting the maxread value higher may help performance in cases
254         where large amounts of output are read back from the child.
255         This feature is useful in conjunction with searchwindowsize.
256         
257         The searchwindowsize attribute sets the how far back in
258         the incomming seach buffer Pexpect will search for pattern matches.
259         Every time Pexpect reads some data from the child it will append the data to
260         the incomming buffer. The default is to search from the beginning of the
261         imcomming buffer each time new data is read from the child.
262         But this is very inefficient if you are running a command that
263         generates a large amount of data where you want to match
264         The searchwindowsize does not effect the size of the incomming data buffer.
265         You will still have access to the full buffer after expect() returns.
266         
267         The logfile member turns on or off logging.
268         All input and output will be copied to the given file object.
269         Set logfile to None to stop logging. This is the default.
270         Set logfile to sys.stdout to echo everything to standard output.
271         The logfile is flushed after each write.
272         Example 1:
273             child = pexpect.spawn('some_command')
274             fout = file('mylog.txt','w')
275             child.logfile = fout
276         Example 2:
277             child = pexpect.spawn('some_command')
278             child.logfile = sys.stdout
279             
280         The delaybeforesend helps overcome a weird behavior that many users were experiencing.
281         The typical problem was that a user would expect() a "Password:" prompt and
282         then immediately call sendline() to send the password. The user would then
283         see that their password was echoed back to them. Passwords don't
284         normally echo. The problem is caused by the fact that most applications
285         print out the "Password" prompt and then turn off stdin echo, but if you
286         send your password before the application turned off echo, then you get
287         your password echoed. Normally this wouldn't be a problem when interacting
288         with a human at a real heyboard. If you introduce a slight delay just before 
289         writing then this seems to clear up the problem. This was such a common problem 
290         for many users that I decided that the default pexpect behavior
291         should be to sleep just before writing to the child application.
292         1/10th of a second (100 ms) seems to be enough to clear up the problem.
293         You can set delaybeforesend to 0 to return to the old behavior.
294         
295         Note that spawn is clever about finding commands on your path.
296         It uses the same logic that "which" uses to find executables.
297
298         If you wish to get the exit status of the child you must call
299         the close() method. The exit or signal status of the child will be
300         stored in self.exitstatus or self.signalstatus.
301         If the child exited normally then exitstatus will store the exit return code and
302         signalstatus will be None.
303         If the child was terminated abnormally with a signal then signalstatus will store
304         the signal value and exitstatus will be None.
305         If you need more detail you can also read the self.status member which stores
306         the status returned by os.waitpid. You can interpret this using
307         os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG.
308         """
309         self.STDIN_FILENO = pty.STDIN_FILENO
310         self.STDOUT_FILENO = pty.STDOUT_FILENO
311         self.STDERR_FILENO = pty.STDERR_FILENO
312         self.stdin = sys.stdin
313         self.stdout = sys.stdout
314         self.stderr = sys.stderr
315
316         self.patterns = None
317         self.ignorecase = False
318         self.before = None
319         self.after = None
320         self.match = None
321         self.match_index = None
322         self.terminated = True
323         self.exitstatus = None
324         self.signalstatus = None
325         self.status = None # status returned by os.waitpid 
326         self.flag_eof = False
327         self.pid = None
328         self.child_fd = -1 # initially closed
329         self.timeout = timeout
330         self.delimiter = EOF
331         self.logfile = logfile    
332         self.maxread = maxread # Max bytes to read at one time into buffer.
333         self.buffer = '' # This is the read buffer. See maxread.
334         self.searchwindowsize = searchwindowsize # Anything before searchwindowsize point is preserved, but not searched.
335         self.delaybeforesend = 0.1 # Sets sleep time used just before sending data to child.
336         self.delayafterclose = 0.1 # Sets delay in close() method to allow kernel time to update process status.
337         self.delayafterterminate = 0.1 # Sets delay in terminate() method to allow kernel time to update process status.
338         self.softspace = False # File-like object.
339         self.name = '<' + repr(self) + '>' # File-like object.
340         self.encoding = None # File-like object.
341         self.closed = True # File-like object.
342         self.env = env
343         self.__irix_hack = sys.platform.lower().find('irix') >= 0 # This flags if we are running on irix
344         self.use_native_pty_fork = not (sys.platform.lower().find('solaris') >= 0) # Solaris uses internal __fork_pty(). All other use pty.fork().
345         self.allstr = ""
346
347         # allow dummy instances for subclasses that may not use command or args.
348         if command is None:
349             self.command = None
350             self.args = None
351             self.name = '<pexpect factory incomplete>'
352             return
353
354         # If command is an int type then it may represent a file descriptor.
355         if type(command) == type(0):
356             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.')
357
358         if type (args) != type([]):
359             raise TypeError ('The argument, args, must be a list.')
360
361         if args == []:
362             self.args = split_command_line(command)
363             self.command = self.args[0]
364         else:
365             self.args = args[:] # work with a copy
366             self.args.insert (0, command)
367             self.command = command
368
369         command_with_path = which(self.command)
370         if command_with_path is None:
371             raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
372         self.command = command_with_path
373         self.args[0] = self.command
374
375         self.name = '<' + ' '.join (self.args) + '>'
376         self.__spawn()
377
378     def __del__(self):
379         """This makes sure that no system resources are left open.
380         Python only garbage collects Python objects. OS file descriptors
381         are not Python objects, so they must be handled explicitly.
382         If the child file descriptor was opened outside of this class
383         (passed to the constructor) then this does not close it.
384         """
385         if not self.closed:
386             self.close()
387
388     def __str__(self):
389         """This returns the current state of the pexpect object as a string.
390         """
391         s = []
392         s.append(repr(self))
393         s.append('version: ' + __version__ + ' (' + __revision__ + ')')
394         s.append('command: ' + str(self.command))
395         s.append('args: ' + str(self.args))
396         if self.patterns is None:
397             s.append('patterns: None')
398         else:
399             s.append('patterns:')
400             for p in self.patterns:
401                 if type(p) is type(re.compile('')):
402                     s.append('    ' + str(p.pattern))
403                 else:
404                     s.append('    ' + str(p))
405         s.append('buffer (last 100 chars): ' + str(self.buffer)[-100:])
406         s.append('before (last 100 chars): ' + str(self.before)[-100:])
407         s.append('after: ' + str(self.after))
408         s.append('match: ' + str(self.match))
409         s.append('match_index: ' + str(self.match_index))
410         s.append('exitstatus: ' + str(self.exitstatus))
411         s.append('flag_eof: ' + str(self.flag_eof))
412         s.append('pid: ' + str(self.pid))
413         s.append('child_fd: ' + str(self.child_fd))
414         s.append('closed: ' + str(self.closed))
415         s.append('timeout: ' + str(self.timeout))
416         s.append('delimiter: ' + str(self.delimiter))
417         s.append('logfile: ' + str(self.logfile))
418         s.append('maxread: ' + str(self.maxread))
419         s.append('ignorecase: ' + str(self.ignorecase))
420         s.append('searchwindowsize: ' + str(self.searchwindowsize))
421         s.append('delaybeforesend: ' + str(self.delaybeforesend))
422         s.append('delayafterclose: ' + str(self.delayafterclose))
423         s.append('delayafterterminate: ' + str(self.delayafterterminate))
424         return '\n'.join(s)
425
426     def __spawn(self):
427         """This starts the given command in a child process.
428         This does all the fork/exec type of stuff for a pty.
429         This is called by __init__. 
430         """
431         # The pid and child_fd of this object get set by this method.
432         # Note that it is difficult for this method to fail.
433         # You cannot detect if the child process cannot start.
434         # So the only way you can tell if the child process started
435         # or not is to try to read from the file descriptor. If you get
436         # EOF immediately then it means that the child is already dead.
437         # That may not necessarily be bad because you may haved spawned a child
438         # that performs some task; creates no stdout output; and then dies.
439
440         assert self.pid is None, 'The pid member should be None.'
441         assert self.command is not None, 'The command member should not be None.'
442
443         if self.use_native_pty_fork:
444             try:
445                 self.pid, self.child_fd = pty.fork()
446             except OSError, e:
447                 raise ExceptionPexpect('Error! pty.fork() failed: ' + str(e))
448         else: # Use internal __fork_pty
449             self.pid, self.child_fd = self.__fork_pty() 
450
451         if self.pid == 0: # Child
452             try: 
453                 self.child_fd = sys.stdout.fileno() # used by setwinsize()
454                 self.setwinsize(24, 80)
455             except: 
456                 # Some platforms do not like setwinsize (Cygwin).
457                 # This will cause problem when running applications that
458                 # are very picky about window size.
459                 # This is a serious limitation, but not a show stopper.
460                 pass
461             # Do not allow child to inherit open file descriptors from parent.
462             max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
463             for i in range (3, max_fd):
464                 try:
465                     os.close (i)
466                 except OSError:
467                     pass
468
469             # I don't know why this works, but ignoring SIGHUP fixes a
470             # problem when trying to start a Java daemon with sudo
471             # (specifically, Tomcat).
472             signal.signal(signal.SIGHUP, signal.SIG_IGN)
473
474             if self.env is None:
475                 os.execv(self.command, self.args)
476             else:
477                 os.execvpe(self.command, self.args, self.env)
478
479         # Parent
480         self.terminated = False
481         self.closed = False
482
483     def __fork_pty(self):
484         """This implements a substitute for the forkpty system call.
485         This should be more portable than the pty.fork() function.
486         Specifically, this should work on Solaris.
487         
488         Modified 10.06.05 by Geoff Marshall:
489             Implemented __fork_pty() method to resolve the issue with Python's 
490             pty.fork() not supporting Solaris, particularly ssh.
491         Based on patch to posixmodule.c authored by Noah Spurrier:
492             http://mail.python.org/pipermail/python-dev/2003-May/035281.html
493         """
494         parent_fd, child_fd = os.openpty()
495         if parent_fd < 0 or child_fd < 0:
496             raise ExceptionPexpect, "Error! Could not open pty with os.openpty()."
497         
498         pid = os.fork()
499         if pid < 0:
500             raise ExceptionPexpect, "Error! Failed os.fork()."
501         elif pid == 0:
502             # Child.
503             os.close(parent_fd)
504             self.__pty_make_controlling_tty(child_fd)
505             
506             os.dup2(child_fd, 0)
507             os.dup2(child_fd, 1)
508             os.dup2(child_fd, 2)
509             
510             if child_fd > 2:
511                 os.close(child_fd)
512         else:
513             # Parent.
514             os.close(child_fd)
515         
516         return pid, parent_fd
517                 
518     def __pty_make_controlling_tty(self, tty_fd):
519         """This makes the pseudo-terminal the controlling tty.
520         This should be more portable than the pty.fork() function.
521         Specifically, this should work on Solaris.
522         """
523         child_name = os.ttyname(tty_fd)
524         
525         # Disconnect from controlling tty if still connected.
526         fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY);
527         if fd >= 0:
528             os.close(fd)
529             
530         os.setsid()
531         
532         # Verify we are disconnected from controlling tty
533         try:
534             fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY);
535             if fd >= 0:
536                 os.close(fd)
537                 raise ExceptionPexpect, "Error! We are not disconnected from a controlling tty."
538         except:
539             # Good! We are disconnected from a controlling tty.
540             pass
541         
542         # Verify we can open child pty.
543         fd = os.open(child_name, os.O_RDWR);
544         if fd < 0:
545             raise ExceptionPexpect, "Error! Could not open child pty, " + child_name
546         else:
547             os.close(fd)
548
549         # Verify we now have a controlling tty.
550         fd = os.open("/dev/tty", os.O_WRONLY)
551         if fd < 0:
552             raise ExceptionPexpect, "Error! Could not open controlling tty, /dev/tty"
553         else:
554             os.close(fd)
555          
556     def fileno (self):   # File-like object.
557         """This returns the file descriptor of the pty for the child.
558         """
559         return self.child_fd
560
561     def close (self, force=True):   # File-like object.
562         """This closes the connection with the child application.
563         Note that calling close() more than once is valid.
564         This emulates standard Python behavior with files.
565         Set force to True if you want to make sure that the child is terminated
566         (SIGKILL is sent if the child ignores SIGHUP and SIGINT).
567         """
568         if not self.closed:
569             self.flush()
570             os.close (self.child_fd)
571             self.child_fd = -1
572             self.closed = True
573             time.sleep(self.delayafterclose) # Give kernel time to update process status.
574             if self.isalive():
575                 if not self.terminate(force):
576                     raise ExceptionPexpect ('close() could not terminate the child using terminate()')
577
578     def flush (self):   # File-like object.
579         """This does nothing. It is here to support the interface for a File-like object.
580         """
581         pass
582
583     def isatty (self):   # File-like object.
584         """This returns True if the file descriptor is open and connected to a tty(-like) device, else False.
585         """
586         return os.isatty(self.child_fd)
587
588     def setecho (self, state):
589         """This sets the terminal echo mode on or off.
590         Note that anything the child sent before the echo will be lost, so
591         you should be sure that your input buffer is empty before you setecho.
592         For example, the following will work as expected.
593             p = pexpect.spawn('cat')
594             p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).
595             p.expect (['1234'])
596             p.expect (['1234'])
597             p.setecho(False) # Turn off tty echo
598             p.sendline ('abcd') # We will set this only once (echoed by cat).
599             p.sendline ('wxyz') # We will set this only once (echoed by cat)
600             p.expect (['abcd'])
601             p.expect (['wxyz'])
602         The following WILL NOT WORK because the lines sent before the setecho
603         will be lost:
604             p = pexpect.spawn('cat')
605             p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).
606             p.setecho(False) # Turn off tty echo
607             p.sendline ('abcd') # We will set this only once (echoed by cat).
608             p.sendline ('wxyz') # We will set this only once (echoed by cat)
609             p.expect (['1234'])
610             p.expect (['1234'])
611             p.expect (['abcd'])
612             p.expect (['wxyz'])
613         """
614         self.child_fd
615         new = termios.tcgetattr(self.child_fd)
616         if state:
617             new[3] = new[3] | termios.ECHO
618         else:
619             new[3] = new[3] & ~termios.ECHO
620         # I tried TCSADRAIN and TCSAFLUSH, but these were inconsistent
621         # and blocked on some platforms. TCSADRAIN is probably ideal if it worked.
622         termios.tcsetattr(self.child_fd, termios.TCSANOW, new)
623     
624     def read_nonblocking (self, size = 1, timeout = -1):
625         """This reads at most size characters from the child application.
626         It includes a timeout. If the read does not complete within the
627         timeout period then a TIMEOUT exception is raised.
628         If the end of file is read then an EOF exception will be raised.
629         If a log file was set using setlog() then all data will
630         also be written to the log file.
631
632         If timeout==None then the read may block indefinitely.
633         If timeout==-1 then the self.timeout value is used.
634         If timeout==0 then the child is polled and 
635             if there was no data immediately ready then this will raise a TIMEOUT exception.
636         
637         The "timeout" refers only to the amount of time to read at least one character.
638         This is not effected by the 'size' parameter, so if you call
639         read_nonblocking(size=100, timeout=30) and only one character is
640         available right away then one character will be returned immediately. 
641         It will not wait for 30 seconds for another 99 characters to come in.
642         
643         This is a wrapper around os.read().
644         It uses select.select() to implement a timeout. 
645         """
646         if self.closed:
647             raise ValueError ('I/O operation on closed file in read_nonblocking().')
648
649         if timeout == -1:
650             timeout = self.timeout
651
652         # Note that some systems such as Solaris do not give an EOF when
653         # the child dies. In fact, you can still try to read
654         # from the child_fd -- it will block forever or until TIMEOUT.
655         # For this case, I test isalive() before doing any reading.
656         # If isalive() is false, then I pretend that this is the same as EOF.
657         if not self.isalive():
658             r,w,e = self.__select([self.child_fd], [], [], 0) # timeout of 0 means "poll"
659             if not r:
660                 self.flag_eof = True
661                 raise EOF ('End Of File (EOF) in read_nonblocking(). Braindead platform.')
662         elif self.__irix_hack:
663             # This is a hack for Irix. It seems that Irix requires a long delay before checking isalive.
664             # This adds a 2 second delay, but only when the child is terminated.
665             r, w, e = self.__select([self.child_fd], [], [], 2)
666             if not r and not self.isalive():
667                 self.flag_eof = True
668                 raise EOF ('End Of File (EOF) in read_nonblocking(). Pokey platform.')
669             
670         r,w,e = self.__select([self.child_fd], [], [], timeout)
671         
672         if not r:
673             if not self.isalive():
674                 # Some platforms, such as Irix, will claim that their processes are alive;
675                 # then timeout on the select; and then finally admit that they are not alive.
676                 self.flag_eof = True
677                 raise EOF ('End of File (EOF) in read_nonblocking(). Very pokey platform.')
678             else:
679                 raise TIMEOUT ('Timeout exceeded in read_nonblocking().')
680
681         if self.child_fd in r:
682             try:
683                 s = os.read(self.child_fd, size)
684             except OSError, e: # Linux does this
685                 self.flag_eof = True
686                 raise EOF ('End Of File (EOF) in read_nonblocking(). Exception style platform.')
687             if s == '': # BSD style
688                 self.flag_eof = True
689                 raise EOF ('End Of File (EOF) in read_nonblocking(). Empty string style platform.')
690
691             if self.logfile is not None:
692                 self.logfile.write (s)
693                 self.logfile.flush()
694
695             return s
696
697         raise ExceptionPexpect ('Reached an unexpected state in read_nonblocking().')
698
699     def read (self, size = -1):   # File-like object.
700         """This reads at most "size" bytes from the file 
701         (less if the read hits EOF before obtaining size bytes). 
702         If the size argument is negative or omitted, 
703         read all data until EOF is reached. 
704         The bytes are returned as a string object. 
705         An empty string is returned when EOF is encountered immediately.
706         """
707         if size == 0:
708             return ''
709         if size < 0:
710             self.expect (self.delimiter) # delimiter default is EOF
711             return self.before
712
713         # I could have done this more directly by not using expect(), but
714         # I deliberately decided to couple read() to expect() so that
715         # I would catch any bugs early and ensure consistant behavior.
716         # It's a little less efficient, but there is less for me to
717         # worry about if I have to later modify read() or expect().
718         # Note, it's OK if size==-1 in the regex. That just means it
719         # will never match anything in which case we stop only on EOF.
720         cre = re.compile('.{%d}' % size, re.DOTALL) 
721         index = self.expect ([cre, self.delimiter]) # delimiter default is EOF
722         if index == 0:
723             return self.after ### self.before should be ''. Should I assert this?
724         return self.before
725         
726     def readline (self, size = -1):    # File-like object.
727         """This reads and returns one entire line. A trailing newline is kept in
728         the string, but may be absent when a file ends with an incomplete line. 
729         Note: This readline() looks for a \\r\\n pair even on UNIX because
730         this is what the pseudo tty device returns. So contrary to what you
731         may expect you will receive the newline as \\r\\n.
732         An empty string is returned when EOF is hit immediately.
733         Currently, the size agument is mostly ignored, so this behavior is not
734         standard for a file-like object. If size is 0 then an empty string
735         is returned.
736         """
737         if size == 0:
738             return ''
739         index = self.expect (['\r\n', self.delimiter]) # delimiter default is EOF
740         if index == 0:
741             return self.before + '\r\n'
742         else:
743             return self.before
744
745     def __iter__ (self):    # File-like object.
746         """This is to support iterators over a file-like object.
747         """
748         return self
749
750     def next (self):    # File-like object.
751         """This is to support iterators over a file-like object.
752         """
753         result = self.readline()
754         if result == "":
755             raise StopIteration
756         return result
757
758     def readlines (self, sizehint = -1):    # File-like object.
759         """This reads until EOF using readline() and returns a list containing 
760         the lines thus read. The optional "sizehint" argument is ignored.
761         """        
762         lines = []
763         while True:
764             line = self.readline()
765             if not line:
766                 break
767             lines.append(line)
768         return lines
769
770     def write(self, str):   # File-like object.
771         """This is similar to send() except that there is no return value.
772         """
773         self.send (str)
774
775     def writelines (self, sequence):   # File-like object.
776         """This calls write() for each element in the sequence.
777         The sequence can be any iterable object producing strings, 
778         typically a list of strings. This does not add line separators
779         There is no return value.
780         """
781         for str in sequence:
782             self.write (str)
783
784     def send(self, str):
785         """This sends a string to the child process.
786         This returns the number of bytes written.
787         If a log file was set then the data is also written to the log.
788         """
789         time.sleep(self.delaybeforesend)
790         if self.logfile is not None:
791             self.logfile.write (str)
792             self.logfile.flush()
793         c = os.write(self.child_fd, str)
794         return c
795
796     def sendline(self, str=''):
797         """This is like send(), but it adds a line feed (os.linesep).
798         This returns the number of bytes written.
799         """
800         n = self.send(str)
801         n = n + self.send (os.linesep)
802         return n
803
804     def sendeof(self):
805         """This sends an EOF to the child.
806         This sends a character which causes the pending parent output
807         buffer to be sent to the waiting child program without
808         waiting for end-of-line. If it is the first character of the
809         line, the read() in the user program returns 0, which
810         signifies end-of-file. This means to work as expected 
811         a sendeof() has to be called at the begining of a line. 
812         This method does not send a newline. It is the responsibility
813         of the caller to ensure the eof is sent at the beginning of a line.
814         """
815         ### Hmmm... how do I send an EOF?
816         ###C  if ((m = write(pty, *buf, p - *buf)) < 0)
817         ###C      return (errno == EWOULDBLOCK) ? n : -1;
818         fd = sys.stdin.fileno()
819         old = termios.tcgetattr(fd) # remember current state
820         new = termios.tcgetattr(fd)
821         new[3] = new[3] | termios.ICANON # ICANON must be set to recognize EOF
822         try: # use try/finally to ensure state gets restored
823             termios.tcsetattr(fd, termios.TCSADRAIN, new)
824             if 'CEOF' in dir(termios):
825                 os.write (self.child_fd, '%c' % termios.CEOF)
826             else:
827                 os.write (self.child_fd, '%c' % 4) # Silly platform does not define CEOF so assume CTRL-D
828         finally: # restore state
829             termios.tcsetattr(fd, termios.TCSADRAIN, old)
830
831     def eof (self):
832         """This returns True if the EOF exception was ever raised.
833         """
834         return self.flag_eof
835
836     def terminate(self, force=False):
837         """This forces a child process to terminate.
838         It starts nicely with SIGHUP and SIGINT. If "force" is True then
839         moves onto SIGKILL.
840         This returns True if the child was terminated.
841         This returns False if the child could not be terminated.
842         """
843         if not self.isalive():
844             return True
845         self.kill(signal.SIGHUP)
846         time.sleep(self.delayafterterminate)
847         if not self.isalive():
848             return True
849         self.kill(signal.SIGCONT)
850         time.sleep(self.delayafterterminate)
851         if not self.isalive():
852             return True
853         self.kill(signal.SIGINT)
854         time.sleep(self.delayafterterminate)
855         if not self.isalive():
856             return True
857         if force:
858             self.kill(signal.SIGKILL)
859             time.sleep(self.delayafterterminate)
860             if not self.isalive():
861                 return True
862             else:
863                 return False
864         return False
865         #raise ExceptionPexpect ('terminate() could not terminate child process. Try terminate(force=True)?')
866      
867     def wait(self):
868         """This waits until the child exits. This is a blocking call.
869             This will not read any data from the child, so this will block forever
870             if the child has unread output and has terminated. In other words, the child
871             may have printed output then called exit(); but, technically, the child is
872             still alive until its output is read.
873         """
874         if self.isalive():
875             pid, status = os.waitpid(self.pid, 0)
876         else:
877             raise ExceptionPexpect ('Cannot wait for dead child process.')
878         self.exitstatus = os.WEXITSTATUS(status)
879         if os.WIFEXITED (status):
880             self.status = status
881             self.exitstatus = os.WEXITSTATUS(status)
882             self.signalstatus = None
883             self.terminated = True
884         elif os.WIFSIGNALED (status):
885             self.status = status
886             self.exitstatus = None
887             self.signalstatus = os.WTERMSIG(status)
888             self.terminated = True
889         elif os.WIFSTOPPED (status):
890             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?')
891         return self.exitstatus
892    
893     def isalive(self):
894         """This tests if the child process is running or not.
895         This is non-blocking. If the child was terminated then this
896         will read the exitstatus or signalstatus of the child.
897         This returns True if the child process appears to be running or False if not.
898         It can take literally SECONDS for Solaris to return the right status.
899         """
900         if self.terminated:
901             return False
902
903         if self.flag_eof:
904             # This is for Linux, which requires the blocking form of waitpid to get
905             # status of a defunct process. This is super-lame. The flag_eof would have
906             # been set in read_nonblocking(), so this should be safe.
907             waitpid_options = 0
908         else:
909             waitpid_options = os.WNOHANG
910             
911         try:
912             pid, status = os.waitpid(self.pid, waitpid_options)
913         except OSError, e: # No child processes
914             if e[0] == errno.ECHILD:
915                 raise ExceptionPexpect ('isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?')
916             else:
917                 raise e
918
919         # I have to do this twice for Solaris. I can't even believe that I figured this out...
920         # If waitpid() returns 0 it means that no child process wishes to
921         # report, and the value of status is undefined.
922         if pid == 0:
923             try:
924                 pid, status = os.waitpid(self.pid, waitpid_options) ### os.WNOHANG) # Solaris!
925             except OSError, e: # This should never happen...
926                 if e[0] == errno.ECHILD:
927                     raise ExceptionPexpect ('isalive() encountered condition that should never happen. There was no child process. Did someone else call waitpid() on our process?')
928                 else:
929                     raise e
930
931             # If pid is still 0 after two calls to waitpid() then
932             # the process really is alive. This seems to work on all platforms, except
933             # for Irix which seems to require a blocking call on waitpid or select, so I let read_nonblocking
934             # take care of this situation (unfortunately, this requires waiting through the timeout).
935             if pid == 0:
936                 return True
937
938         if pid == 0:
939             return True
940
941         if os.WIFEXITED (status):
942             self.status = status
943             self.exitstatus = os.WEXITSTATUS(status)
944             self.signalstatus = None
945             self.terminated = True
946         elif os.WIFSIGNALED (status):
947             self.status = status
948             self.exitstatus = None
949             self.signalstatus = os.WTERMSIG(status)
950             self.terminated = True
951         elif os.WIFSTOPPED (status):
952             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?')
953         return False
954
955     def kill(self, sig):
956         """This sends the given signal to the child application.
957         In keeping with UNIX tradition it has a misleading name.
958         It does not necessarily kill the child unless
959         you send the right signal.
960         """
961         # Same as os.kill, but the pid is given for you.
962         if self.isalive():
963             os.kill(self.pid, sig)
964
965     def compile_pattern_list(self, patterns):
966         """This compiles a pattern-string or a list of pattern-strings.
967         Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or 
968         a list of those. Patterns may also be None which results in
969         an empty list.
970
971         This is used by expect() when calling expect_list().
972         Thus expect() is nothing more than::
973              cpl = self.compile_pattern_list(pl)
974              return self.expect_list(clp, timeout)
975
976         If you are using expect() within a loop it may be more
977         efficient to compile the patterns first and then call expect_list().
978         This avoid calls in a loop to compile_pattern_list():
979              cpl = self.compile_pattern_list(my_pattern)
980              while some_condition:
981                 ...
982                 i = self.expect_list(clp, timeout)
983                 ...
984         """
985         if patterns is None:
986             return []
987         if type(patterns) is not types.ListType:
988             patterns = [patterns]
989
990         compile_flags = re.DOTALL # Allow dot to match \n
991         if self.ignorecase:
992             compile_flags = compile_flags | re.IGNORECASE
993         compiled_pattern_list = []
994         for p in patterns:
995             if type(p) is types.StringType:
996                 compiled_pattern_list.append(re.compile(p, compile_flags))
997             elif p is EOF:
998                 compiled_pattern_list.append(EOF)
999             elif p is TIMEOUT:
1000                 compiled_pattern_list.append(TIMEOUT)
1001             elif type(p) is type(re.compile('')):
1002                 compiled_pattern_list.append(p)
1003             else:
1004                 raise TypeError ('Argument must be one of StringType, EOF, TIMEOUT, SRE_Pattern, or a list of those type. %s' % str(type(p)))
1005
1006         return compiled_pattern_list
1007  
1008     def expect(self, pattern, timeout = -1, searchwindowsize=None):
1009
1010         """This seeks through the stream until a pattern is matched.
1011         The pattern is overloaded and may take several types including a list.
1012         The pattern can be a StringType, EOF, a compiled re, or a list of
1013         those types. Strings will be compiled to re types. This returns the
1014         index into the pattern list. If the pattern was not a list this
1015         returns index 0 on a successful match. This may raise exceptions for
1016         EOF or TIMEOUT. To avoid the EOF or TIMEOUT exceptions add
1017         EOF or TIMEOUT to the pattern list.
1018
1019         After a match is found the instance attributes
1020         'before', 'after' and 'match' will be set.
1021         You can see all the data read before the match in 'before'.
1022         You can see the data that was matched in 'after'.
1023         The re.MatchObject used in the re match will be in 'match'.
1024         If an error occured then 'before' will be set to all the
1025         data read so far and 'after' and 'match' will be None.
1026
1027         If timeout is -1 then timeout will be set to the self.timeout value.
1028
1029         Note: A list entry may be EOF or TIMEOUT instead of a string.
1030         This will catch these exceptions and return the index
1031         of the list entry instead of raising the exception.
1032         The attribute 'after' will be set to the exception type.
1033         The attribute 'match' will be None.
1034         This allows you to write code like this:
1035                 index = p.expect (['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])
1036                 if index == 0:
1037                     do_something()
1038                 elif index == 1:
1039                     do_something_else()
1040                 elif index == 2:
1041                     do_some_other_thing()
1042                 elif index == 3:
1043                     do_something_completely_different()
1044         instead of code like this:
1045                 try:
1046                     index = p.expect (['good', 'bad'])
1047                     if index == 0:
1048                         do_something()
1049                     elif index == 1:
1050                         do_something_else()
1051                 except EOF:
1052                     do_some_other_thing()
1053                 except TIMEOUT:
1054                     do_something_completely_different()
1055         These two forms are equivalent. It all depends on what you want.
1056         You can also just expect the EOF if you are waiting for all output
1057         of a child to finish. For example:
1058                 p = pexpect.spawn('/bin/ls')
1059                 p.expect (pexpect.EOF)
1060                 print p.before
1061
1062         If you are trying to optimize for speed then see expect_list().
1063         """
1064         compiled_pattern_list = self.compile_pattern_list(pattern)
1065         return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
1066
1067     def expect_list(self, pattern_list, timeout = -1, searchwindowsize = -1):
1068         """This takes a list of compiled regular expressions and returns 
1069         the index into the pattern_list that matched the child output.
1070         The list may also contain EOF or TIMEOUT (which are not
1071         compiled regular expressions). This method is similar to
1072         the expect() method except that expect_list() does not
1073         recompile the pattern list on every call.
1074         This may help if you are trying to optimize for speed, otherwise
1075         just use the expect() method.  This is called by expect().
1076         If timeout==-1 then the self.timeout value is used.
1077         If searchwindowsize==-1 then the self.searchwindowsize value is used.
1078         """
1079
1080         self.patterns = pattern_list
1081
1082         if timeout == -1:
1083             timeout = self.timeout
1084         if timeout is not None:
1085             end_time = time.time() + timeout 
1086         if searchwindowsize == -1:
1087             searchwindowsize = self.searchwindowsize
1088
1089         try:
1090             incoming = self.buffer
1091             while True: # Keep reading until exception or return.
1092                 # Sequence through the list of patterns looking for a match.
1093                 first_match = -1
1094                 for cre in pattern_list:
1095                     if cre is EOF or cre is TIMEOUT: 
1096                         continue # The patterns for PexpectExceptions are handled differently.
1097                     if searchwindowsize is None: # search everything
1098                         match = cre.search(incoming)
1099                     else:
1100                         startpos = max(0, len(incoming) - searchwindowsize)
1101                         match = cre.search(incoming, startpos)
1102                     if match is None:
1103                         continue
1104                     if first_match > match.start() or first_match == -1:
1105                         first_match = match.start()
1106                         self.match = match
1107                         self.match_index = pattern_list.index(cre)
1108                 if first_match > -1:
1109                     self.buffer = incoming[self.match.end() : ]
1110                     self.before = incoming[ : self.match.start()]
1111                     self.after = incoming[self.match.start() : self.match.end()]
1112                     #print "MATCH--", self.after, "--EOM"
1113                     return self.match_index
1114                 # No match at this point
1115                 if timeout < 0 and timeout is not None:
1116                     raise TIMEOUT ('Timeout exceeded in expect_list().')
1117                 # Still have time left, so read more data
1118                 c = self.read_nonblocking (self.maxread, timeout)
1119                 time.sleep (0.0001)
1120                 incoming = incoming + c
1121                 self.allstr += c
1122                 #print "INCOMING--", c, "--EOI"
1123                 if timeout is not None:
1124                     timeout = end_time - time.time()
1125         except EOF, e:
1126             self.buffer = ''
1127             self.before = incoming
1128             self.after = EOF
1129             if EOF in pattern_list:
1130                 self.match = EOF
1131                 self.match_index = pattern_list.index(EOF)
1132                 return self.match_index
1133             else:
1134                 self.match = None
1135                 self.match_index = None
1136                 raise EOF (str(e) + '\n' + str(self))
1137         except TIMEOUT, e:
1138             self.before = incoming
1139             self.after = TIMEOUT
1140             if TIMEOUT in pattern_list:
1141                 self.match = TIMEOUT
1142                 self.match_index = pattern_list.index(TIMEOUT)
1143                 return self.match_index
1144             else:
1145                 self.match = None
1146                 self.match_index = None
1147                 raise TIMEOUT (str(e) + '\n' + str(self))
1148         except Exception:
1149             self.before = incoming
1150             self.after = None
1151             self.match = None
1152             self.match_index = None
1153             raise
1154
1155     def getwinsize(self):
1156         """This returns the terminal window size of the child tty.
1157         The return value is a tuple of (rows, cols).
1158         """
1159         if 'TIOCGWINSZ' in dir(termios):
1160             TIOCGWINSZ = termios.TIOCGWINSZ
1161         else:
1162             TIOCGWINSZ = 1074295912L # assume if not defined
1163         s = struct.pack('HHHH', 0, 0, 0, 0)
1164         x = fcntl.ioctl(self.fileno(), TIOCGWINSZ, s)
1165         return struct.unpack('HHHH', x)[0:2]
1166
1167     def setwinsize(self, r, c):
1168         """This sets the terminal window size of the child tty.
1169         This will cause a SIGWINCH signal to be sent to the child.
1170         This does not change the physical window size.
1171         It changes the size reported to TTY-aware applications like
1172         vi or curses -- applications that respond to the SIGWINCH signal.
1173         """
1174         # Check for buggy platforms. Some Python versions on some platforms
1175         # (notably OSF1 Alpha and RedHat 7.1) truncate the value for
1176         # termios.TIOCSWINSZ. It is not clear why this happens.
1177         # These platforms don't seem to handle the signed int very well;
1178         # yet other platforms like OpenBSD have a large negative value for
1179         # TIOCSWINSZ and they don't have a truncate problem.
1180         # Newer versions of Linux have totally different values for TIOCSWINSZ.
1181         # Note that this fix is a hack.
1182         if 'TIOCSWINSZ' in dir(termios):
1183             TIOCSWINSZ = termios.TIOCSWINSZ
1184         else:
1185             TIOCSWINSZ = -2146929561
1186         if TIOCSWINSZ == 2148037735L: # L is not required in Python >= 2.2.
1187             TIOCSWINSZ = -2146929561 # Same bits, but with sign.
1188         # Note, assume ws_xpixel and ws_ypixel are zero.
1189         s = struct.pack('HHHH', r, c, 0, 0)
1190         fcntl.ioctl(self.fileno(), TIOCSWINSZ, s)
1191
1192     def interact(self, escape_character = chr(29), input_filter = None, output_filter = None):
1193         """This gives control of the child process to the interactive user
1194         (the human at the keyboard).
1195         Keystrokes are sent to the child process, and the stdout and stderr
1196         output of the child process is printed.
1197         This simply echos the child stdout and child stderr to the real
1198         stdout and it echos the real stdin to the child stdin.
1199         When the user types the escape_character this method will stop.
1200         The default for escape_character is ^]. This should not be confused
1201         with ASCII 27 -- the ESC character. ASCII 29 was chosen
1202         for historical merit because this is the character used
1203         by 'telnet' as the escape character. The escape_character will
1204         not be sent to the child process.
1205
1206         You may pass in optional input and output filter functions.
1207         These functions should take a string and return a string.
1208         The output_filter will be passed all the output from the child process.
1209         The input_filter will be passed all the keyboard input from the user.
1210         The input_filter is run BEFORE the check for the escape_character.
1211
1212         Note that if you change the window size of the parent
1213         the SIGWINCH signal will not be passed through to the child.
1214         If you want the child window size to change when the parent's
1215         window size changes then do something like the following example:
1216             import pexpect, struct, fcntl, termios, signal, sys
1217             def sigwinch_passthrough (sig, data):
1218                 s = struct.pack("HHHH", 0, 0, 0, 0)
1219                 a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ , s))
1220                 global p
1221                 p.setwinsize(a[0],a[1])
1222             p = pexpect.spawn('/bin/bash') # Note this is global and used in sigwinch_passthrough.
1223             signal.signal(signal.SIGWINCH, sigwinch_passthrough)
1224             p.interact()
1225         """
1226         # Flush the buffer.
1227         self.stdout.write (self.buffer)
1228         self.stdout.flush()
1229         self.buffer = ''
1230         mode = tty.tcgetattr(self.STDIN_FILENO)
1231         tty.setraw(self.STDIN_FILENO)
1232         try:
1233             self.__interact_copy(escape_character, input_filter, output_filter)
1234         finally:
1235             tty.tcsetattr(self.STDIN_FILENO, tty.TCSAFLUSH, mode)
1236
1237     def __interact_writen(self, fd, data):
1238         """This is used by the interact() method.
1239         """
1240         while data != '' and self.isalive():
1241             n = os.write(fd, data)
1242             data = data[n:]
1243     def __interact_read(self, fd):
1244         """This is used by the interact() method.
1245         """
1246         return os.read(fd, 1000)
1247     def __interact_copy(self, escape_character = None, input_filter = None, output_filter = None):
1248         """This is used by the interact() method.
1249         """
1250         while self.isalive():
1251             r,w,e = self.__select([self.child_fd, self.STDIN_FILENO], [], [])
1252             if self.child_fd in r:
1253                 data = self.__interact_read(self.child_fd)
1254                 if output_filter: data = output_filter(data)
1255                 if self.logfile is not None:
1256                     self.logfile.write (data)
1257                     self.logfile.flush()
1258                 os.write(self.STDOUT_FILENO, data)
1259             if self.STDIN_FILENO in r:
1260                 data = self.__interact_read(self.STDIN_FILENO)
1261                 if input_filter: data = input_filter(data)
1262                 i = data.rfind(escape_character)
1263                 if i != -1:
1264                     data = data[:i]
1265                     self.__interact_writen(self.child_fd, data)
1266                     break
1267                 self.__interact_writen(self.child_fd, data)
1268     def __select (self, iwtd, owtd, ewtd, timeout=None):
1269         """This is a wrapper around select.select() that ignores signals.
1270         If select.select raises a select.error exception and errno is an EINTR error then
1271         it is ignored. Mainly this is used to ignore sigwinch (terminal resize).
1272         """
1273         # if select() is interrupted by a signal (errno==EINTR) then
1274         # we loop back and enter the select() again.
1275         if timeout is not None:
1276             end_time = time.time() + timeout 
1277         while True:
1278             try:
1279                 return select.select (iwtd, owtd, ewtd, timeout)
1280             except select.error, e:
1281                 if e[0] == errno.EINTR:
1282                     # if we loop back we have to subtract the amount of time we already waited.
1283                     if timeout is not None:
1284                         timeout = end_time - time.time()
1285                         if timeout < 0:
1286                             return ([],[],[])
1287                 else: # something else caused the select.error, so this really is an exception
1288                     raise
1289
1290 ##############################################################################
1291 # The following methods are no longer supported or allowed..                
1292     def setmaxread (self, maxread):
1293         """This method is no longer supported or allowed.
1294         I don't like getters and setters without a good reason.
1295         """
1296         raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the maxread member variable.')
1297     def expect_exact (self, pattern_list, timeout = -1):
1298         """This method is no longer supported or allowed.
1299         It was too hard to maintain and keep it up to date with expect_list.
1300         Few people used this method. Most people favored reliability over speed.
1301         The implementation is left in comments in case anyone needs to hack this
1302         feature back into their copy.
1303         If someone wants to diff this with expect_list and make them work
1304         nearly the same then I will consider adding this make in.
1305         """
1306         raise ExceptionPexpect ('This method is no longer supported or allowed.')
1307     def setlog (self, fileobject):
1308         """This method is no longer supported or allowed.
1309         """
1310         raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the logfile member variable.')
1311
1312 ##############################################################################
1313 # End of spawn class
1314 ##############################################################################
1315
1316 def which (filename):
1317     """This takes a given filename; tries to find it in the environment path; 
1318     then checks if it is executable.
1319     This returns the full path to the filename if found and executable.
1320     Otherwise this returns None.
1321     """
1322     # Special case where filename already contains a path.
1323     if os.path.dirname(filename) != '':
1324         if os.access (filename, os.X_OK):
1325             return filename
1326
1327     if not os.environ.has_key('PATH') or os.environ['PATH'] == '':
1328         p = os.defpath
1329     else:
1330         p = os.environ['PATH']
1331
1332     # Oddly enough this was the one line that made Pexpect
1333     # incompatible with Python 1.5.2.
1334     #pathlist = p.split (os.pathsep) 
1335     pathlist = string.split (p, os.pathsep)
1336
1337     for path in pathlist:
1338         f = os.path.join(path, filename)
1339         if os.access(f, os.X_OK):
1340             return f
1341     return None
1342
1343 def split_command_line(command_line):
1344     """This splits a command line into a list of arguments.
1345     It splits arguments on spaces, but handles
1346     embedded quotes, doublequotes, and escaped characters.
1347     It's impossible to do this with a regular expression, so
1348     I wrote a little state machine to parse the command line.
1349     """
1350     arg_list = []
1351     arg = ''
1352
1353     # Constants to name the states we can be in.
1354     state_basic = 0
1355     state_esc = 1
1356     state_singlequote = 2
1357     state_doublequote = 3
1358     state_whitespace = 4 # The state of consuming whitespace between commands.
1359     state = state_basic
1360
1361     for c in command_line:
1362         if state == state_basic or state == state_whitespace:
1363             if c == '\\': # Escape the next character
1364                 state = state_esc
1365             elif c == r"'": # Handle single quote
1366                 state = state_singlequote
1367             elif c == r'"': # Handle double quote
1368                 state = state_doublequote
1369             elif c.isspace():
1370                 # Add arg to arg_list if we aren't in the middle of whitespace.
1371                 if state == state_whitespace:
1372                     None # Do nothing.
1373                 else:
1374                     arg_list.append(arg)
1375                     arg = ''
1376                     state = state_whitespace
1377             else:
1378                 arg = arg + c
1379                 state = state_basic
1380         elif state == state_esc:
1381             arg = arg + c
1382             state = state_basic
1383         elif state == state_singlequote:
1384             if c == r"'":
1385                 state = state_basic
1386             else:
1387                 arg = arg + c
1388         elif state == state_doublequote:
1389             if c == r'"':
1390                 state = state_basic
1391             else:
1392                 arg = arg + c
1393
1394     if arg != '':
1395         arg_list.append(arg)
1396     return arg_list
1397