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