~ubuntu-branches/ubuntu/precise/pexpect/precise

« back to all changes in this revision

Viewing changes to pexpect.py

  • Committer: Bazaar Package Importer
  • Author(s): Ganesan Rajagopal
  • Date: 2008-07-27 18:21:35 UTC
  • mfrom: (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20080727182135-3ncewehrk5ux7ph2
Tags: 2.3-1
* New upstream release.
* Fix lintian warnings:
      - Move debhelper to Build-Depends from Build-Depends-Indep.
      - Remove DH_COMPAT in debian/rules.
        - Added debian/pycompat.
        - Added #! line for examples/ssh_session.py.
* Updated standards version.

Show diffs side-by-side

added added

removed removed

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