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