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