~ubuntu-branches/ubuntu/trusty/pexpect/trusty-proposed

« back to all changes in this revision

Viewing changes to pexpect/fdpexpect.py

  • Committer: Package Import Robot
  • Author(s): Andrew Starr-Bochicchio, Thomas Kluyver, Jakub Wilk, Jackson Doak, Andrew Starr-Bochicchio
  • Date: 2013-12-06 20:20:26 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20131206202026-9k9oixbv7e8ke30q
Tags: 3.0-1
* Team upload.

[ Thomas Kluyver ]
* New upstream release. Closes: #729518
* Add packaging for Python 3.
* Use pybuild for packaging.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Jackson Doak ]
* Create debian/python3-pexpect.docs

[ Andrew Starr-Bochicchio ]
* Remove empty debian/patches dir.
* Move documentation and examples into a new python-pexpect-doc
  package. They are shared between the Python 2 and Python 3 packages,
  so there is no need to install them with both.

Show diffs side-by-side

added added

removed removed

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