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