fdpexpect.py 3.3 KB

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