Exemplo n.º 1
0
    def on_inotify_event(self, ev) -> None:
        try:
            logger.debug(
                "inotify-event: name: '%s'  mask: %s", ev.name,
                ", ".join([str(x) for x in inotify_flags.from_mask(ev.mask)]))

            location = Location.join(self.location, ev.name)

            if ev.mask & inotify_flags.CREATE:
                self.sig_file_added.emit(self.vfs.get_fileinfo(location))
            elif ev.mask & inotify_flags.DELETE:
                self.sig_file_removed.emit(location)
            elif ev.mask & inotify_flags.DELETE_SELF:
                pass  # directory itself has disappeared
            elif ev.mask & inotify_flags.MOVE_SELF:
                pass  # directory itself has moved
            elif ev.mask & inotify_flags.MODIFY or ev.mask & inotify_flags.ATTRIB:
                self.sig_file_modified.emit(self.vfs.get_fileinfo(location))
            elif ev.mask & inotify_flags.MOVED_FROM:
                self.sig_file_removed.emit(location)
            elif ev.mask & inotify_flags.MOVED_TO:
                self.sig_file_added.emit(self.vfs.get_fileinfo(location))
            elif ev.mask & inotify_flags.CLOSE_WRITE:
                self.sig_file_closed.emit(self.vfs.get_fileinfo(location))
            else:
                # unhandled event
                print("ERROR: Unhandled flags:")
                for flag in inotify_flags.from_mask(ev.mask):
                    print('    ' + str(flag))
        except Exception as err:
            print(traceback.format_exc())
            print("DirectoryWatcher:", err)
def _main():
    watch_dir = "/tmp/watch_tree"
    create_thread = threading.Thread(target=create_test_files,
                                     args=(watch_dir,))

    try:
        os.mkdir(watch_dir)
    except OSError:
        pass

    inotify = INotify()
    watch_flags = (flags.CREATE | flags.DELETE | flags.MODIFY
                   | flags.DELETE_SELF | flags.ISDIR)

    wd_to_path = {}

    wd = inotify.add_watch(watch_dir, watch_flags)
    wd_to_path[wd] = watch_dir
    print("wd_to_path: ", wd_to_path)

    create_thread.start()

    while True:
        for event in inotify.read():
            path = wd_to_path[event.wd]
            event_type = flags.from_mask(event.mask)

            # remember dir name
            if flags.ISDIR in event_type and flags.CREATE in event_type:
                new_dir = os.path.join(path, event.name)
                wd = inotify.add_watch(new_dir, watch_flags)
                wd_to_path[wd] = new_dir

            print("PATH=[{}] FILENAME=[{}] EVENT_TYPES={}"
                  .format(path, event.name, event_type))
Exemplo n.º 3
0
    def run(self, inotify: INotify, store: OrderedDict, timeout: int):
        for file_path in list(store):
            try:
                # for now only monitor modification of files
                logger.info("adding %s to watch list" % file_path)
                wd = inotify.add_watch(file_path, flags.MODIFY)
                watches[wd] = file_path
            except FileNotFoundError:
                pass

        # print("watch list %s " % watches)
        while self._running:
            for event in inotify.read(timeout=timeout):
                # print(str(event))
                for flag in flags.from_mask(event.mask):
                    # we only watch files for modification events
                    if flag == flags.MODIFY:
                        # only if the event is for a file that we are watching for
                        if event.wd in watches:
                            file_path = watches[event.wd]
                            logger.debug("modify event received for %s " %
                                         file_path)
                            store[
                                file_path] = FStatCache.get_file_stats_using_stat(
                                    file_path)
Exemplo n.º 4
0
    def on_watch_event(self, event):
        MASK_NEW_DIR = flags.CREATE | flags.ISDIR

        if logging.getLogger().isEnabledFor(logging.DEBUG):
            logger.debug('New event: %r', event)
            for flag in flags.from_mask(event.mask):
                logger.debug('-> flag: %s', flag)

        if MASK_NEW_DIR == MASK_NEW_DIR & event.mask:
            new_dir_path = join(self.wds[event.wd], event.name)
            self.on_new_dir(new_dir_path)
        elif flags.CLOSE_WRITE & event.mask and event.name:
            # we are watching a directory and a file inside of it has been touched
            logger.debug('Watching dir, file touched')
            self.on_file_write(join(self.wds[event.wd], event.name))
        elif flags.CLOSE_WRITE & event.mask and not event.name:
            # we are watching a file
            logger.debug('Watching file, file touched')
            self.on_file_write(self.wds[event.wd])
        elif flags.IGNORED & event.mask:
            # inotify_rm_watch was called automatically
            # (file/directory removed/unmounted)
            path = self.wds[event.wd]
            self.wds.pop(event.wd)
            self.on_file_gone(path)
