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.
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().
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
24 child = pexpect.spawn('scp foo myname@host.example.com:.')
25 child.expect ('Password:')
26 child.sendline (mypassword)
28 This works even for commands that ask for passwords or other input outside of
29 the normal stdio streams.
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,
37 (Let me know if I forgot anyone.)
39 Free, open source, and all that good stuff.
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:
48 The above copyright notice and this permission notice shall be included in all
49 copies or substantial portions of the Software.
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
59 Pexpect Copyright (c) 2006 Noah Spurrier
60 http://pexpect.sourceforge.net/
63 $Date: 2007/01/11 20:51:46 $
82 except ImportError, e:
83 raise ImportError (str(e) + """
84 A critical module was not found. Probably this operating system does not support it.
85 Pexpect is intended for UNIX-like operating systems.""")
88 __revision__ = '$Revision: 1.2 $'
89 __all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'run', 'which', 'split_command_line',
90 '__version__', '__revision__']
92 # Exception classes used by this module.
93 class ExceptionPexpect(Exception):
94 """Base class for all exceptions raised by this module.
96 def __init__(self, value):
99 return str(self.value)
101 """This returns an abbreviated stack trace with lines that only concern the caller.
102 In other words, the stack trace inside the Pexpect module is not included.
104 tblist = traceback.extract_tb(sys.exc_info()[2])
105 tblist = filter(self.__filter_not_pexpect, tblist)
106 tblist = traceback.format_list(tblist)
107 return ''.join(tblist)
108 def __filter_not_pexpect(self, trace_list_item):
109 if trace_list_item[0].find('pexpect.py') == -1:
113 class EOF(ExceptionPexpect):
114 """Raised when EOF is read from a child.
116 class TIMEOUT(ExceptionPexpect):
117 """Raised when a read time exceeds the timeout.
120 def run (command, timeout=-1, withexitstatus=False, events=None, extra_args=None, logfile=None):
121 """This function runs the given command; waits for it to finish;
122 then returns all output as a string. STDERR is included in output.
123 If the full path to the command is not given then the path is searched.
125 Note that lines are terminated by CR/LF (\\r\\n) combination
126 even on UNIX-like systems because this is the standard for pseudo ttys.
127 If you set withexitstatus to true, then run will return a tuple of
128 (command_output, exitstatus). If withexitstatus is false then this
129 returns just command_output.
131 The run() function can often be used instead of creating a spawn instance.
132 For example, the following code uses spawn:
133 from pexpect import *
134 child = spawn('scp foo myname@host.example.com:.')
135 child.expect ('(?i)password')
136 child.sendline (mypassword)
137 The previous code can be replace with the following, which you may
138 or may not find simpler:
139 from pexpect import *
140 run ('scp foo myname@host.example.com:.', events={'(?i)password': mypassword})
143 Start the apache daemon on the local machine:
144 from pexpect import *
145 run ("/usr/local/apache/bin/apachectl start")
146 Check in a file using SVN:
147 from pexpect import *
148 run ("svn ci -m 'automatic commit' my_file.py")
149 Run a command and capture exit status:
150 from pexpect import *
151 (command_output, exitstatus) = run ('ls -l /bin', withexitstatus=1)
154 The following will run SSH and execute 'ls -l' on the remote machine.
155 The password 'secret' will be sent if the '(?i)password' pattern is ever seen.
156 run ("ssh username@machine.example.com 'ls -l'", events={'(?i)password':'secret\n'})
158 This will start mencoder to rip a video from DVD. This will also display
159 progress ticks every 5 seconds as it runs.
160 from pexpect import *
162 print d['event_count'],
163 run ("mencoder dvd://1 -o video.avi -oac copy -ovc copy", events={TIMEOUT:print_ticks}, timeout=5)
165 The 'events' argument should be a dictionary of patterns and responses.
166 Whenever one of the patterns is seen in the command out
167 run() will send the associated response string. Note that you should
168 put newlines in your string if Enter is necessary.
169 The responses may also contain callback functions.
170 Any callback is function that takes a dictionary as an argument.
171 The dictionary contains all the locals from the run() function, so
172 you can access the child spawn object or any other variable defined
173 in run() (event_count, child, and extra_args are the most useful).
174 A callback may return True to stop the current run process otherwise
175 run() continues until the next event.
176 A callback may also return a string which will be sent to the child.
177 'extra_args' is not used by directly run(). It provides a way to pass data to
178 a callback function through run() through the locals dictionary passed to a callback.
181 child = spawn(command, maxread=2000, logfile=logfile)
183 child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile)
184 if events is not None:
185 patterns = events.keys()
186 responses = events.values()
188 patterns=None # We assume that EOF or TIMEOUT will save us.
190 child_result_list = []
194 index = child.expect (patterns)
195 if type(child.after) is types.StringType:
196 child_result_list.append(child.before + child.after)
197 else: # child.after may have been a TIMEOUT or EOF, so don't cat those.
198 child_result_list.append(child.before)
199 if type(responses[index]) is types.StringType:
200 child.send(responses[index])
201 elif type(responses[index]) is types.FunctionType:
202 callback_result = responses[index](locals())
204 if type(callback_result) is types.StringType:
205 child.send(callback_result)
206 elif callback_result:
209 raise TypeError ('The callback must be a string or function type.')
210 event_count = event_count + 1
212 child_result_list.append(child.before)
215 child_result_list.append(child.before)
217 child_result = ''.join(child_result_list)
220 return (child_result, child.exitstatus)
224 class spawn (object):
225 """This is the main class interface for Pexpect.
226 Use this class to start and control child applications.
229 def __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None, env=None):
230 """This is the constructor. The command parameter may be a string
231 that includes a command and any arguments to the command. For example:
232 p = pexpect.spawn ('/usr/bin/ftp')
233 p = pexpect.spawn ('/usr/bin/ssh user@example.com')
234 p = pexpect.spawn ('ls -latr /tmp')
235 You may also construct it with a list of arguments like so:
236 p = pexpect.spawn ('/usr/bin/ftp', [])
237 p = pexpect.spawn ('/usr/bin/ssh', ['user@example.com'])
238 p = pexpect.spawn ('ls', ['-latr', '/tmp'])
239 After this the child application will be created and
240 will be ready to talk to. For normal use, see expect() and
241 send() and sendline().
243 The maxread attribute sets the read buffer size.
244 This is maximum number of bytes that Pexpect will try to read
245 from a TTY at one time.
246 Seeting the maxread size to 1 will turn off buffering.
247 Setting the maxread value higher may help performance in cases
248 where large amounts of output are read back from the child.
249 This feature is useful in conjunction with searchwindowsize.
251 The searchwindowsize attribute sets the how far back in
252 the incomming seach buffer Pexpect will search for pattern matches.
253 Every time Pexpect reads some data from the child it will append the data to
254 the incomming buffer. The default is to search from the beginning of the
255 imcomming buffer each time new data is read from the child.
256 But this is very inefficient if you are running a command that
257 generates a large amount of data where you want to match
258 The searchwindowsize does not effect the size of the incomming data buffer.
259 You will still have access to the full buffer after expect() returns.
261 The logfile member turns on or off logging.
262 All input and output will be copied to the given file object.
263 Set logfile to None to stop logging. This is the default.
264 Set logfile to sys.stdout to echo everything to standard output.
265 The logfile is flushed after each write.
267 child = pexpect.spawn('some_command')
268 fout = file('mylog.txt','w')
271 child = pexpect.spawn('some_command')
272 child.logfile = sys.stdout
274 The delaybeforesend helps overcome a weird behavior that many users were experiencing.
275 The typical problem was that a user would expect() a "Password:" prompt and
276 then immediately call sendline() to send the password. The user would then
277 see that their password was echoed back to them. Passwords don't
278 normally echo. The problem is caused by the fact that most applications
279 print out the "Password" prompt and then turn off stdin echo, but if you
280 send your password before the application turned off echo, then you get
281 your password echoed. Normally this wouldn't be a problem when interacting
282 with a human at a real heyboard. If you introduce a slight delay just before
283 writing then this seems to clear up the problem. This was such a common problem
284 for many users that I decided that the default pexpect behavior
285 should be to sleep just before writing to the child application.
286 1/10th of a second (100 ms) seems to be enough to clear up the problem.
287 You can set delaybeforesend to 0 to return to the old behavior.
289 Note that spawn is clever about finding commands on your path.
290 It uses the same logic that "which" uses to find executables.
292 If you wish to get the exit status of the child you must call
293 the close() method. The exit or signal status of the child will be
294 stored in self.exitstatus or self.signalstatus.
295 If the child exited normally then exitstatus will store the exit return code and
296 signalstatus will be None.
297 If the child was terminated abnormally with a signal then signalstatus will store
298 the signal value and exitstatus will be None.
299 If you need more detail you can also read the self.status member which stores
300 the status returned by os.waitpid. You can interpret this using
301 os.WIFEXITED/os.WEXITSTATUS or os.WIFSIGNALED/os.TERMSIG.
303 self.STDIN_FILENO = pty.STDIN_FILENO
304 self.STDOUT_FILENO = pty.STDOUT_FILENO
305 self.STDERR_FILENO = pty.STDERR_FILENO
306 self.stdin = sys.stdin
307 self.stdout = sys.stdout
308 self.stderr = sys.stderr
311 self.ignorecase = False
315 self.match_index = None
316 self.terminated = True
317 self.exitstatus = None
318 self.signalstatus = None
319 self.status = None # status returned by os.waitpid
320 self.flag_eof = False
322 self.child_fd = -1 # initially closed
323 self.timeout = timeout
325 self.logfile = logfile
326 self.maxread = maxread # Max bytes to read at one time into buffer.
327 self.buffer = '' # This is the read buffer. See maxread.
328 self.searchwindowsize = searchwindowsize # Anything before searchwindowsize point is preserved, but not searched.
329 self.delaybeforesend = 0.1 # Sets sleep time used just before sending data to child.
330 self.delayafterclose = 0.1 # Sets delay in close() method to allow kernel time to update process status.
331 self.delayafterterminate = 0.1 # Sets delay in terminate() method to allow kernel time to update process status.
332 self.softspace = False # File-like object.
333 self.name = '<' + repr(self) + '>' # File-like object.
334 self.encoding = None # File-like object.
335 self.closed = True # File-like object.
337 self.__irix_hack = sys.platform.lower().find('irix') >= 0 # This flags if we are running on irix
338 self.use_native_pty_fork = not (sys.platform.lower().find('solaris') >= 0) # Solaris uses internal __fork_pty(). All other use pty.fork().
340 # allow dummy instances for subclasses that may not use command or args.
344 self.name = '<pexpect factory incomplete>'
347 # If command is an int type then it may represent a file descriptor.
348 if type(command) == type(0):
349 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.')
351 if type (args) != type([]):
352 raise TypeError ('The argument, args, must be a list.')
355 self.args = split_command_line(command)
356 self.command = self.args[0]
358 self.args = args[:] # work with a copy
359 self.args.insert (0, command)
360 self.command = command
362 command_with_path = which(self.command)
363 if command_with_path is None:
364 raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
365 self.command = command_with_path
366 self.args[0] = self.command
368 self.name = '<' + ' '.join (self.args) + '>'
372 """This makes sure that no system resources are left open.
373 Python only garbage collects Python objects. OS file descriptors
374 are not Python objects, so they must be handled explicitly.
375 If the child file descriptor was opened outside of this class
376 (passed to the constructor) then this does not close it.
382 """This returns the current state of the pexpect object as a string.
386 s.append('version: ' + __version__ + ' (' + __revision__ + ')')
387 s.append('command: ' + str(self.command))
388 s.append('args: ' + str(self.args))
389 if self.patterns is None:
390 s.append('patterns: None')
392 s.append('patterns:')
393 for p in self.patterns:
394 if type(p) is type(re.compile('')):
395 s.append(' ' + str(p.pattern))
397 s.append(' ' + str(p))
398 s.append('buffer (last 100 chars): ' + str(self.buffer)[-100:])
399 s.append('before (last 100 chars): ' + str(self.before)[-100:])
400 s.append('after: ' + str(self.after))
401 s.append('match: ' + str(self.match))
402 s.append('match_index: ' + str(self.match_index))
403 s.append('exitstatus: ' + str(self.exitstatus))
404 s.append('flag_eof: ' + str(self.flag_eof))
405 s.append('pid: ' + str(self.pid))
406 s.append('child_fd: ' + str(self.child_fd))
407 s.append('closed: ' + str(self.closed))
408 s.append('timeout: ' + str(self.timeout))
409 s.append('delimiter: ' + str(self.delimiter))
410 s.append('logfile: ' + str(self.logfile))
411 s.append('maxread: ' + str(self.maxread))
412 s.append('ignorecase: ' + str(self.ignorecase))
413 s.append('searchwindowsize: ' + str(self.searchwindowsize))
414 s.append('delaybeforesend: ' + str(self.delaybeforesend))
415 s.append('delayafterclose: ' + str(self.delayafterclose))
416 s.append('delayafterterminate: ' + str(self.delayafterterminate))
420 """This starts the given command in a child process.
421 This does all the fork/exec type of stuff for a pty.
422 This is called by __init__.
424 # The pid and child_fd of this object get set by this method.
425 # Note that it is difficult for this method to fail.
426 # You cannot detect if the child process cannot start.
427 # So the only way you can tell if the child process started
428 # or not is to try to read from the file descriptor. If you get
429 # EOF immediately then it means that the child is already dead.
430 # That may not necessarily be bad because you may haved spawned a child
431 # that performs some task; creates no stdout output; and then dies.
433 assert self.pid is None, 'The pid member should be None.'
434 assert self.command is not None, 'The command member should not be None.'
436 if self.use_native_pty_fork:
438 self.pid, self.child_fd = pty.fork()
440 raise ExceptionPexpect('Error! pty.fork() failed: ' + str(e))
441 else: # Use internal __fork_pty
442 self.pid, self.child_fd = self.__fork_pty()
444 if self.pid == 0: # Child
446 self.child_fd = sys.stdout.fileno() # used by setwinsize()
447 self.setwinsize(24, 80)
449 # Some platforms do not like setwinsize (Cygwin).
450 # This will cause problem when running applications that
451 # are very picky about window size.
452 # This is a serious limitation, but not a show stopper.
454 # Do not allow child to inherit open file descriptors from parent.
455 max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
456 for i in range (3, max_fd):
462 # I don't know why this works, but ignoring SIGHUP fixes a
463 # problem when trying to start a Java daemon with sudo
464 # (specifically, Tomcat).
465 signal.signal(signal.SIGHUP, signal.SIG_IGN)
468 os.execv(self.command, self.args)
470 os.execvpe(self.command, self.args, self.env)
473 self.terminated = False
476 def __fork_pty(self):
477 """This implements a substitute for the forkpty system call.
478 This should be more portable than the pty.fork() function.
479 Specifically, this should work on Solaris.
481 Modified 10.06.05 by Geoff Marshall:
482 Implemented __fork_pty() method to resolve the issue with Python's
483 pty.fork() not supporting Solaris, particularly ssh.
484 Based on patch to posixmodule.c authored by Noah Spurrier:
485 http://mail.python.org/pipermail/python-dev/2003-May/035281.html
487 parent_fd, child_fd = os.openpty()
488 if parent_fd < 0 or child_fd < 0:
489 raise ExceptionPexpect, "Error! Could not open pty with os.openpty()."
493 raise ExceptionPexpect, "Error! Failed os.fork()."
497 self.__pty_make_controlling_tty(child_fd)
509 return pid, parent_fd
511 def __pty_make_controlling_tty(self, tty_fd):
512 """This makes the pseudo-terminal the controlling tty.
513 This should be more portable than the pty.fork() function.
514 Specifically, this should work on Solaris.
516 child_name = os.ttyname(tty_fd)
518 # Disconnect from controlling tty if still connected.
519 fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY);
525 # Verify we are disconnected from controlling tty
527 fd = os.open("/dev/tty", os.O_RDWR | os.O_NOCTTY);
530 raise ExceptionPexpect, "Error! We are not disconnected from a controlling tty."
532 # Good! We are disconnected from a controlling tty.
535 # Verify we can open child pty.
536 fd = os.open(child_name, os.O_RDWR);
538 raise ExceptionPexpect, "Error! Could not open child pty, " + child_name
542 # Verify we now have a controlling tty.
543 fd = os.open("/dev/tty", os.O_WRONLY)
545 raise ExceptionPexpect, "Error! Could not open controlling tty, /dev/tty"
549 def fileno (self): # File-like object.
550 """This returns the file descriptor of the pty for the child.
554 def close (self, force=True): # File-like object.
555 """This closes the connection with the child application.
556 Note that calling close() more than once is valid.
557 This emulates standard Python behavior with files.
558 Set force to True if you want to make sure that the child is terminated
559 (SIGKILL is sent if the child ignores SIGHUP and SIGINT).
563 os.close (self.child_fd)
566 time.sleep(self.delayafterclose) # Give kernel time to update process status.
568 if not self.terminate(force):
569 raise ExceptionPexpect ('close() could not terminate the child using terminate()')
571 def flush (self): # File-like object.
572 """This does nothing. It is here to support the interface for a File-like object.
576 def isatty (self): # File-like object.
577 """This returns True if the file descriptor is open and connected to a tty(-like) device, else False.
579 return os.isatty(self.child_fd)
581 def setecho (self, state):
582 """This sets the terminal echo mode on or off.
583 Note that anything the child sent before the echo will be lost, so
584 you should be sure that your input buffer is empty before you setecho.
585 For example, the following will work as expected.
586 p = pexpect.spawn('cat')
587 p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).
590 p.setecho(False) # Turn off tty echo
591 p.sendline ('abcd') # We will set this only once (echoed by cat).
592 p.sendline ('wxyz') # We will set this only once (echoed by cat)
595 The following WILL NOT WORK because the lines sent before the setecho
597 p = pexpect.spawn('cat')
598 p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).
599 p.setecho(False) # Turn off tty echo
600 p.sendline ('abcd') # We will set this only once (echoed by cat).
601 p.sendline ('wxyz') # We will set this only once (echoed by cat)
608 new = termios.tcgetattr(self.child_fd)
610 new[3] = new[3] | termios.ECHO
612 new[3] = new[3] & ~termios.ECHO
613 # I tried TCSADRAIN and TCSAFLUSH, but these were inconsistent
614 # and blocked on some platforms. TCSADRAIN is probably ideal if it worked.
615 termios.tcsetattr(self.child_fd, termios.TCSANOW, new)
617 def read_nonblocking (self, size = 1, timeout = -1):
618 """This reads at most size characters from the child application.
619 It includes a timeout. If the read does not complete within the
620 timeout period then a TIMEOUT exception is raised.
621 If the end of file is read then an EOF exception will be raised.
622 If a log file was set using setlog() then all data will
623 also be written to the log file.
625 If timeout==None then the read may block indefinitely.
626 If timeout==-1 then the self.timeout value is used.
627 If timeout==0 then the child is polled and
628 if there was no data immediately ready then this will raise a TIMEOUT exception.
630 The "timeout" refers only to the amount of time to read at least one character.
631 This is not effected by the 'size' parameter, so if you call
632 read_nonblocking(size=100, timeout=30) and only one character is
633 available right away then one character will be returned immediately.
634 It will not wait for 30 seconds for another 99 characters to come in.
636 This is a wrapper around os.read().
637 It uses select.select() to implement a timeout.
640 raise ValueError ('I/O operation on closed file in read_nonblocking().')
643 timeout = self.timeout
645 # Note that some systems such as Solaris do not give an EOF when
646 # the child dies. In fact, you can still try to read
647 # from the child_fd -- it will block forever or until TIMEOUT.
648 # For this case, I test isalive() before doing any reading.
649 # If isalive() is false, then I pretend that this is the same as EOF.
650 if not self.isalive():
651 r,w,e = self.__select([self.child_fd], [], [], 0) # timeout of 0 means "poll"
654 raise EOF ('End Of File (EOF) in read_nonblocking(). Braindead platform.')
655 elif self.__irix_hack:
656 # This is a hack for Irix. It seems that Irix requires a long delay before checking isalive.
657 # This adds a 2 second delay, but only when the child is terminated.
658 r, w, e = self.__select([self.child_fd], [], [], 2)
659 if not r and not self.isalive():
661 raise EOF ('End Of File (EOF) in read_nonblocking(). Pokey platform.')
663 r,w,e = self.__select([self.child_fd], [], [], timeout)
666 if not self.isalive():
667 # Some platforms, such as Irix, will claim that their processes are alive;
668 # then timeout on the select; and then finally admit that they are not alive.
670 raise EOF ('End of File (EOF) in read_nonblocking(). Very pokey platform.')
672 raise TIMEOUT ('Timeout exceeded in read_nonblocking().')
674 if self.child_fd in r:
676 s = os.read(self.child_fd, size)
677 except OSError, e: # Linux does this
679 raise EOF ('End Of File (EOF) in read_nonblocking(). Exception style platform.')
680 if s == '': # BSD style
682 raise EOF ('End Of File (EOF) in read_nonblocking(). Empty string style platform.')
684 if self.logfile is not None:
685 self.logfile.write (s)
690 raise ExceptionPexpect ('Reached an unexpected state in read_nonblocking().')
692 def read (self, size = -1): # File-like object.
693 """This reads at most "size" bytes from the file
694 (less if the read hits EOF before obtaining size bytes).
695 If the size argument is negative or omitted,
696 read all data until EOF is reached.
697 The bytes are returned as a string object.
698 An empty string is returned when EOF is encountered immediately.
703 self.expect (self.delimiter) # delimiter default is EOF
706 # I could have done this more directly by not using expect(), but
707 # I deliberately decided to couple read() to expect() so that
708 # I would catch any bugs early and ensure consistant behavior.
709 # It's a little less efficient, but there is less for me to
710 # worry about if I have to later modify read() or expect().
711 # Note, it's OK if size==-1 in the regex. That just means it
712 # will never match anything in which case we stop only on EOF.
713 cre = re.compile('.{%d}' % size, re.DOTALL)
714 index = self.expect ([cre, self.delimiter]) # delimiter default is EOF
716 return self.after ### self.before should be ''. Should I assert this?
719 def readline (self, size = -1): # File-like object.
720 """This reads and returns one entire line. A trailing newline is kept in
721 the string, but may be absent when a file ends with an incomplete line.
722 Note: This readline() looks for a \\r\\n pair even on UNIX because
723 this is what the pseudo tty device returns. So contrary to what you
724 may expect you will receive the newline as \\r\\n.
725 An empty string is returned when EOF is hit immediately.
726 Currently, the size agument is mostly ignored, so this behavior is not
727 standard for a file-like object. If size is 0 then an empty string
732 index = self.expect (['\r\n', self.delimiter]) # delimiter default is EOF
734 return self.before + '\r\n'
738 def __iter__ (self): # File-like object.
739 """This is to support iterators over a file-like object.
743 def next (self): # File-like object.
744 """This is to support iterators over a file-like object.
746 result = self.readline()
751 def readlines (self, sizehint = -1): # File-like object.
752 """This reads until EOF using readline() and returns a list containing
753 the lines thus read. The optional "sizehint" argument is ignored.
757 line = self.readline()
763 def write(self, str): # File-like object.
764 """This is similar to send() except that there is no return value.
768 def writelines (self, sequence): # File-like object.
769 """This calls write() for each element in the sequence.
770 The sequence can be any iterable object producing strings,
771 typically a list of strings. This does not add line separators
772 There is no return value.
778 """This sends a string to the child process.
779 This returns the number of bytes written.
780 If a log file was set then the data is also written to the log.
782 time.sleep(self.delaybeforesend)
783 if self.logfile is not None:
784 self.logfile.write (str)
786 c = os.write(self.child_fd, str)
789 def sendline(self, str=''):
790 """This is like send(), but it adds a line feed (os.linesep).
791 This returns the number of bytes written.
794 n = n + self.send (os.linesep)
798 """This sends an EOF to the child.
799 This sends a character which causes the pending parent output
800 buffer to be sent to the waiting child program without
801 waiting for end-of-line. If it is the first character of the
802 line, the read() in the user program returns 0, which
803 signifies end-of-file. This means to work as expected
804 a sendeof() has to be called at the begining of a line.
805 This method does not send a newline. It is the responsibility
806 of the caller to ensure the eof is sent at the beginning of a line.
808 fd = sys.stdin.fileno()
809 old = termios.tcgetattr(fd) # remember current state
810 new = termios.tcgetattr(fd)
811 new[3] = new[3] | termios.ICANON # ICANON must be set to recognize EOF
812 try: # use try/finally to ensure state gets restored
813 termios.tcsetattr(fd, termios.TCSADRAIN, new)
814 if 'CEOF' in dir(termios):
815 os.write (self.child_fd, '%c' % termios.CEOF)
817 os.write (self.child_fd, '%c' % 4) # Silly platform does not define CEOF so assume CTRL-D
818 finally: # restore state
819 termios.tcsetattr(fd, termios.TCSADRAIN, old)
822 """This returns True if the EOF exception was ever raised.
826 def terminate(self, force=False):
827 """This forces a child process to terminate.
828 It starts nicely with SIGHUP and SIGINT. If "force" is True then
830 This returns True if the child was terminated.
831 This returns False if the child could not be terminated.
833 if not self.isalive():
835 self.kill(signal.SIGHUP)
836 time.sleep(self.delayafterterminate)
837 if not self.isalive():
839 self.kill(signal.SIGCONT)
840 time.sleep(self.delayafterterminate)
841 if not self.isalive():
843 self.kill(signal.SIGINT)
844 time.sleep(self.delayafterterminate)
845 if not self.isalive():
848 self.kill(signal.SIGKILL)
849 time.sleep(self.delayafterterminate)
850 if not self.isalive():
855 #raise ExceptionPexpect ('terminate() could not terminate child process. Try terminate(force=True)?')
858 """This waits until the child exits. This is a blocking call.
859 This will not read any data from the child, so this will block forever
860 if the child has unread output and has terminated. In other words, the child
861 may have printed output then called exit(); but, technically, the child is
862 still alive until its output is read.
865 pid, status = os.waitpid(self.pid, 0)
867 raise ExceptionPexpect ('Cannot wait for dead child process.')
868 self.exitstatus = os.WEXITSTATUS(status)
869 if os.WIFEXITED (status):
871 self.exitstatus = os.WEXITSTATUS(status)
872 self.signalstatus = None
873 self.terminated = True
874 elif os.WIFSIGNALED (status):
876 self.exitstatus = None
877 self.signalstatus = os.WTERMSIG(status)
878 self.terminated = True
879 elif os.WIFSTOPPED (status):
880 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?')
881 return self.exitstatus
884 """This tests if the child process is running or not.
885 This is non-blocking. If the child was terminated then this
886 will read the exitstatus or signalstatus of the child.
887 This returns True if the child process appears to be running or False if not.
888 It can take literally SECONDS for Solaris to return the right status.
894 # This is for Linux, which requires the blocking form of waitpid to get
895 # status of a defunct process. This is super-lame. The flag_eof would have
896 # been set in read_nonblocking(), so this should be safe.
899 waitpid_options = os.WNOHANG
902 pid, status = os.waitpid(self.pid, waitpid_options)
903 except OSError, e: # No child processes
904 if e[0] == errno.ECHILD:
905 raise ExceptionPexpect ('isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?')
909 # I have to do this twice for Solaris. I can't even believe that I figured this out...
910 # If waitpid() returns 0 it means that no child process wishes to
911 # report, and the value of status is undefined.
914 pid, status = os.waitpid(self.pid, waitpid_options) ### os.WNOHANG) # Solaris!
915 except OSError, e: # This should never happen...
916 if e[0] == errno.ECHILD:
917 raise ExceptionPexpect ('isalive() encountered condition that should never happen. There was no child process. Did someone else call waitpid() on our process?')
921 # If pid is still 0 after two calls to waitpid() then
922 # the process really is alive. This seems to work on all platforms, except
923 # for Irix which seems to require a blocking call on waitpid or select, so I let read_nonblocking
924 # take care of this situation (unfortunately, this requires waiting through the timeout).
931 if os.WIFEXITED (status):
933 self.exitstatus = os.WEXITSTATUS(status)
934 self.signalstatus = None
935 self.terminated = True
936 elif os.WIFSIGNALED (status):
938 self.exitstatus = None
939 self.signalstatus = os.WTERMSIG(status)
940 self.terminated = True
941 elif os.WIFSTOPPED (status):
942 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?')
946 """This sends the given signal to the child application.
947 In keeping with UNIX tradition it has a misleading name.
948 It does not necessarily kill the child unless
949 you send the right signal.
951 # Same as os.kill, but the pid is given for you.
953 os.kill(self.pid, sig)
955 def compile_pattern_list(self, patterns):
956 """This compiles a pattern-string or a list of pattern-strings.
957 Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or
958 a list of those. Patterns may also be None which results in
961 This is used by expect() when calling expect_list().
962 Thus expect() is nothing more than::
963 cpl = self.compile_pattern_list(pl)
964 return self.expect_list(clp, timeout)
966 If you are using expect() within a loop it may be more
967 efficient to compile the patterns first and then call expect_list().
968 This avoid calls in a loop to compile_pattern_list():
969 cpl = self.compile_pattern_list(my_pattern)
970 while some_condition:
972 i = self.expect_list(clp, timeout)
977 if type(patterns) is not types.ListType:
978 patterns = [patterns]
980 compile_flags = re.DOTALL # Allow dot to match \n
982 compile_flags = compile_flags | re.IGNORECASE
983 compiled_pattern_list = []
985 if type(p) is types.StringType:
986 compiled_pattern_list.append(re.compile(p, compile_flags))
988 compiled_pattern_list.append(EOF)
990 compiled_pattern_list.append(TIMEOUT)
991 elif type(p) is type(re.compile('')):
992 compiled_pattern_list.append(p)
994 raise TypeError ('Argument must be one of StringType, EOF, TIMEOUT, SRE_Pattern, or a list of those type. %s' % str(type(p)))
996 return compiled_pattern_list
998 def expect(self, pattern, timeout = -1, searchwindowsize=None):
1000 """This seeks through the stream until a pattern is matched.
1001 The pattern is overloaded and may take several types including a list.
1002 The pattern can be a StringType, EOF, a compiled re, or a list of
1003 those types. Strings will be compiled to re types. This returns the
1004 index into the pattern list. If the pattern was not a list this
1005 returns index 0 on a successful match. This may raise exceptions for
1006 EOF or TIMEOUT. To avoid the EOF or TIMEOUT exceptions add
1007 EOF or TIMEOUT to the pattern list.
1009 After a match is found the instance attributes
1010 'before', 'after' and 'match' will be set.
1011 You can see all the data read before the match in 'before'.
1012 You can see the data that was matched in 'after'.
1013 The re.MatchObject used in the re match will be in 'match'.
1014 If an error occured then 'before' will be set to all the
1015 data read so far and 'after' and 'match' will be None.
1017 If timeout is -1 then timeout will be set to the self.timeout value.
1019 Note: A list entry may be EOF or TIMEOUT instead of a string.
1020 This will catch these exceptions and return the index
1021 of the list entry instead of raising the exception.
1022 The attribute 'after' will be set to the exception type.
1023 The attribute 'match' will be None.
1024 This allows you to write code like this:
1025 index = p.expect (['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])
1031 do_some_other_thing()
1033 do_something_completely_different()
1034 instead of code like this:
1036 index = p.expect (['good', 'bad'])
1042 do_some_other_thing()
1044 do_something_completely_different()
1045 These two forms are equivalent. It all depends on what you want.
1046 You can also just expect the EOF if you are waiting for all output
1047 of a child to finish. For example:
1048 p = pexpect.spawn('/bin/ls')
1049 p.expect (pexpect.EOF)
1052 If you are trying to optimize for speed then see expect_list().
1054 compiled_pattern_list = self.compile_pattern_list(pattern)
1055 return self.expect_list(compiled_pattern_list, timeout, searchwindowsize)
1057 def expect_list(self, pattern_list, timeout = -1, searchwindowsize = -1):
1058 """This takes a list of compiled regular expressions and returns
1059 the index into the pattern_list that matched the child output.
1060 The list may also contain EOF or TIMEOUT (which are not
1061 compiled regular expressions). This method is similar to
1062 the expect() method except that expect_list() does not
1063 recompile the pattern list on every call.
1064 This may help if you are trying to optimize for speed, otherwise
1065 just use the expect() method. This is called by expect().
1066 If timeout==-1 then the self.timeout value is used.
1067 If searchwindowsize==-1 then the self.searchwindowsize value is used.
1070 self.patterns = pattern_list
1073 timeout = self.timeout
1074 if timeout is not None:
1075 end_time = time.time() + timeout
1076 if searchwindowsize == -1:
1077 searchwindowsize = self.searchwindowsize
1080 incoming = self.buffer
1081 while True: # Keep reading until exception or return.
1082 # Sequence through the list of patterns looking for a match.
1084 for cre in pattern_list:
1085 if cre is EOF or cre is TIMEOUT:
1086 continue # The patterns for PexpectExceptions are handled differently.
1087 if searchwindowsize is None: # search everything
1088 match = cre.search(incoming)
1090 startpos = max(0, len(incoming) - searchwindowsize)
1091 match = cre.search(incoming, startpos)
1094 if first_match > match.start() or first_match == -1:
1095 first_match = match.start()
1097 self.match_index = pattern_list.index(cre)
1098 if first_match > -1:
1099 self.buffer = incoming[self.match.end() : ]
1100 self.before = incoming[ : self.match.start()]
1101 self.after = incoming[self.match.start() : self.match.end()]
1102 return self.match_index
1103 # No match at this point
1104 if timeout < 0 and timeout is not None:
1105 raise TIMEOUT ('Timeout exceeded in expect_list().')
1106 # Still have time left, so read more data
1107 c = self.read_nonblocking (self.maxread, timeout)
1109 incoming = incoming + c
1110 if timeout is not None:
1111 timeout = end_time - time.time()
1114 self.before = incoming
1116 if EOF in pattern_list:
1118 self.match_index = pattern_list.index(EOF)
1119 return self.match_index
1122 self.match_index = None
1123 raise EOF (str(e) + '\n' + str(self))
1125 self.before = incoming
1126 self.after = TIMEOUT
1127 if TIMEOUT in pattern_list:
1128 self.match = TIMEOUT
1129 self.match_index = pattern_list.index(TIMEOUT)
1130 return self.match_index
1133 self.match_index = None
1134 raise TIMEOUT (str(e) + '\n' + str(self))
1136 self.before = incoming
1139 self.match_index = None
1142 def getwinsize(self):
1143 """This returns the terminal window size of the child tty.
1144 The return value is a tuple of (rows, cols).
1146 if 'TIOCGWINSZ' in dir(termios):
1147 TIOCGWINSZ = termios.TIOCGWINSZ
1149 TIOCGWINSZ = 1074295912L # assume if not defined
1150 s = struct.pack('HHHH', 0, 0, 0, 0)
1151 x = fcntl.ioctl(self.fileno(), TIOCGWINSZ, s)
1152 return struct.unpack('HHHH', x)[0:2]
1154 def setwinsize(self, r, c):
1155 """This sets the terminal window size of the child tty.
1156 This will cause a SIGWINCH signal to be sent to the child.
1157 This does not change the physical window size.
1158 It changes the size reported to TTY-aware applications like
1159 vi or curses -- applications that respond to the SIGWINCH signal.
1161 # Check for buggy platforms. Some Python versions on some platforms
1162 # (notably OSF1 Alpha and RedHat 7.1) truncate the value for
1163 # termios.TIOCSWINSZ. It is not clear why this happens.
1164 # These platforms don't seem to handle the signed int very well;
1165 # yet other platforms like OpenBSD have a large negative value for
1166 # TIOCSWINSZ and they don't have a truncate problem.
1167 # Newer versions of Linux have totally different values for TIOCSWINSZ.
1168 # Note that this fix is a hack.
1169 if 'TIOCSWINSZ' in dir(termios):
1170 TIOCSWINSZ = termios.TIOCSWINSZ
1172 TIOCSWINSZ = -2146929561
1173 if TIOCSWINSZ == 2148037735L: # L is not required in Python >= 2.2.
1174 TIOCSWINSZ = -2146929561 # Same bits, but with sign.
1175 # Note, assume ws_xpixel and ws_ypixel are zero.
1176 s = struct.pack('HHHH', r, c, 0, 0)
1177 fcntl.ioctl(self.fileno(), TIOCSWINSZ, s)
1179 def interact(self, escape_character = chr(29), input_filter = None, output_filter = None):
1180 """This gives control of the child process to the interactive user
1181 (the human at the keyboard).
1182 Keystrokes are sent to the child process, and the stdout and stderr
1183 output of the child process is printed.
1184 This simply echos the child stdout and child stderr to the real
1185 stdout and it echos the real stdin to the child stdin.
1186 When the user types the escape_character this method will stop.
1187 The default for escape_character is ^]. This should not be confused
1188 with ASCII 27 -- the ESC character. ASCII 29 was chosen
1189 for historical merit because this is the character used
1190 by 'telnet' as the escape character. The escape_character will
1191 not be sent to the child process.
1193 You may pass in optional input and output filter functions.
1194 These functions should take a string and return a string.
1195 The output_filter will be passed all the output from the child process.
1196 The input_filter will be passed all the keyboard input from the user.
1197 The input_filter is run BEFORE the check for the escape_character.
1199 Note that if you change the window size of the parent
1200 the SIGWINCH signal will not be passed through to the child.
1201 If you want the child window size to change when the parent's
1202 window size changes then do something like the following example:
1203 import pexpect, struct, fcntl, termios, signal, sys
1204 def sigwinch_passthrough (sig, data):
1205 s = struct.pack("HHHH", 0, 0, 0, 0)
1206 a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ , s))
1208 p.setwinsize(a[0],a[1])
1209 p = pexpect.spawn('/bin/bash') # Note this is global and used in sigwinch_passthrough.
1210 signal.signal(signal.SIGWINCH, sigwinch_passthrough)
1214 self.stdout.write (self.buffer)
1217 mode = tty.tcgetattr(self.STDIN_FILENO)
1218 tty.setraw(self.STDIN_FILENO)
1220 self.__interact_copy(escape_character, input_filter, output_filter)
1222 tty.tcsetattr(self.STDIN_FILENO, tty.TCSAFLUSH, mode)
1224 def __interact_writen(self, fd, data):
1225 """This is used by the interact() method.
1227 while data != '' and self.isalive():
1228 n = os.write(fd, data)
1230 def __interact_read(self, fd):
1231 """This is used by the interact() method.
1233 return os.read(fd, 1000)
1234 def __interact_copy(self, escape_character = None, input_filter = None, output_filter = None):
1235 """This is used by the interact() method.
1237 while self.isalive():
1238 r,w,e = self.__select([self.child_fd, self.STDIN_FILENO], [], [])
1239 if self.child_fd in r:
1240 data = self.__interact_read(self.child_fd)
1241 if output_filter: data = output_filter(data)
1242 if self.logfile is not None:
1243 self.logfile.write (data)
1244 self.logfile.flush()
1245 os.write(self.STDOUT_FILENO, data)
1246 if self.STDIN_FILENO in r:
1247 data = self.__interact_read(self.STDIN_FILENO)
1248 if input_filter: data = input_filter(data)
1249 i = data.rfind(escape_character)
1252 self.__interact_writen(self.child_fd, data)
1254 self.__interact_writen(self.child_fd, data)
1255 def __select (self, iwtd, owtd, ewtd, timeout=None):
1256 """This is a wrapper around select.select() that ignores signals.
1257 If select.select raises a select.error exception and errno is an EINTR error then
1258 it is ignored. Mainly this is used to ignore sigwinch (terminal resize).
1260 # if select() is interrupted by a signal (errno==EINTR) then
1261 # we loop back and enter the select() again.
1262 if timeout is not None:
1263 end_time = time.time() + timeout
1266 return select.select (iwtd, owtd, ewtd, timeout)
1267 except select.error, e:
1268 if e[0] == errno.EINTR:
1269 # if we loop back we have to subtract the amount of time we already waited.
1270 if timeout is not None:
1271 timeout = end_time - time.time()
1274 else: # something else caused the select.error, so this really is an exception
1277 ##############################################################################
1278 # The following methods are no longer supported or allowed..
1279 def setmaxread (self, maxread):
1280 """This method is no longer supported or allowed.
1281 I don't like getters and setters without a good reason.
1283 raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the maxread member variable.')
1284 def expect_exact (self, pattern_list, timeout = -1):
1285 """This method is no longer supported or allowed.
1286 It was too hard to maintain and keep it up to date with expect_list.
1287 Few people used this method. Most people favored reliability over speed.
1288 The implementation is left in comments in case anyone needs to hack this
1289 feature back into their copy.
1290 If someone wants to diff this with expect_list and make them work
1291 nearly the same then I will consider adding this make in.
1293 raise ExceptionPexpect ('This method is no longer supported or allowed.')
1294 def setlog (self, fileobject):
1295 """This method is no longer supported or allowed.
1297 raise ExceptionPexpect ('This method is no longer supported or allowed. Just assign a value to the logfile member variable.')
1299 ##############################################################################
1300 # End of spawn class
1301 ##############################################################################
1303 def which (filename):
1304 """This takes a given filename; tries to find it in the environment path;
1305 then checks if it is executable.
1306 This returns the full path to the filename if found and executable.
1307 Otherwise this returns None.
1309 # Special case where filename already contains a path.
1310 if os.path.dirname(filename) != '':
1311 if os.access (filename, os.X_OK):
1314 if not os.environ.has_key('PATH') or os.environ['PATH'] == '':
1317 p = os.environ['PATH']
1319 # Oddly enough this was the one line that made Pexpect
1320 # incompatible with Python 1.5.2.
1321 #pathlist = p.split (os.pathsep)
1322 pathlist = string.split (p, os.pathsep)
1324 for path in pathlist:
1325 f = os.path.join(path, filename)
1326 if os.access(f, os.X_OK):
1330 def split_command_line(command_line):
1331 """This splits a command line into a list of arguments.
1332 It splits arguments on spaces, but handles
1333 embedded quotes, doublequotes, and escaped characters.
1334 It's impossible to do this with a regular expression, so
1335 I wrote a little state machine to parse the command line.
1340 # Constants to name the states we can be in.
1343 state_singlequote = 2
1344 state_doublequote = 3
1345 state_whitespace = 4 # The state of consuming whitespace between commands.
1348 for c in command_line:
1349 if state == state_basic or state == state_whitespace:
1350 if c == '\\': # Escape the next character
1352 elif c == r"'": # Handle single quote
1353 state = state_singlequote
1354 elif c == r'"': # Handle double quote
1355 state = state_doublequote
1357 # Add arg to arg_list if we aren't in the middle of whitespace.
1358 if state == state_whitespace:
1361 arg_list.append(arg)
1363 state = state_whitespace
1367 elif state == state_esc:
1370 elif state == state_singlequote:
1375 elif state == state_doublequote:
1382 arg_list.append(arg)