python看门狗不会触发Linux事件

时间:2020-10-13 08:42:41

标签: python python-watchdog

我有一个简单的脚本来监视文件的更改,并查看正则表达式中文件的模式,然后发送闲置通知。在测试脚本时,我使用vim写入文件,此操作按预期工作,但是现在我试图使用该脚本从某些EDA工具监视日志文件,但该文件无法正常工作。当我使用VS代码写入文件时,它也不会拾取事件。如果我手动添加一些行并使用vim保存EDA日志文件,则事件会发生。

这是我的文件处理程序类:

class FileCreatedHandler(FileSystemEventHandler):
  def __init__(self, observer, file_path, re_pattern, channel, message, exit_on_find = True):
    self.observer = observer
    self.file_path = file_path
    self.filename = os.path.basename(file_path)
    self.dir = os.path.dirname(file_path)
    self.src_path = self.dir
    self.re_pattern = re_pattern
    self.channel = channel
    self.message = message
    self.exit_on_find = exit_on_find
  @staticmethod
  def _LastNlines(fname, N):
    assert N >= 0
    pos = N + 1
    lines = []
    with open(fname) as f:
        while len(lines) <= N:
            try:
                f.seek(-pos, 2)

            except IOError:
                f.seek(0)
                break
            finally:
                lines = list(f)
            pos *= 2
    return lines[-N:]

  def on_any_event(self, event):
    logging.info("an event happened")
    if not event.is_directory and event.src_path.endswith(self.filename):
        SendMessage(self.channel, "An event was triggered for {0}".format(self.filename))

  def on_created(self, event):
    if not event.is_directory and event.src_path.endswith(self.filename):
        logging.info("File: {} has been created".format(self.filename))

  def _check_file_for_pattern(self):
    try:
        lines = (self._LastNlines(self.file_path, 100))
        matches = []
        for line in lines:
            m = self.re_pattern.search(line)
            if(m):
                matches.append(m)

        if(len(matches)):
            SendMessage(self.channel, self.message)
            return 1
        return 0
    except Exception as e:
        logging.error(e)
        sys.exit(1)
  def on_modified(self, event):
    if not event.is_directory and event.src_path.endswith(self.filename):
        if(self._check_file_for_pattern() ):
            logging.info("match found")
            if(self.exit_on_find):
                self.observer.stop()
                sys.exit(0)

我这样启动观察者:

observers = []
observer = Observer()
for watcher in watchers: 
    observers.append(watcher.SetupWatcher(observer))

try: 
    observer.start()
    observer.join()
except KeyboardInterrupt as e:
    observer.stop()

我有一个使用pyyaml隐式构造实例化的watcher类:

class Watcher(YamlObject):
  yaml_tag = u"!watcher"

  def __init__(self, file_path, grep, message, channel_name, exit_on_find = True):
    self.File = os.path.abspath(file_path)
    self.File_dir = os.path.dirname(self.File)
    self.Grep = re.compile(grep)
    self.Message = message 
    self.Exit_on_find = exit_on_find
    self.channel_name = channel_name

  def SetupWatcher(self, observer):
    print("adding watcher file: {}".format(self.File))
    created_handler = FileCreatedHandler(observer, self.File, self.Grep, self.channel_name, 
    self.Message, self.Exit_on_find)
    observer.schedule(created_handler, self.File_dir, recursive = False)
    return observer

任何想法可能导致这种行为吗?谢谢!

0 个答案:

没有答案