Exemplo n.º 5
0
    def process(self, event):
        flags_ = flags.from_mask(event.mask)

        if flags.ISDIR in flags_:
            if flags.DELETE in flags_:
                path = os.path.join(self.TRACKING_DIRS, event.directory.path)
                if os.path.exists(path):
                    os.system('rm -r {}'.format(path))
            else:
                # TODO: add proper exception
                logger_srv.error('Do not support "{}" type of operations '
                                .format(flags_))
                raise NotImplementedError
        else:
            path = os.path.join(self.TRACKING_DIRS, event.directory.path,
                                event.name)
            if flags.MODIFY in flags_:
                fc.patch(path, event.diff)
            elif flags.DELETE in flags_:
                if os.path.exists(path):
                    os.system('rm {}'.format(path))
            else:
                # TODO: add proper exception
                logger_srv.error('Do not support "{}" type of operations '
                                .format(flags_))
                raise NotImplementedError
Exemplo n.º 6
0
 def run(self):
     self.alive = True
     try:
         while self.alive == True:
             for event in self.inotify.read():
                 if self.alive == False:
                     break
                 if event.name.endswith('.py'):
                     print(
                         'Cheguei em event.name. Name: {}, knownmodules:{}'.
                         format(event.name, self.known_modules))
                     module_name = self.path + '.' + os.path.splitext(
                         event.name)[0]
                     for flag in flags.from_mask(event.mask):
                         if flag.name == 'CLOSE_WRITE':
                             if event.name not in self.known_modules:
                                 print('Primeira vez em known modules ({})'.
                                       format(self.known_modules))
                                 self.known_modules.add((event.name))
                                 self.register.add_module(module_name)
                         if flag.name == 'DELETE':
                             # TODO: handle unload from roles / modules
                             pass
     finally:
         self.inotify.rm_watch(self.wd)
Exemplo n.º 7
0
    def run(self):
        """Run the event detection.
        """

        if self.create_job is not None:
            self.create_job.start()

        timeout = self.timeout * 1000  # is in ms
        n_events = 0
        t_start = 0
        run_loop = True
        while run_loop:
            events = self.inotify.read(timeout=timeout)

            if t_start and not events:
                run_loop = False

            for event in events:
                if not t_start:
                    t_start = time.time()

                if event.wd < 0:
                    continue

                event_type = flags.from_mask(event.mask)

                if flags.OPEN in event_type:
                    n_events += 1

        t_needed = time.time() - t_start
        print("n_events {} in {} s, ({} Hz)"
              .format(n_events, t_needed, n_events / t_needed))

        if self.create_job is not None:
            self.create_job.join()
def _main():
    watch_dir = "/tmp/watch_tree"

    try:
        for path in watch_dir:
            os.mkdir(path)
    except OSError:
        pass

    inotify = INotify()
    watch_flags = flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF

    wd_to_path = {}

    wd = inotify.add_watch(watch_dir, watch_flags)
    wd_to_path[wd] = watch_dir
    print("wd_to_path: ", wd_to_path)

    with open(os.path.join(watch_dir, "test_file"), "w"):
        pass

    while True:
        for event in inotify.read():
            path = wd_to_path[event.wd]
            event_type = flags.from_mask(event.mask)

            print("PATH=[{}] FILENAME=[{}] EVENT_TYPES={}".format(
                path, event.name, event_type))
