stdio.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- test-case-name: twisted.conch.test.test_manhole -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Asynchronous local terminal input handling
  6. @author: Jp Calderone
  7. """
  8. import os, tty, sys, termios
  9. from twisted.internet import reactor, stdio, protocol, defer
  10. from twisted.python import failure, reflect, log
  11. from twisted.conch.insults.insults import ServerProtocol
  12. from twisted.conch.manhole import ColoredManhole
  13. class UnexpectedOutputError(Exception):
  14. pass
  15. class TerminalProcessProtocol(protocol.ProcessProtocol):
  16. def __init__(self, proto):
  17. self.proto = proto
  18. self.onConnection = defer.Deferred()
  19. def connectionMade(self):
  20. self.proto.makeConnection(self)
  21. self.onConnection.callback(None)
  22. self.onConnection = None
  23. def write(self, data):
  24. """
  25. Write to the terminal.
  26. @param data: Data to write.
  27. @type data: L{bytes}
  28. """
  29. self.transport.write(data)
  30. def outReceived(self, data):
  31. """
  32. Receive data from the terminal.
  33. @param data: Data received.
  34. @type data: L{bytes}
  35. """
  36. self.proto.dataReceived(data)
  37. def errReceived(self, data):
  38. """
  39. Report an error.
  40. @param data: Data to include in L{Failure}.
  41. @type data: L{bytes}
  42. """
  43. self.transport.loseConnection()
  44. if self.proto is not None:
  45. self.proto.connectionLost(failure.Failure(UnexpectedOutputError(data)))
  46. self.proto = None
  47. def childConnectionLost(self, childFD):
  48. if self.proto is not None:
  49. self.proto.childConnectionLost(childFD)
  50. def processEnded(self, reason):
  51. if self.proto is not None:
  52. self.proto.connectionLost(reason)
  53. self.proto = None
  54. class ConsoleManhole(ColoredManhole):
  55. """
  56. A manhole protocol specifically for use with L{stdio.StandardIO}.
  57. """
  58. def connectionLost(self, reason):
  59. """
  60. When the connection is lost, there is nothing more to do. Stop the
  61. reactor so that the process can exit.
  62. """
  63. reactor.stop()
  64. def runWithProtocol(klass):
  65. fd = sys.__stdin__.fileno()
  66. oldSettings = termios.tcgetattr(fd)
  67. tty.setraw(fd)
  68. try:
  69. stdio.StandardIO(ServerProtocol(klass))
  70. reactor.run()
  71. finally:
  72. termios.tcsetattr(fd, termios.TCSANOW, oldSettings)
  73. os.write(fd, b"\r\x1bc\r")
  74. def main(argv=None):
  75. log.startLogging(open('child.log', 'w'))
  76. if argv is None:
  77. argv = sys.argv[1:]
  78. if argv:
  79. klass = reflect.namedClass(argv[0])
  80. else:
  81. klass = ConsoleManhole
  82. runWithProtocol(klass)
  83. if __name__ == '__main__':
  84. main()