1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
|
"""
Pexpect is a Python module for spawning child applications;
controlling them; and responding to expected patterns in their output.
Pexpect can be used for automating interactive applications such as
ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts
for duplicating software package installations on different servers.
It can be used for automated software testing. Pexpect is in the spirit of
Don Libes' Expect, but Pexpect is pure Python. Other Expect-like
modules for Python require TCL and Expect or require C extensions to
be compiled. Pexpect does not use C, Expect, or TCL extensions. It
should work on any platform that supports the standard Python pty
module. The Pexpect interface focuses on ease of use so that simple
tasks are easy.
Pexpect is Open Source, free, and all that stuff.
License: Python Software Foundation License
http://www.opensource.org/licenses/PythonSoftFoundation.html
Noah Spurrier
$Revision$
$Date$
"""
import os, sys
import select
# import signal
import traceback
import re
import struct
import resource
from types import *
try:
import pty
import tty
import termios
import fcntl
except ImportError, e:
raise ImportError, str(e) + """
A critical module was not found. Probably this OS does not support it.
Currently pexpect is intended for UNIX operating systems (including OS-X)."""
__version__ = '0.96'
__revision__ = '$Revision$'
__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn',
'__version__', '__revision__']
# Exception classes used by this module.
class ExceptionPexpect(Exception):
"""Base class for all exceptions raised by this module."""
def __init__(self, value):
self.value = value
def __str__(self):
return `self.value`
class EOF(ExceptionPexpect):
"""Raised when EOF is read from a child."""
class TIMEOUT(ExceptionPexpect):
"""Raised when a read time exceeds the timeout."""
##class MAXBUFFER(ExceptionPexpect):
## """Raised when a scan buffer fills before matching an expected pattern."""
class spawn:
"""This is the main class interface for Pexpect. Use this class to
start and control child applications.
"""
def __init__(self, command, args=None, timeout=30):
"""This is the constructor. The command parameter is a string
that includes the path and any arguments to the command. For example:
p = pexpect.spawn ('/usr/bin/ftp')
p = pexpect.spawn ('/usr/bin/ssh some@host.com')
p = pexpect.spawn ('/bin/ls -latr /tmp')
You may also construct it with a list of arguments like so:
p = pexpect.spawn ('/usr/bin/ftp', [])
p = pexpect.spawn ('/usr/bin/ssh', ['some@host.com'])
p = pexpect.spawn ('/bin/ls', ['-latr', '/tmp'])
After this the child application will be created and
will be ready for action. See expect() and send()/sendline().
"""
self.STDIN_FILENO = sys.stdin.fileno()
self.STDOUT_FILENO = sys.stdout.fileno()
self.STDERR_FILENO = sys.stderr.fileno()
### IMPLEMENT THIS FEATURE!!!
#self.maxbuffersize = 10000
# anything before maxsearchsize point is preserved, but not searched.
#self.maxsearchsize = 1000
self.timeout = timeout
self.child_fd = -1 # initially closed
self.pid = None
self.log_file = None
self.before = None
self.after = None
self.match = None
self.softspace = 0 # File-like object.
self.name = '' # File-like object.
if args is None:
self.args = split_command_line(command)
self.command = self.args[0]
else:
args.insert (0, command)
self.args = args
self.command = command
self.name = reduce(lambda x, y: x+' '+y, self.args)
self.__spawn()
def __del__(self):
"""This makes sure that no system resources are left open.
Python only garbage collects Python objects. OS file descriptors
are not Python objects, so they must be handled manually.
"""
self.close()
def __spawn(self):
"""This starts the given command in a child process. This does
all the fork/exec type of stuff for a pty. This is called by
__init__. The args parameter is a list, command is a string.
"""
# The pid and child_fd of this object get set by this method.
# Note that it is difficult for this method to fail.
# You cannot detect if the child process cannot start.
# So the only way you can tell if the child process started
# or not is to try to read from the file descriptor. If you get
# EOF immediately then it means that the child is already dead.
# That may not necessarily be bad, because you may haved spawned a child
# that performs some task; creates no stdout output; and then dies.
# It is a fuzzy edge case. Any child process that you are likely to
# want to interact with Pexpect would probably not fall into this
# category.
# FYI, This essentially does a fork/exec operation.
assert self.pid == None, 'The pid member is not None.'
assert self.command != None, 'The command member is None.'
if which(self.command) == None:
raise ExceptionPexpect ('The command was not found or was not executable: %s.' % self.command)
try:
self.pid, self.child_fd = pty.fork()
except OSError, e:
raise ExceptionPexpect('Pexpect: pty.fork() failed: ' + str(e))
if self.pid == 0: # Child
try: # Some platforms do not like setwinsize (for example, Cygwin).
setwinsize(24, 80)
except:
pass
# Do not allow child to inherit open file descriptors from parent.
max_fd = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
for i in range (3, max_fd):
try:
os.close (i)
except OSError:
pass
os.execvp(self.command, self.args)
raise ExceptionPexpect ('Reached an unexpected state in __spawn().')
# Parent
def fileno (self): # File-like object.
"""This returns the file descriptor of the pty for the child."""
return self.child_fd
def close (self): # File-like object.
"""This is experimental.
This closes the file descriptor of the child application.
It makes no attempt to actually kill the child or wait for its status.
"""
if self.child_fd != -1:
self.flush()
os.close (self.child_fd)
self.child_fd = -1
def flush (self): # File-like object.
"""This currently does nothing.
"""
pass
def isatty (self): # File-like object.
"""This always returns 1.
"""
return 1
def setecho (self, on):
"""This sets the terminal echo-mode on or off."""
new = termios.tcgetattr(self.child_fd)
if on:
new[3] = new[3] | termios.ECHO # lflags
else:
new[3] = new[3] & ~termios.ECHO # lflags
termios.tcsetattr(self.child_fd, termios.TCSADRAIN, new)
def setlog (self, fileobject):
"""This sets logging output to go to the given fileobject.
Set fileobject to None to stop logging.
"""
self.log_file = fileobject
def compile_pattern_list(self, patterns):
"""This compiles a pattern-string or a list of pattern-strings.
Argument must be one of StringType, EOF, SRE_Pattern, or a list of those type.
This is used by expect() when calling expect_list().
Thus expect() is nothing more than::
cpl = self.compile_pattern_list(pl)
return self.expect_list(clp, timeout)
If you are using expect() within a loop it may be more
efficient to compile the patterns first and then call expect_list().
This avoid calls in a loop to compile_pattern_list():
cpl = self.compile_pattern_list(my_pattern)
while some_condition:
...
i = self.expect_list(clp, timeout)
...
"""
if type(patterns) is not ListType:
patterns = [patterns]
compiled_pattern_list = []
for p in patterns:
if type(p) is StringType:
compiled_pattern_list.append(re.compile(p, re.DOTALL))
elif p is EOF:
compiled_pattern_list.append(EOF)
elif type(p) is type(re.compile('')):
compiled_pattern_list.append(p)
else:
raise TypeError, 'Argument must be one of StringType, EOF, SRE_Pattern, or a list of those type. %s' % str(type(p))
return compiled_pattern_list
def expect(self, pattern, timeout = None):
"""This seeks through the stream until an expected pattern is matched.
The pattern is overloaded and may take several types including a list.
The pattern can be a StringType, EOF, a compiled re, or
a list of those types. Strings will be compiled to re types.
This returns the index into the pattern list. If the pattern was
not a list this returns index 0 on a successful match.
This may raise exceptions for EOF or TIMEOUT.
To avoid the EOF exception add EOF to the pattern list.
After a match is found the instance attributes
'before', 'after' and 'match' will be set.
You can see all the data read before the match in 'before'.
You can see the data that was matched in 'after'.
The re.MatchObject used in the re match will be in 'match'.
If an error occured then 'before' will be set to all the
data read so far and 'after' and 'match' will be None.
Note: A list entry may be EOF instead of a string.
This will catch EOF exceptions and return the index
of the EOF entry instead of raising the EOF exception.
The attributes 'after' and 'match' will be None.
This allows you to write code like this:
index = p.expect (['good', 'bad', pexpect.EOF])
if index == 0:
do_something()
elif index == 1:
do_something_else()
elif index == 2:
do_some_other_thing()
instead of code like this:
try:
index = p.expect (['good', 'bad'])
if index == 0:
do_something()
elif index == 1:
do_something_else()
except EOF:
do_some_other_thing()
These two forms are equivalent. It all depends on what you want.
You can also just expect the EOF if you are waiting for all output
of a child to finish. For example:
p = pexpect.spawn('/bin/ls')
p.expect (pexpect.EOF)
print p.before
"""
compiled_pattern_list = self.compile_pattern_list(pattern)
return self.expect_list(compiled_pattern_list, timeout)
def expect_exact (self, pattern_list, local_timeout = None):
"""This is similar to expect() except that it takes
list of regular strings instead of compiled regular expressions.
The idea is that this should be much faster. It could also be
useful when you don't want to have to worry about escaping
regular expression characters that you want to match.
You may also pass just a string without a list and the single
string will be converted to a list.
"""
### This is dumb. It shares most of the code with expect_list.
### The only different is the comparison method and that
### self.match is always None after calling this.
if type(pattern_list) is StringType:
pattern_list = [pattern_list]
try:
incoming = ''
while 1: # Keep reading until exception or return.
c = self.read_nonblocking (1, local_timeout)
incoming = incoming + c
# Sequence through the list of patterns and look for a match.
index = -1
for str_target in pattern_list:
index = index + 1
match_index = incoming.find (str_target)
if match_index >= 0:
self.before = incoming [ : match_index]
self.after = incoming [match_index : ]
self.match = None
return index
except EOF, e:
if EOF in pattern_list:
self.before = incoming
self.after = EOF
return pattern_list.index(EOF)
else:
raise
except Exception, e:
self.before = incoming
self.after = None
self.match = None
raise
def expect_list(self, pattern_list, local_timeout = None):
"""This is called by expect(). This takes a list of compiled
regular expressions. This returns the index into the pattern_list
that matched the child's output.
"""
if local_timeout is None:
local_timeout = self.timeout
try:
incoming = ''
while 1: # Keep reading until exception or return.
c = self.read_nonblocking (1, local_timeout)
incoming = incoming + c
# Sequence through the list of patterns and look for a match.
index = -1
for cre in pattern_list:
index = index + 1
if cre is EOF:
continue # The EOF pattern is not a regular expression.
match = cre.search(incoming)
if match is not None:
self.before = incoming[ : match.start()]
self.after = incoming[match.start() : ]
self.match = match
return index
except EOF, e:
if EOF in pattern_list:
self.before = incoming
self.after = EOF
return pattern_list.index(EOF)
else:
raise
except Exception, e:
self.before = incoming
self.after = None
self.match = None
raise
def read_nonblocking (self, size = -1, timeout = None):
"""This reads at most size characters from the child application.
It includes a timeout. If the read does not complete within the
timeout period then a TIMEOUT exception is raised.
If the end of file is read then an EOF exception will be raised.
If a log file was set using setlog() then all data will
also be written to the log file.
Notice that if this method is called with timeout=None
then it actually may block.
This is a non-blocking wrapper around os.read().
It uses select.select() to supply a timeout.
"""
if self.child_fd == -1:
raise ValueError ('I/O operation on closed file')
# Note that some systems like Solaris don't seem to ever give
# an EOF when the child dies. In fact, you can still try to read
# from the child_fd -- it will block forever or until TIMEOUT.
# For this case, I test isalive() before doing any reading.
# If isalive() is false, then I pretend that this is the same as EOF.
if not self.isalive():
r, w, e = select.select([self.child_fd], [], [], 0)
if not r:
self.flag_eof = 1
raise EOF ('End Of File (EOF) in read(). Braindead platform.')
r, w, e = select.select([self.child_fd], [], [], timeout)
if not r:
raise TIMEOUT('Timeout exceeded in read().')
if self.child_fd in r:
try:
s = os.read(self.child_fd, size)
except OSError, e:
self.flag_eof = 1
raise EOF('End Of File (EOF) in read(). Exception style platform.')
if s == '':
self.flag_eof = 1
raise EOF('End Of File (EOF) in read(). Empty string style platform.')
if self.log_file != None:
self.log_file.write (s)
self.log_file.flush()
return s
raise ExceptionPexpect('Reached an unexpected state in read().')
def read (self, size = -1):
"""This reads at most size bytes from the file
(less if the read hits EOF before obtaining size bytes).
If the size argument is negative or omitted,
read all data until EOF is reached.
The bytes are returned as a string object.
An empty string is returned when EOF is encountered immediately.
"""
if size == 0:
return ''
if size < 0:
self.expect (EOF)
return self.before
# I could make this more dicrect by no using expect(), but
# I deliberately decided to couple read() to expect() so that
# I would catch any bugs early and ensure consistant behavior.
# It's a little less efficient, but it makes less for me to
# worry about if I have to later modify read() or expect().
cre = re.compile('.{%d}' % size, re.DOTALL)
index = self.expect ([cre, EOF])
if index == 0:
return self.after ### self.before should be ''. Should I assert this?
return self.before
def readline (self, size = -1): # File-like object.
"""This reads and returns one entire line. A trailing newline is kept in
the string, but may be absent when a file ends with an incomplete line.
Note: This readline() looks for a \r\n pair even on UNIX because this is
what the pseudo tty device returns. So contrary to what you may be used to
you will get a newline as \r\n.
An empty string is returned when EOF is hit immediately.
"""
index = self.expect (['\r\n', EOF])
if index == 0:
return self.before + '\r\n'
else:
return self.before
def readlines (self, sizehint = -1): # File-like object.
"""This reads until EOF using readline() and returns a list containing
the lines thus read. THhe optional sizehint argument is ignored.
"""
lines = []
while 1:
line = self.readline()
if not line:
break
lines.append(line)
return lines
def write(self, str): # File-like object.
"""This is an alias for send() except that there is no return value.
"""
self.send (str)
def writelines (self, sequence): # File-like object.
"""This calls write() for each element in the sequence.
The sequence can be any iterable object producing strings,
typically a list of strings. This does not add line separators
There is no return value.
"""
for str in sequence:
self.write (str)
def send(self, str):
"""This sends a string to the child process.
This returns the number of bytes written.
"""
return os.write(self.child_fd, str)
def sendline(self, str=''):
"""This is like send(), but it adds a line separator.
This returns the number of bytes written.
"""
n = self.send(str)
n = n + self.send (os.linesep)
return n
def sendeof(self):
"""This sends an EOF to the child.
More precisely: this sends a character which causes the pending
child buffer to be sent to the waiting user program without
waiting for end-of-line. If it is the first character of the
line, the read() in the user program returns 0, which
signifies end-of-file.
This means: to make this work as expected a send_eof() has to be
called at the begining of a line. A newline character is not
send here to avoid problems.
"""
### Hmmm... how do I send an EOF?
###C if ((m = write(pty, *buf, p - *buf)) < 0)
###C return (errno == EWOULDBLOCK) ? n : -1;
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd) # remember current state
new = termios.tcgetattr(fd)
new[3] = new[3] | termios.ICANON # lflags
# use try/finally to ensure state gets restored
try:
# EOF is recognized when ICANON is set, thus ensure it is:
termios.tcsetattr(fd, termios.TCSADRAIN, new)
os.write (self.child_fd, '%c' % termios.CEOF)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old) # restore state
def isalive(self):
"""This tests if the child process is running or not.
This returns 1 if the child process appears to be running or 0 if not.
I can't figure out how to check if a process is alive on Solaris.
This is what is wrong. Apparently the call:
os.waitpid(self.pid, os.WNOHANG)
does not work. It always returns with (0,0) whether the process
is running or defunct. This works:
os.waitpid(self.pid, 0)
but it blocks if the process IS alive, so it's useless for me.
I don't want to use signals. Signals on UNIX suck and they
mess up Python pipes (setting SIGCHLD to SIGIGNORE).
"""
try:
pid, status = os.waitpid(self.pid, os.WNOHANG)
except OSError, e:
return 0
### This does not work on Solaris. Grrr!!!
# if pid == self.pid and status == 0:
if status == 0:
return 1
# if pid == 0 and status == 0: # This happens on Solaris...
# # This means (I think) that the Solaris process is "defunct"
# # and a second wait must be called without the WNOHANG.
# # This is dangerous because if I am wrong then this could block.
# pid, status = os.waitpid (self.pid, 0)
# I didn't OR this together because I want hooks for debugging.
if os.WIFEXITED (status):
return 0
elif os.WIFSTOPPED (status):
return 0
elif os.WIFSIGNALED (status):
return 0
else:
return 1
def kill(self, sig):
"""This sends the given signal to the child application.
In keeping with UNIX tradition it has a misleading name.
It does not necessarily kill the child unless
you send the right signal.
"""
# Same as os.kill, but the pid is given for you.
if self.isalive():
os.kill(self.pid, sig)
def interact(self, escape_character = chr(29)):
"""This gives control of the child process to the interactive user.
Keystrokes are sent to the child process, and the stdout and stderr
output of the child process is printed.
When the user types the escape_character this method will stop.
The default for escape_character is ^] (ASCII 29).
This simply echos the child stdout and child stderr to the real
stdout and it echos the real stdin to the child stdin.
"""
mode = tty.tcgetattr(self.STDIN_FILENO)
tty.setraw(self.STDIN_FILENO)
try:
self.__interact_copy(escape_character)
finally:
tty.tcsetattr(self.STDIN_FILENO, tty.TCSAFLUSH, mode)
def __interact_writen(self, fd, data):
"""This is used by the interact() method.
"""
while data != '' and self.isalive():
n = os.write(fd, data)
data = data[n:]
def __interact_read(self, fd):
"""This is used by the interact() method.
"""
return os.read(fd, 1000)
def __interact_copy(self, escape_character = None):
"""This is used by the interact() method.
"""
while self.isalive():
r, w, e = select.select([self.child_fd, self.STDIN_FILENO], [], [])
if self.child_fd in r:
data = self.__interact_read(self.child_fd)
os.write(self.STDOUT_FILENO, data)
if self.STDIN_FILENO in r:
data = self.__interact_read(self.STDIN_FILENO)
self.__interact_writen(self.child_fd, data)
if escape_character in data:
break
## def send_human(self, text, delay_min = 0, delay_max = 1):
## pass
## def spawn2(self, command, args):
## """return pid, fd_stdio, fd_stderr
## """
## pass
## def expect_ex(self, string_match, local_timeout = None):
## """This is like expect(), except that instead of regular expression patterns
## it matches on exact strings.
## """
## pass
## # Return (data_read)
def which (filename):
"""This takes a given filename and tries to find it in the
environment path and check if it is executable.
"""
# Special case where filename already contains a path.
if os.path.dirname(filename) != '':
if os.access (filename, os.X_OK):
return filename
if not os.environ.has_key('PATH') or os.environ['PATH'] == '':
p = os.defpath
else:
p = os.environ['PATH']
pathlist = p.split (os.pathsep)
for path in pathlist:
f = os.path.join(path, filename)
if os.access(f, os.X_OK):
return f
return None
def setwinsize(r, c):
"""This sets the windowsize of the tty for stdout.
This does not change the physical window size.
It changes the size reported to TTY-aware applications like
vi or curses. In other words, applications that respond to the
SIGWINCH signal.
This is used by __spawn to set the tty window size of the child.
"""
# Check for buggy platforms. Some Pythons on some platforms
# (notably OSF1 Alpha and RedHat 7.1) truncate the value for
# termios.TIOCSWINSZ. It is not clear why this happens.
# These platforms don't seem to handle the signed int very well;
# yet other platforms like OpenBSD have a large negative value for
# TIOCSWINSZ and they don't truncate.
# Newer versions of Linux have totally different values for TIOCSWINSZ.
# Note, this fix is a hack.
TIOCSWINSZ = termios.TIOCSWINSZ
if TIOCSWINSZ == 2148037735L: # L is not required in Python 2.2.
TIOCSWINSZ = -2146929561 # Same number in binary, but with sign.
# Assume ws_xpixel and ws_ypixel are zero.
s = struct.pack("HHHH", r, c, 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), TIOCSWINSZ, s)
def getwinsize():
s = struct.pack("HHHH", 0, 0, 0, 0)
x = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, s)
return struct.unpack("HHHH", x)
def split_command_line(command_line):
"""This splits a command line into a list of arguments.
It splits arguments on spaces, but handles
embedded quotes, doublequotes, and escaped characters.
I couldn't do this with a regular expression, so
I wrote a little state machine to parse the command line.
"""
arg_list = []
arg = ''
state_quote = 0
state_doublequote = 0
state_esc = 0
for c in command_line:
if c == '\\': # Escape the next character
state_esc = 1
elif c == r"'": # Handle single quote
if state_esc:
state_esc = 0
elif not state_quote:
state_quote = 1
else:
state_quote = 0
elif c == r'"': # Handle double quote
if state_esc:
state_esc = 0
elif not state_doublequote:
state_doublequote = 1
else:
state_doublequote = 0
# Add arg to arg_list unless in some other state.
elif c == ' 'and not state_quote and not state_doublequote and not state_esc:
arg_list.append(arg)
arg = ''
else:
arg = arg + c
if c != '\\'and state_esc: # escape mode lasts for one character.
state_esc = 0
# Handle last argument.
if arg != '':
arg_list.append(arg)
return arg_list
####################
#
# NOTES
#
####################
# Reason for double fork:
# http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC15
# Reason for ptys:
# http://www.erlenstar.demon.co.uk/unix/faq_4.html#SEC52
# Nonblocking on Win32?
# Reasearch this as a way to maybe make pipe work for Win32.
# http://groups.google.com/groups?q=setraw+tty&hl=en&selm=uvgpvisvk.fsf%40roundpoint.com&rnum=7
#
# if istty:
# if os.name=='posix':
# import tty
# tty.setraw(sys.stdin.fileno())
# elif os.name=='nt':
# import win32file, win32con
# hstdin = win32file._get_osfhandle(sys.stdin.fileno())
# modes = (win32file.GetConsoleMode(hstdin)
# & ~(win32con.ENABLE_LINE_INPUT
# |win32con.ENABLE_ECHO_INPUT))
# win32file.SetConsoleMode(hstdin, modes)
# Basic documentation:
# Explain use of lists of patterns and return index.
# Explain exceptions for non-handled special cases like EOF
# Advanced documentation:
# Explain how patterns can be associated with actions.
# Can I Do this without changing interface.
# Test bad fork
# Test ENOENT. In other words, no more TTY devices.
#GLOBAL_SIGCHLD_RECEIVED = 0
#def childdied (signum, frame):
# print 'Signal handler called with signal', signum
# frame.f_globals['pexpect'].GLOBAL_SIGCHLD_RECEIVED = 1
# print str(frame.f_globals['pexpect'].GLOBAL_SIGCHLD_RECEIVED)
# GLOBAL_SIGCHLD_RECEIVED = 1
### Add a greedy read -- like a readall() to keep reading until a
# timeout is returned. Will e.expect('') work? or e.expect(None)?
### Weird bug. If you read too fast after doing a sendline()
# Sometimes you will read the data back that you just sent even if
# the child did not echo the data. This is particularly a problem if
# you send a password.
# This was previously used to implement a look-ahead in reads.
# if the lookahead failed then Pexpect would "push-back" the data
# that was read. The idea was to allow read() to read blocks of data.
# What I do now is just read one character at a time and then try a
# match. This is not as efficient, but it works well enough for the
# output of most applications and it makes program logic much simpler.
##class PushbackReader:
## """This class is a wrapper around os.read. It adds the features of buffering
## to allow push-back of data and to provide a timeout on a read.
## """
## def __init__(self, file_descriptor):
## self.fd = file_descriptor
## self.buffer = ''
##
## def read(self, n, timeout = None):
## """This does a read restricted by a timeout and
## it includes any cached data from previous calls.
## This is a non-blocking wrapper around os.read.
## it uses select.select to supply a timeout.
## Note that if this is called with timeout=None (the default)
## then this actually MAY block.
## """
## # The read() call is a problem.
## # Some platforms return an empty string '' at EOF.
## # Whereas other platforms raise an Input/output exception.
##
## avail = len(self.buffer)
## if n > avail:
## result = self.buffer
## n = n-avail
## else:
## result = self.buffer[: n]
## self.buffer = self.buffer[n:]
##
## r, w, e = select.select([self.fd], [], [], timeout)
## if not r:
## self.flag_timeout = 1
## raise TIMEOUT('Read exceeded time: %d'%timeout)
##
## if self.fd in r:
## try:
## s = os.read(self.fd, n)
## except OSError, e:
## self.flag_eof = 1
## raise EOF('Read reached End Of File (EOF). Exception platform.')
## if s == '':
## self.flag_eof = 1
## raise EOF('Read reached End Of File (EOF). Empty string platform.')
## return s
##
## self.flag_error = 1
## raise ExceptionPexpect('PushbackReader.read() reached an unexpected state.'+
## ' There is a logic error in the Pexpect source code.')
##
## def pushback(self, data):
## self.buffer = piece+self.buffer
|