Exemplo n.º 9
0
    def on_watch_event(self, event):
        MASK_NEW_DIR = flags.CREATE | flags.ISDIR

        if logging.getLogger().isEnabledFor(logging.DEBUG):
            logger.debug('New event: %r', event)
            for flag in flags.from_mask(event.mask):
                logger.debug('-> flag: %s', flag)

        if MASK_NEW_DIR == MASK_NEW_DIR & event.mask:
            new_dir_path = join(self.wds[event.wd], event.name)
            self.on_new_dir(new_dir_path)
        elif flags.CLOSE_WRITE & event.mask and event.name:
            # we are watching a directory and a file inside of it has been touched
            logger.debug('Watching dir, file touched')
            self.on_file_write(join(self.wds[event.wd], event.name))
        elif flags.CLOSE_WRITE & event.mask and not event.name:
            # we are watching a file
            logger.debug('Watching file, file touched')
            self.on_file_write(self.wds[event.wd])
        elif flags.IGNORED & event.mask:
            # inotify_rm_watch was called automatically
            # (file/directory removed/unmounted)
            path = self.wds[event.wd]
            self.wds.pop(event.wd)
            self.on_file_gone(path)
Exemplo n.º 10
0
 def run(self, pool_name):
     event = self.inotify.read()[0]
     file_path = ""
     print(" >>>>> event = ", event)
     for flag in flags.from_mask(event.mask):
         if str(flag) == "flags.CREATE" or str(flag) == "flags.MODIFY":
             try:
                 Printer().cprint(
                     " **** TaskRegistrator detected a new task to be registered ... name = %s"
                     % event.name)
                 cmd, key = self.__extract_json(event.name)
                 file_path = self.main_path + pool_name + "/" + event.name
                 print(" ** DIBACA = ", file_path)
                 with open(file_path) as json_file:
                     self.data = {
                         "cmd": cmd,
                         "key": key,
                         "pool_name": pool_name,
                         "path": file_path,
                         "value": json.load(json_file)
                     }
                 print("Data .. DISIMPAN")
                 return True
             except:
                 print(" damn .. masuk else ...")
                 pass
     if os.path.exists(file_path):
         print(" sampah! deleting ..")
         os.remove(file_path)
         print(" File is deleted ..")
     return True
Exemplo n.º 11
0
 def run(self):
     event = self.inotify.read()[0]
     for flag in flags.from_mask(event.mask):
         if str(flag) == "flags.MODIFY":
             with open(self.tick_file_path) as json_file:
                 self.tick_data = json.load(json_file)
     return True
Exemplo n.º 12
0
 def run(self):
     logger.info(f'Watching input for {self.input_file}')
     watch_flags = flags.CREATE | flags.MODIFY
     self.watched = self.inotify.add_watch(self.input_file, watch_flags)
     while self.running:
         for event in self.inotify.read():
             if flags.from_mask(event.mask) is flags.CREATE | flags.MODIFY:
                 self.fire_event()
Exemplo n.º 13
0
def main():
    inotify = INotify()
    watch_flags = flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF
    wd = inotify.add_watch('/tmp', watch_flags)
    for event in inotify.read():
        print(event)
        for flag in flags.from_mask(event.mask):
            print('    ' + str(flag))
Exemplo n.º 14
0
 def __init__(self, event, path):
     self.event = event
     self.wd = event[0]
     self.mask = event[1]
     self.cookie = event[2]
     self.name = event[3]
     self.flags = flags.from_mask(self.mask)
     self.parent_path = path
     self.full_path = path + "/" + self.name
Exemplo n.º 15
0
 def __inotifyRead(self):
     while self.__active:
         for event in self.__inotify.read():
             for flag in flags.from_mask(event.mask):
                 for path in self.paths:
                     self.emit(flag, {
                         'type': flag,
                         'path': path,
                         'entry': event.name
                     })
