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