示例#1
0
class Link():
  def __init__(self, conn, chan, command, args):
    self.conn = conn
    self.chan = chan
    self.command = command
    self.args = args
    self.queue = []

  def start(self):
    self.pid, self.child = pty.fork()
    if self.pid == 0: #is child process?
      finalargs = self.args
      if self.args == []:
        finalargs = [""]
      os.execv(self.command, finalargs)
    else:
      self.intimer = RepeatTimer(1, self.syncin)
      self.outtimer = Timer(1, self.syncout)
      self.intimer.start()
      self.outtimer.start()

  def stop(self):
    self.intimer.cancel()
    self.outtimer.cancel()
    os.kill(self.pid, signal.SIGKILL)

  def syncin(self):
    if self.queue != []:
      for line in self.queue:
        os.write(self.child, line + "\r\n")
      self.queue = []

  def syncout(self):
    try:
      readstr = os.read(self.child, 1024)
      lines = itertools.groupby(readstr.splitlines())
      for line in lines:
        if line[0] != "":
          self.conn.privmsg(self.chan, "[%s] %s" % (self.command, line[0]))
      self.outtimer = Timer(2, self.syncout)
      self.outtimer.start()
    except OSError:
      self.conn.privmsg(self.chan, "[] %s terminated" % self.command)