def run_monitor(monitored_path, slack_lambda_name, state_machine_arn):
    wd_dir_map = {}

    root_wd = inotify_service.add_watch(monitored_path, WATCH_FLAGS)
    wd_dir_map[root_wd] = monitored_path

    while 1:
        for event in inotify_service.read(read_delay=500):
            reported_flags = flags.from_mask(event.mask)

            # we're only interested in creation events
            if flags.CREATE in reported_flags or flags.MOVED_TO in reported_flags:
                parent_path = wd_dir_map[
                    event.
                    wd]  # map the current watch descriptor to the watched folder
                current_path = os.path.join(
                    parent_path, event.name)  # the full path of the event
                # and only directory creations in the root folder (direct sub-directories)
                if flags.ISDIR in reported_flags and event.wd is root_wd:
                    logger.info(f"New runfolder detected: {current_path}")
                    try:
                        # try add a watch for the newly created directory (runfolder)
                        # these watches are automatically removed when the directory is deleted
                        wd = inotify_service.add_watch(current_path,
                                                       WATCH_FLAGS)
                        wd_dir_map[wd] = current_path
                    except OSError as err:
                        notify_slack(
                            topic=SLACK_TOPIC,
                            title=event.name,
                            message="ERROR creating watch for new runfolder!",
                            lambda_name=slack_lambda_name)
                    notify_slack(topic=SLACK_TOPIC,
                                 title=event.name,
                                 message="New runfolder detected.",
                                 lambda_name=slack_lambda_name)
                # or the creation of the ready flag file
                elif event.name == FLAG_FILE_NAME:
                    logger.info(f"New flag file detected: {current_path}")
                    # found a flag file, so the directory linked to the watch descriptor is the runfolder
                    runfolder = os.path.basename(parent_path)
                    notify_slack(topic=SLACK_TOPIC,
                                 title=runfolder,
                                 message="Runfolder ready flag detected.",
                                 lambda_name=slack_lambda_name)
                    start_pipeline(state_machine_arn=state_machine_arn,
                                   runfolder=runfolder)
                    # Could remove watch for this run, instead of watching it until the directory is removed
                else:  # Ignore other events
                    logger.debug(
                        f"Ignored CREATE/MOVE_TO event for {event.name}")
            else:  # Ignore event types we haven't signed up for (e.g. DELETE_SELF)
                logger.debug(
                    f"Ignored event with flags {reported_flags} for {event.name}"
                )
Exemplo n.º 17
0
def main(argv: List[str]) -> None:
    inotify = INotify()
    watch_flags = masks.ALL_EVENTS

    print("watching /tmp")
    inotify.add_watch('/tmp', watch_flags)

    while True:
        for event in inotify.read():
            print(event)
            evs = [str(fl) for fl in flags.from_mask(event.mask)]
            print("    " + ", ".join(evs))
Exemplo n.º 18
0
def main(argv: List[str]) -> None:
    inotify = INotify()
    watch_flags = masks.ALL_EVENTS

    print("watching /tmp")
    inotify.add_watch('/tmp', watch_flags)

    while True:
        for event in inotify.read():
            print(event)
            evs = [str(fl) for fl in flags.from_mask(event.mask)]
            print("    " + ", ".join(evs))
    def update_watch_list(self, flags, event):
        flags_concat = ''
        #used to add or remove a watch on a folder
        for flag in flags.from_mask(event.mask):
            flags_concat = flags_concat + str(flag)

        if flags_concat == "flags.CREATEflags.ISDIR":
            self.create_watch_list(flags, event)
        #this sectio is used to remove the entry from the watch dictionary
        elif (flags_concat
              == "flags.DELETEflags.ISDIR") or (flags_concat
                                                == "flags.DELETE_SELF"):
            self.delete_watch_list(flags, event)
Exemplo n.º 20
0
 def changed(self):
     modified = saved = False
     events = self.__inotify.read()
     events = (e for e in events if e.name == self.__name)
     flags = (f for e in events for f in Flags.from_mask(e.mask))
     for flag in flags:
         if flag == Flags.MODIFY:
             modified = True
         elif flag == Flags.CLOSE_WRITE:
             saved = True
         elif flag == Flags.MOVED_TO:
             modified = saved = True
     return modified and saved
 def inotify_watch(self):
     for event in self.inotify.read():
         print(event.name)
         flags_concat = ''
         for flag in flags.from_mask(event.mask):
             self.update_watch_list(flags, event)
             print('    ' + str(flag))
             #get the nature of the event
             flags_concat = flags_concat + str(flag)
         #if not re.search(".*flags.ISDIR", flags_concat):
         json_data = self.json_formatter_to_kafka(event, flags_concat)
         print(str(json_data))
         #Kafka_producer(self.kafka_url, self.kafka_topic_name).run(json_data)
     self.inotify_watch()
Exemplo n.º 22
0
def config_file_listener(config_file):
    # Initialize inotify object
    inotify = INotify()
    watch_flags = flags.MODIFY
    wd = inotify.add_watch(config_file, watch_flags)

    while True:
        # Watch config file changes
        readable, _, _ = select.select([inotify], [], [])

        if inotify in readable:
            for event in inotify.read():
                for flag in flags.from_mask(event.mask):
                    logging.info("Config file changed")
                    # Do stuff
