Пример #1
0
    def start_stdio(self):
        fl_in = AsyncDataStream(
            self.ed, os.fdopen(sys.stdin.fileno(), 'rb', 0, closefd=False))
        fl_out = AsyncDataStream(self.ed,
                                 os.fdopen(sys.stdout.fileno(),
                                           'wb',
                                           0,
                                           closefd=False),
                                 read_r=False)

        sd = ed_shutdown(self.ed)
        fl_in.process_close = sd
        fl_out.process_close = sd

        self.stream = EventStreamServer(fl_in, fl_out)
        self.stream.watch_files = self._watch_files
        self._start_watch()
Пример #2
0
  def start_stdio(self):
    fl_in = AsyncDataStream(self.ed, os.fdopen(sys.stdin.fileno(), 'rb', 0, closefd=False))
    fl_out = AsyncDataStream(self.ed, os.fdopen(sys.stdout.fileno(), 'wb', 0, closefd=False), read_r=False)

    sd = ed_shutdown(self.ed)
    fl_in.process_close = sd
    fl_out.process_close = sd

    self.stream = EventStreamServer(fl_in, fl_out)
    self.stream.watch_files = self._watch_files
    self._start_watch()
Пример #3
0
class FileGazer:
    def __init__(self, ed, *args, **kwargs):
        self.ed = ed
        self.stream = None
        self.wd2pn = []
        self.fp2sz = {}

    def _start_watch(self):
        from gonium.linux import inotify

        self.iw = inotify.InotifyWatch(self.ed)
        self.iw.process_event = self._process_inotify_event

    def _process_inotify_event(self, wd, mask, cookie, name):
        from os import stat

        pn = self.wd2pn[wd]
        # TODO: Match line patterns
        sz = stat(pn).st_size
        sz_prev = self.fp2sz.get(pn)

        if (sz != sz_prev):
            self.fp2sz[pn] = sz
            lines = None

            def get_lines():
                nonlocal lines
                if (lines is None):
                    f = open(pn, 'rb')
                    f.seek(sz_prev)
                    data = f.read()
                    f.close()
                    lines = data.split(b'\n')
                    if (lines and lines[-1] == b''):
                        del (lines[-1])
                #sys.stderr.write('L: {!r}\n'.format(lines)); sys.stderr.flush()
                return lines

            self.stream.notify(pn, get_lines)

    def start_stdio(self):
        fl_in = AsyncDataStream(
            self.ed, os.fdopen(sys.stdin.fileno(), 'rb', 0, closefd=False))
        fl_out = AsyncDataStream(self.ed,
                                 os.fdopen(sys.stdout.fileno(),
                                           'wb',
                                           0,
                                           closefd=False),
                                 read_r=False)

        sd = ed_shutdown(self.ed)
        fl_in.process_close = sd
        fl_out.process_close = sd

        self.stream = EventStreamServer(fl_in, fl_out)
        self.stream.watch_files = self._watch_files
        self._start_watch()

    def update_file_size(self, path):
        from os import stat
        sz_prev = self.fp2sz.get(path)
        self.fp2sz[path] = stat(path).st_size
        return sz_prev

    def scan_dir(self, path):
        from os import walk
        from os.path import join

        for (p, dns, fns) in walk(path):
            for fn in fns:
                fp = join(p, fn)
                if fp.startswith(b'./'):
                    fp = fp[2:]
                self.stream.add_file(fp)
                self.update_file_size(fp)

    def watch_all(self):
        self._watch_files(self.stream.get_watched_files())

    def _add_watch_descriptor(self, wd, pn):
        off = wd - len(self.wd2pn) + 1
        if (off > 0):
            from itertools import repeat
            self.wd2pn.extend(repeat(None, off))

        self.wd2pn[wd] = pn

    def _watch_files(self, pns):
        from gonium.linux.inotify import IN_MODIFY

        for pn in pns:
            wd = self.iw.add_watch(pn, IN_MODIFY)
            self._add_watch_descriptor(wd, pn)
Пример #4
0
class FileGazer:
  def __init__(self, ed, *args, **kwargs):
    self.ed = ed
    self.stream = None
    self.wd2pn = []
    self.fp2sz = {}

  def _start_watch(self):
    from gonium.linux import inotify

    self.iw = inotify.InotifyWatch(self.ed)
    self.iw.process_event = self._process_inotify_event

  def _process_inotify_event(self, wd, mask, cookie, name):
    from os import stat

    pn = self.wd2pn[wd]
    # TODO: Match line patterns
    sz = stat(pn).st_size
    sz_prev = self.fp2sz.get(pn)

    if (sz != sz_prev):
      self.fp2sz[pn] = sz
      lines = None

      def get_lines():
        nonlocal lines
        if (lines is None):
          f = open(pn, 'rb')
          f.seek(sz_prev)
          data = f.read()
          f.close()
          lines = data.split(b'\n')
          if (lines and lines[-1] == b''):
            del(lines[-1])
        #sys.stderr.write('L: {!r}\n'.format(lines)); sys.stderr.flush()
        return lines

      self.stream.notify(pn, get_lines)

  def start_stdio(self):
    fl_in = AsyncDataStream(self.ed, os.fdopen(sys.stdin.fileno(), 'rb', 0, closefd=False))
    fl_out = AsyncDataStream(self.ed, os.fdopen(sys.stdout.fileno(), 'wb', 0, closefd=False), read_r=False)

    sd = ed_shutdown(self.ed)
    fl_in.process_close = sd
    fl_out.process_close = sd

    self.stream = EventStreamServer(fl_in, fl_out)
    self.stream.watch_files = self._watch_files
    self._start_watch()

  def update_file_size(self, path):
    from os import stat
    sz_prev = self.fp2sz.get(path)
    self.fp2sz[path] = stat(path).st_size
    return sz_prev

  def scan_dir(self, path):
    from os import walk
    from os.path import join

    for (p, dns, fns) in walk(path):
      for fn in fns:
        fp = join(p, fn)
        if fp.startswith(b'./'):
          fp = fp[2:]
        self.stream.add_file(fp)
        self.update_file_size(fp)

  def watch_all(self):
    self._watch_files(self.stream.get_watched_files())

  def _add_watch_descriptor(self, wd, pn):
    off = wd - len(self.wd2pn) + 1
    if (off > 0):
      from itertools import repeat
      self.wd2pn.extend(repeat(None, off))

    self.wd2pn[wd] = pn

  def _watch_files(self, pns):
    from gonium.linux.inotify import IN_MODIFY

    for pn in pns:
      wd = self.iw.add_watch(pn, IN_MODIFY)
      self._add_watch_descriptor(wd, pn)