LevelDBDatabase::Compact compacts db (on disk) w/o requiring reopen.
[chromium-blink-merge.git] / third_party / pexpect / fdpexpect.py
blob248f6e2a2befe2d44e1f21836aeb581b5b8c4538
1 """This is like pexpect, but will work on any file descriptor that you pass it.
2 You are reponsible for opening and close the file descriptor.
4 PEXPECT LICENSE
6 This license is approved by the OSI and FSF as GPL-compatible.
7 http://opensource.org/licenses/isc-license.txt
9 Copyright (c) 2012, Noah Spurrier <noah@noah.org>
10 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
11 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
12 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
13 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 """
23 from pexpect import *
24 import os
26 __all__ = ['fdspawn']
28 class fdspawn (spawn):
30 """This is like pexpect.spawn but allows you to supply your own open file
31 descriptor. For example, you could use it to read through a file looking
32 for patterns, or to control a modem or serial device. """
34 def __init__ (self, fd, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None):
36 """This takes a file descriptor (an int) or an object that support the
37 fileno() method (returning an int). All Python file-like objects
38 support fileno(). """
40 ### TODO: Add better handling of trying to use fdspawn in place of spawn
41 ### TODO: (overload to allow fdspawn to also handle commands as spawn does.
43 if type(fd) != type(0) and hasattr(fd, 'fileno'):
44 fd = fd.fileno()
46 if type(fd) != type(0):
47 raise ExceptionPexpect ('The fd argument is not an int. If this is a command string then maybe you want to use pexpect.spawn.')
49 try: # make sure fd is a valid file descriptor
50 os.fstat(fd)
51 except OSError:
52 raise ExceptionPexpect, 'The fd argument is not a valid file descriptor.'
54 self.args = None
55 self.command = None
56 spawn.__init__(self, None, args, timeout, maxread, searchwindowsize, logfile)
57 self.child_fd = fd
58 self.own_fd = False
59 self.closed = False
60 self.name = '<file descriptor %d>' % fd
62 def __del__ (self):
64 return
66 def close (self):
68 if self.child_fd == -1:
69 return
70 if self.own_fd:
71 self.close (self)
72 else:
73 self.flush()
74 os.close(self.child_fd)
75 self.child_fd = -1
76 self.closed = True
78 def isalive (self):
80 """This checks if the file descriptor is still valid. If os.fstat()
81 does not raise an exception then we assume it is alive. """
83 if self.child_fd == -1:
84 return False
85 try:
86 os.fstat(self.child_fd)
87 return True
88 except:
89 return False
91 def terminate (self, force=False):
93 raise ExceptionPexpect ('This method is not valid for file descriptors.')
95 def kill (self, sig):
97 return