Exemplo n.º 23
0
    def read(self):
        for watch, mask, _, name in self.inotify.read():
            masks = flags.from_mask(mask)
            pathname = self.watch_dir[watch] / name
            status = None

            if flags.ISDIR in masks:
                if flags.CREATE in masks or flags.MOVED_TO in masks:
                    status = "mkdir"
                else:
                    status = "rmdir"

            elif flags.CLOSE_WRITE in masks or flags.MOVED_TO in masks:
                status = "file"

            yield status, pathname
Exemplo n.º 24
0
 def log_file_event(self, file_event):
     actionable_flags = flags.from_mask((self.watchFlags ^ flags.ISDIR
                                         ^ flags.Q_OVERFLOW)
                                        & file_event.mask)
     if len(actionable_flags) > 0:
         log_string = ""
         for flag in actionable_flags:
             log_string += str(flag)[6:] + ", "
         log_string = log_string[:-2]
         log_string += " " + file_event.full_path
         if flags.Q_OVERFLOW & file_event.mask:
             self.logger.error(log_string)
             self.logger.error(
                 "INOTIFY QUEUE OVERFLOW DETECTED - some events may not have been logged!"
             )
         else:
             self.logger.info(log_string)
Exemplo n.º 25
0
    def run(self, rollover=False):
        if rollover:
            self.logger.info("Tailing log file after rollover at %s" %
                             self.config.log_path)
        else:
            self.logger.info('Begin tailing log file at %s' %
                             self.config.log_path)

        try:
            with open(self.config.log_path, 'rt') as fh:
                try:
                    inotify = INotify()
                except IsADirectoryError as e:
                    #inotify_simple known issue
                    #https://github.com/chrisjbillington/inotify_simple/issues/17
                    self.logger.critical(
                        'inotify_simple version error on this kernel')
                    self.queue.put(PyaltEnum.INOTIFY_ERR)
                    return

                mask = flags.MODIFY | flags.MOVE_SELF
                wd = inotify.add_watch(self.config.log_path, mask)

                while True:
                    do_break = False
                    for event in inotify.read():
                        for flag in flags.from_mask(event.mask):
                            if flag is flags.MOVE_SELF:
                                # rollover is happening
                                self.logfile_modified(fh)
                                do_break = True
                            elif flag is flags.MODIFY:
                                self.logfile_modified(fh)

                    if do_break:
                        break

        except FileNotFoundError as e:
            self.logger.warning(
                'Log rollover did not happen quickly enough. Quick sleep then try again.'
            )
            time.sleep(.05)
            self.run(rollover=True)

        inotify.close()
        self.run(rollover=True)
Exemplo n.º 26
0
 def log_file_event(self, file_event):
     actionable_flags = flags.from_mask((self.watch_flags ^ flags.ISDIR
                                         ^ flags.Q_OVERFLOW)
                                        & file_event.mask)
     if len(actionable_flags) > 0:
         log_string = ""
         for flag in actionable_flags:
             log_string += str(flag)[6:] + ", "
         log_string = log_string[:-2]
         log_string += " " + file_event.full_path
         if flags.Q_OVERFLOW & file_event.mask:
             self.logger.error(log_string)
             self.logger.error(
                 "INOTIFY QUEUE OVERFLOW DETECTED - some events may not have been logged!"
             )
         else:
             # Only log this event if we haven't logged an identical event very recently
             if self.expiring_file_event_dict.get(log_string) == None:
                 self.expiring_file_event_dict[log_string] = True
                 self.logger.info(log_string)
Exemplo n.º 27
0
def watch_directory(directory):
    inotify = INotify()
    watch_flags = flags.CREATE | flags.DELETE

    logging.info(f"Starting inotify on directory: {directory}")
    try:
        inotify.add_watch(directory, watch_flags)
    except Exception as e:
        logging.error(f"{e}")
        return

    while True:
        for event in inotify.read():
            for flag in flags.from_mask(event.mask):
                logging.info(f"Event on {directory}: {event.name} - {flag}")
                item = {
                    "path": f"{directory}/{event.name}",
                    "action": str(flag)
                }

                q.put(item)
