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