Exemplo n.º 28
0
    def process(self, event):
        flags_ = flags.from_mask(event.mask)
        # Generate path to local copy for new object to have chance
        # get changes on object modify event later.
        ds_path_to_new_object = \
            os.path.join(self.ds_path,
                         event.directory.path[len(self.directory)+1:])

        if flags.ISDIR in flags_:
            if flags.CREATE in flags_:
                copy_tree(event.directory.path, ds_path_to_new_object)
                self.sender.send_directory(event.directory.path)
            elif flags.DELETE in flags_:
                os.system('rm -r {}'.format(ds_path_to_new_object))
                self.sender.send_event(event)
            else:
                logger_cl.error('Can not handle "{}" event'.format(flags_))
                # TODO: add proper exception
                raise NotImplementedError
        else:
            file_path = os.path.join(event.directory.path, event.name)
            ds_file_path = os.path.join(ds_path_to_new_object, event.name)
            if flags.CREATE in flags_:
                if not os.path.exists(ds_path_to_new_object):
                    os.mkdir(ds_path_to_new_object)
                os.system('cp {} {}'.format(file_path, ds_file_path))
                self.sender.send_file(event.directory.path, event.name)
            elif flags.MODIFY in flags_:
                diff = fc.diff(ds_file_path, file_path)
                fc.patch(ds_file_path, diff)
                event.diff = diff
                self.sender.send_event(event)
            elif flags.DELETE in flags_:
                if os.path.exists(ds_file_path):
                    os.system('rm -r {}'.format(ds_file_path))
                self.sender.send_event(event)
            else:
                # TODO: add proper exception
                logger_cl.error('Can not handle "{}" event'.format(flags_))
                raise NotImplementedError
Exemplo n.º 29
0
    def setupandloop(self):
        self.inotify = INotify()
        # FIXME: make flags configuratble
        watch_flags = flags.CREATE | flags.DELETE | flags.CLOSE_WRITE | flags.DELETE_SELF
        #watch_flags = flags.CREATE | flags.DELETE | flags.MODIFY | flags.CLOSE_WRITE | flags.DELETE_SELF
        for i in self.config['watch']:
            logging.debug('Watching: %s', i)
            wd = self.inotify.add_watch(i, watch_flags)

        # loop while run is True
        while self.run:
            # wait for events for 1 second
            for event in self.inotify.read(timeout=1000):
                print(event)
                # FIXME
                # id, event = '', type = 'message', ageent = '', user = ''
                self.eventcallback(
                    lib.event.event(
                        self.config['instanceid'], event, 'message', '', '', {
                            'flags': str(flags.from_mask(event.mask)),
                            'event': event
                        }))
def beda(log, check):
    inotify.add_watch(log, watch_flags)
    try:
        for event in inotify.read(timeout=1):
            if event is not None:
                for flag in flags.from_mask(event.mask):
                    if str(flag) == 'flags.MODIFY':
                        with open(log,
                                  "r") as logFile, open(check) as checkFile:
                            loglines = logFile.readlines()
                            checklines = checkFile.readlines()
                            d = difflib.Differ()
                            diff = d.compare(loglines, checklines)
                            perbedaan = "".join(x[2:] for x in diff
                                                if x.startswith('- '))
                        if perbedaan != "":
                            try:
                                kirim = 'ERROR TERDETEKSI:\n' + perbedaan
                                bot.send_message(chatId, kirim)
                                with open(check, "a+") as appendFile:
                                    appendFile.write(perbedaan)
                            except:
                                print("Gagal mengirim error")
                        else:
                            pass
                    elif str(flag) == 'flags.MOVE_SELF':
                        with open(check, 'w'):
                            pass
                    elif str(flag) == 'flags.IGNORED':
                        print("warning")
                        bot.send_message(
                            chatId,
                            "Peringatan! file error.log diubah secara manual")
                    else:
                        print(str(flag))
                        bot.send_message(chatId, str(flag))
    except:
        print("error event")
        inotify.rm_watch(log)
Exemplo n.º 31
0
 def _recognize_event_patterns(events):
     move_pairs = dict()
     move_pairs_tmp = dict()
     all_events = []
     for ev in events:
         # Store the event and flags for chrono order processing.
         flags = _inotify_flags.from_mask(ev.mask)
         all_events.append((ev, flags))
         # Form pairs for move events.
         if _inotify_flags.MOVED_FROM in flags:
             if ev.cookie in move_pairs_tmp:
                 move_pairs[ev.cookie] = ((ev, flags), move_pairs_tmp[ev.cookie])
                 del move_pairs_tmp[ev.cookie]
             else:
                 move_pairs_tmp[ev.cookie] = (ev, flags)
         elif _inotify_flags.MOVED_TO in flags:
             if ev.cookie in move_pairs_tmp:
                 move_pairs[ev.cookie] = (move_pairs_tmp[ev.cookie], (ev, flags))
                 del move_pairs_tmp[ev.cookie]
             else:
                 move_pairs_tmp[ev.cookie] = (ev, flags)
     return move_pairs, all_events
Exemplo n.º 32
0
def watch(args):
    logger = setup_logger()
    config = load_config(args.config)
    keeplog = Keeplog(logger, config)

    inotify = INotify()
    watch_flags = flags.MODIFY | flags.DELETE_SELF
    inotify.add_watch(config.file, watch_flags)

    while True:
        try:
            logger.info("Watching local log file for changes")
            modified = False
            for event in inotify.read(timeout=config.watch_interval * 1000):
                for flag in flags.from_mask(event.mask):
                    if flag == flags.MODIFY:
                        logger.info("File modified, triggering sync")
                        modified = True
                    elif flag == flags.DELETE_SELF:
                        logger.info("File deleted/replaced, triggering sync")
                        modified = True
                        inotify.add_watch(config.file, watch_flags)
            if not modified:
                logger.info("Local file unmodified, triggering scheduled sync")
            time.sleep(config.watch_sync_delay)
            keeplog.sync()
        except KeyboardInterrupt:
            logger.info("Interrupt received. Exiting.")
            break
        except:
            logger.warning("Unexpected error:", sys.exc_info()[0])
            if config.on_watch_error == "exit":
                logger.warning("Exiting as per watch error strategy 'exit'.")
                exit(1)
            else:
                logger.warning(
                    f"Sleeping {config.watch_interval} seconds before trying again. Events may be missed."
                )
                time.sleep(config.watch_interval)
Exemplo n.º 33
0
 def _recognize_event_patterns(events):
     move_pairs = dict()
     move_pairs_tmp = dict()
     all_events = []
     for ev in events:
         # Store the event and flags for chrono order processing.
         flags = _inotify_flags.from_mask(ev.mask)
         all_events.append((ev, flags))
         # Form pairs for move events.
         if _inotify_flags.MOVED_FROM in flags:
             if ev.cookie in move_pairs_tmp:
                 move_pairs[ev.cookie] = ((ev, flags), move_pairs_tmp[ev.cookie])
                 del move_pairs_tmp[ev.cookie]
             else:
                 move_pairs_tmp[ev.cookie] = (ev, flags)
         elif _inotify_flags.MOVED_TO in flags:
             if ev.cookie in move_pairs_tmp:
                 move_pairs[ev.cookie] = (move_pairs_tmp[ev.cookie], (ev, flags))
                 del move_pairs_tmp[ev.cookie]
             else:
                 move_pairs_tmp[ev.cookie] = (ev, flags)
     return move_pairs, all_events
Exemplo n.º 34
0
from inotify_simple import INotify, flags

os.mkdir('/tmp/inotify_test')

inotify = INotify()
watch_flags = flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF
wd = inotify.add_watch('/tmp/inotify_test', watch_flags)

# Now create, delete and modify some files in the directory being monitored:
os.chdir('/tmp/inotify_test')
# CREATE event for a directory:
os.system('mkdir foo')
# CREATE event for a file:
os.system('echo hello > test.txt')
# MODIFY event for the file:
os.system('echo world >> test.txt')
# DELETE event for the file
os.system('rm test.txt')
# DELETE event for the directory
os.system('rmdir foo')
os.chdir('/tmp')
# DELETE_SELF on the original directory. # Also generates an IGNORED event
# indicating the watch was removed.
os.system('rmdir inotify_test')

# And see the corresponding events:
for event in inotify.read():
    print(event)
    for flag in flags.from_mask(event.mask):
        print('    ' + str(flag))
Exemplo n.º 35
0
 def on_event(ev):
     print(ev)
     evs = [str(fl) for fl in inotify_flags.from_mask(ev.mask)]
     print("    " + ", ".join(evs))