Esempio n. 1
0
 def __init__(self, root_dir=None, excluded_dirs=[]):
     if not root_dir:
         # TODO: create proper exception class
         raise NotADirectoryError
     self.root_dir = Directory(root_dir)
     self.excluded_dirs = excluded_dirs
     self.inotify = INotify()
     self.watch_flags = flags.CREATE | flags.MODIFY | flags.DELETE
     self.watched_dirs = {}
     self.wd_tracker = {}
     self.watch(self.root_dir)
Esempio n. 2
0
 def __init__(self, timeout=None):
     # this could potentially be an LRU Cache
     self._store = OrderedDict()
     self._inotify = INotify()
     self._monitor = MonitorThread()
     self._monitor_thread = Thread(target=self._monitor.run,
                                   args=(
                                       self._inotify,
                                       self._store,
                                       timeout,
                                   ))
Esempio n. 3
0
def loop(device_matches, device_watch, quiet):
    devices = select_device(device_matches, True)
    try:
        for device in devices:
            device.grab()
    except IOError:
        print("IOError when grabbing device")
        exit(1)
    if device_watch:
        from inotify_simple import INotify, flags
        inotify = INotify()
        inotify.add_watch("/dev/input", flags.CREATE)
        print("Watching keyboard devices plug in")
    device_filter = DeviceFilter(device_matches)
    if quiet:
        print("No key event will be output since quiet option was specified.")
    try:
        while True:
            try:
                waitables = devices[:]
                if device_watch:
                    waitables.append(inotify.fd)
                r, w, x = select(waitables, [], [])
                for waitable in r:
                    if isinstance(waitable, InputDevice):
                        for event in waitable.read():
                            if event.type == ecodes.EV_KEY:
                                on_event(event, waitable.name, quiet)
                            else:
                                send_event(event)
                    else:
                        new_devices = []
                        for event in inotify.read():
                            new_device = InputDevice("/dev/input/" + event.name)
                            if device_filter(new_device) and not in_device_list(new_device.fn, devices):
                                try:
                                    new_device.grab()
                                    devices.append(new_device)
                                    new_devices.append(new_device)
                                except IOError:
                                    # Ignore errors on new devices
                                    print("IOError when grabbing new device: " + str(new_device.name))
                        if new_devices:
                            print("Okay, now enable remapping on the following new device(s):\n")
                            print_device_list(new_devices)
            except OSError as e:
                if isinstance(waitable, InputDevice):
                    print("Device removed: " + str(waitable.name))
                    devices.remove(waitable)
    finally:
        for device in devices:
            device.ungrab()
        if device_watch:
            inotify.close()
Esempio n. 4
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))
Esempio n. 5
0
 def watch_file(self, filename):
     """
     Watch current file line by line
     """
     with INotify() as inotify:
         logging.info("starting watch on %s (inode %d)", filename, inode)
         inotify.add_watch(filename, flags.MODIFY | flags.CLOSE_WRITE)
         for i in yield_until_eof(f):
             yield i
         for i in self._watch_until_closed(f, inotify):
             yield i
         logging.info("finished watch on %s (inode %d)", filename, inode)
Esempio n. 6
0
 def __set_config(self, symbol, binary_id, id):
     self.binary_id = binary_id
     self.id = id
     self.tick_dir = local_settings["inotify_config"]["default_dir"] + symbol
     self.tick_file_path = self.tick_dir + "/" + local_settings[
         "inotify_config"]["default_name"]
     self.inotify = INotify()
     watch_flags = flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF
     self.inotify.add_watch(self.tick_dir, watch_flags)
     os.chdir(self.tick_dir)
     self.tick_data = None
     self.symbol = symbol
 def __watcher(self):
     """Watch for file changes"""
     inotify = INotify()
     wd = inotify.add_watch(self.work_dir, flags.MODIFY)
     while not self.watch_event.is_set():
         for event in inotify.read(timeout=100, read_delay=100):
             for filename in [self.CONFIG_FILE, self.SECRETS_FILE]:
                 if event.name == filename:
                     log.info("File change detected: %s", filename)
                     self.load(self.work_dir)
                     break
     # stop watching
     inotify.rm_watch(wd)
Esempio n. 8
0
def load(config_file, default_config = {}):
    global _config_file, _default_config, _inotify, _wd
    _config_file = config_file
    _default_config = default_config
    try:
        _inotify = INotify()
    except:
        print("Notice: Couldn't load inotify. Config will be reloaded with every call to update()");
        
    dir = './' + os.path.dirname(config_file)
    if _inotify:
        _wd = _inotify.add_watch(dir, flags.CREATE | flags.MODIFY)
    _update_config()
    return Config(_config)
Esempio n. 9
0
 def run(self):
     LOGGER.info("starting")
     init_signals()
     ih = INotify()
     wds = {}
     register_watches(ih, wds)
     force = True
     events = []
     while RUN:
         if force:
             force = False
         else:
             events = ih.read(1000)
             if events is None or len(events) == 0:
                 continue
         if len(events) > 0:
             LOGGER.info("got events")
         for event in events:
             try:
                 path = wds[event.wd]
             except KeyError:
                 path = "unknown"
             LOGGER.debug("%s on %s" % (event, path))
             if not (event.mask & flags.IGNORED):
                 if event.mask & flags.DELETE_SELF:
                     path = wds[event.wd]
                     unregister_watch(ih, wds, event.wd)
                     register_watch(ih, wds, path)
         try:
             lock = filelock.FileLock(get_plugin_lock_path(), timeout=300)
             with lock.acquire(poll_intervall=1):
                 # ok, there is no plugins.install/uninstall running
                 pass
         except filelock.Timeout:
             LOGGER.warning("can't acquire plugin management lock => "
                            "maybe a blocked plugins.install/uninstall ? "
                            "=> exiting")
             break
         if not is_status_running_or_error():
             LOGGER.info("The module is not RUNNING or ERROR => "
                         "ignoring...")
             time.sleep(2)
             force = True
             continue
         ret = self.handle_event()
         if not ret:
             break
         register_watches(ih, wds)
         LOGGER.info("waiting for events...")
     LOGGER.info("stopped")
Esempio n. 10
0
async def run_watch(has_slow_tests: bool, watch_delay: int, cache_dir: str) -> None:
    inotify = INotify()
    watch_flags = (
        masks.MOVE | flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF
    )
    watchers = {}
    for root, dirs, _ in walk("."):
        for d in [".git", "__pycache__"]:
            if d in dirs:
                dirs.remove(d)
        wd = inotify.add_watch(root, watch_flags)
        watchers[wd] = root
    paths: List[str] = []
    finalize = False
    while True:
        events = inotify.read(
            timeout=(watch_delay if (paths or finalize) else None), read_delay=None
        )
        for event in events:
            if event.name.endswith(".isorted"):
                continue
            path = join(watchers[event.wd], event.name)
            if event.mask & flags.ISDIR and event.mask & (
                flags.CREATE | flags.MOVED_TO
            ):
                wd = inotify.add_watch(path, watch_flags)
                watchers[wd] = path
            elif event.mask & flags.IGNORED:
                del watchers[event.wd]
            else:
                paths.append(path)
        if not events:
            if paths:
                if not finalize:
                    p("\n")
                try:
                    await run_formatters()
                except SubprocessError:
                    p("!!! Problems found.")
                else:
                    finalize = True
                paths.clear()
            elif finalize:
                try:
                    await run_checks(has_slow_tests=has_slow_tests, cache_dir=cache_dir)
                except SubprocessError:
                    p("!!! Problems found.")
                else:
                    p("*** All good.")
                finalize = False
Esempio n. 11
0
def subfiles(directory):
    """Return the list of subfiles of a directory, and wait for the newly created
  ones.

  CAUTION : *DONT TRY TO CONVERT THE RESULT OF THIS FUNCTION INTO A LIST !
  ALWAYS ITERATE OVER IT !!!*"""

    with INotify() as inotify:
        inotify.add_watch(directory, flags.CLOSE_WRITE | flags.MOVED_TO)

        names = os.listdir(directory)
        while True:
            for name in names:
                yield os.path.join(directory, name)
            names = (event.name for event in inotify.read())
Esempio n. 12
0
    def __init__(self, path, options):
        super(INotifyWait, self).__init__()
        if str(path) == path:
            self.paths = [os.path.abspath(path)]
        else:
            self.paths = []
            for p in path:
                self.path.append(os.path.abspath(p))
        self.__active = True
        self.__process = None
        self.__inotify = INotify()

        # TODO: find a way to screen events emitted from unrelated paths
        for path in self.paths:
            self.__inotify.add_watch(path, options.get('events'))
Esempio n. 13
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
Esempio n. 14
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)
Esempio n. 15
0
    def __init__(self, dir_filter, observer):
        self.inotify = INotify()
        self.watch_flags = flags.CREATE | flags.DELETE | flags.MODIFY | flags.DELETE_SELF
        self.watch_flags = masks.ALL_EVENTS
        self.watch_flags = \
            flags.CREATE | \
            flags.DELETE | \
            flags.DELETE_SELF | \
            flags.CLOSE_WRITE | \
            flags.MOVE_SELF | \
            flags.MOVED_FROM | \
            flags.MOVED_TO | \
            flags.EXCL_UNLINK

        self.wds = {}
        self.dir_filter = dir_filter
        self.observer = observer
Esempio n. 16
0
    def handle_inotify(self, directory):
        logging.getLogger(__name__).info(
            f"Using inotify to watch directory for changes: {directory}")

        inotify = INotify()
        descriptor = inotify.add_watch(directory,
                                       flags.CLOSE_WRITE | flags.MOVED_TO)
        try:
            while not self.stop_flag:
                for event in inotify.read(timeout=1000, read_delay=1000):
                    file = os.path.join(directory, event.name)
                    _consume(file)
        except KeyboardInterrupt:
            pass

        inotify.rm_watch(descriptor)
        inotify.close()
Esempio n. 17
0
 def add_inotify(self, working_dir):
     inotify = INotify()
     add_inotify_watch(inotify,
                       working_dir,
                       ignores=[
                           "python3_virtualenv",
                           "python3_virtualenv_sources",
                           "python2_virtualenv_sources",
                           "python2_virtualenv",
                           "*.log",
                           "log*",
                           "Log*",
                           "cache*"
                           "Cache*",
                           "Local Storage",
                           "*.sw?",
                           "*~", ".*"])
     return inotify
Esempio n. 18
0
    def on_created(self, event):
        regex = r"(.tif)|(.jp2)"  # TODO parametrizar
        matches = re.finditer(regex, event.src_path)
        images_path = ''

        inotify = INotify()

        print("listening to " + event.src_path)

        for match in matches:
            print("match found")
            images_path = os.path.dirname(event.src_path) + '/'
            img = os.path.basename(event.src_path)

            watch_flags = flags.CLOSE_WRITE
            wd = inotify.add_watch(images_path, watch_flags)  # noqa

            inotify.read(
            )  # bloquea el resto de los procesos hasta que se cierre la escritura del archivo que se está copiando
            print('copied: ' + img)

        red_exists = os.path.isfile(images_path + 'red_665_10.jp2')
        nir_exists = os.path.isfile(images_path + 'nir4_832_10.jp2')

        if red_exists and nir_exists:
            ni = NormalizedDifferenceIndex(images_path, 'sentinel')
            print("starting ndvi calculation process...")
            ni.write_ndvi_image()

            support = Support()

            src_name = 'ndvi.tif'
            original_image = images_path + src_name
            reprojected_image = images_path + 'reprojected-' + src_name

            print("starting reprojection process...")
            support.reprojection('EPSG:4326', original_image,
                                 reprojected_image)

            print("starting cropping process...")
            support.crop_process(images_path, 'reprojected-' + src_name,
                                 images_path, '/graticules', "ISO8859-1")

            print("returning to detection process")
Esempio n. 19
0
    def change_watcher(self):
        # Set up listener for changes
        self.inotify = INotify()
        watch_flags = flags.CREATE | flags.MOVED_TO

        for watch in self.watch_cfg:
            try:
                iwatch = self.inotify.add_watch(watch, watch_flags)
                self.watchers[iwatch] = self.watch_cfg[watch]
            except FileNotFoundError:
                logging.warn(f"Could not set up watcher for {watch}")

        while True:
            for event in self.inotify.read():
                branch = self.watchers[event.wd]['branch']
                directory = str(self.watchers[event.wd]['dir'])

                logging.info(f'Re-populating directory {directory} with {branch}') # noqa

                tree = self.repo.revparse_single(branch).tree
                self.files[directory] = self.build_tree(tree)
Esempio n. 20
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)
Esempio n. 21
0
File: api.py Progetto: danlg/zato
    def __init__(self, server, worker_store):
        # type: (ParallelServer, WorkerStore) -> None

        self.server = server
        self.worker_store = worker_store
        self.update_lock = RLock()

        self.keep_running = True

        # A list of all observer objects
        self.observer_list = []

        # A mapping of channel_id to an observer object associated with the channel.
        # Note that only non-inotify observers are added here.
        self.observer_dict = {}

        # Caches parser objects by their name
        self._parser_cache = {}

        if is_linux:

            # inotify_simple
            from inotify_simple import flags as inotify_flags, INotify

            self.inotify_lock = RLock()

            self.inotify = INotify()
            self.inotify_flags = inotify_flags.CLOSE_WRITE

            self.inotify_wd_to_path = {}
            self.inotify_path_to_observer_list = {}

            # Inotify is used only under Linux
            self.observer_start_args = self.inotify, self.inotify_flags, self.inotify_lock, self.inotify_wd_to_path

        else:
            self.observer_start_args = ()

        # Maps channel name to a list of globre patterns for the channel's directories
        self.pattern_matcher_dict = {}
Esempio n. 22
0
def _wait_files_creation(file_list):
  # Establish a list of directory and subfiles.
  # and test existence before watching, so that we don't miss an event.
  directories = defaultdict(dict)
  for f in file_list:
    dirname, filename = os.path.split(f)
    directories[dirname][filename] = os.path.lexists(f)

  def all_files_exists():
    return all(all(six.itervalues(files)) for files in six.itervalues(directories))

  with INotify() as inotify:
    watchdescriptors = {inotify.add_watch(dirname,
        flags.CREATE | flags.DELETE | flags.MOVED_TO | flags.MOVED_FROM
        ): dirname
      for dirname in directories}

    while not all_files_exists():
      for event in inotify.read():
        directory = directories[watchdescriptors[event.wd]]
        if event.name in directory:
          directory[event.name] = event.mask & (flags.CREATE | flags.MOVED_TO)
Esempio n. 23
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)
Esempio n. 24
0
    def __init__(self):
        """Summary
        """
        self.inotify = INotify()
        self.watched_flags = flags.CREATE | flags.MOVED_TO | flags.ISDIR
        self.directories = {}
        self.watch_descriptors = {}
        watch_descriptor = self.inotify.add_watch(
            config.APP_LANDING_INGEST_DIR, self.watched_flags)
        self.directories[watch_descriptor] = config.APP_LANDING_INGEST_DIR
        self.watch_descriptors[
            self.directories[watch_descriptor]] = watch_descriptor
        logger.info("Watching ingestion folder")

        while True:
            events = self.inotify.read(read_delay=1000)
            all_events = self.get_all_events(events)
            for event in all_events:
                path = os.path.join(self.directories[event.wd], event.name)
                upload_file(path)
                os.remove(path)
            self.delete_folders_if_empty(config.APP_LANDING_INGEST_DIR)
Esempio n. 25
0
def notifications(path):

    nag = Nagios()

    watchdir = str(Path(path).parent)
    watchfile = str(Path(path).name)

    watchdog = INotify()
    watch_flags = flags.MOVED_TO
    watchdog.add_watch(watchdir, watch_flags)

    nag.loads(path)
    for service, status in nag.get_services('*'):
        cached_services[service] = status

    while True:
        for event in watchdog.read(1000): # 1000 msecs timeout
            if event.name == watchfile:
                nag.loads(path)

            for service, status in nag.get_services('*'):
                cached_services[service] = status
Esempio n. 26
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
                        }))
Esempio n. 27
0
    def _notify_changes(self, directories):
        """
        Registers notification on FS events.

        Keyword arguments:
        directory -- directory to watch
        callback -- function to execute on event
        """
        self._inotify = INotify()
        watch_flags = flags.CREATE | flags.DELETE | flags.MODIFY | flags.MOVED_FROM | \
                      flags.MOVED_TO | flags.DELETE_SELF
        for directory in directories:
            if exists(directory):
                self._watch_descriptors.append(
                    self._inotify.add_watch(directory, watch_flags))

        # wait for event
        try:
            self._inotify.read()
            self._callback()
        except ValueError:
            # do not care if the watcher has been removed / thread killed
            pass
Esempio n. 28
0
    def loop_inotify(self, mail_delta):
        directory = self.file_consumer.consume
        inotify = INotify()
        inotify.add_watch(directory, flags.CLOSE_WRITE | flags.MOVED_TO)

        # Run initial mail fetch and consume all currently existing documents
        self.loop_step(mail_delta)
        next_mail_time = self.mail_fetcher.last_checked + mail_delta

        while True:
            # Consume documents until next_mail_time
            while True:
                delta = next_mail_time - time.time()
                if delta > 0:
                    for event in inotify.read(timeout=delta):
                        file = os.path.join(directory, event.name)
                        if os.path.isfile(file):
                            self.file_consumer.try_consume_file(file)
                else:
                    break

            self.mail_fetcher.pull()
            next_mail_time = self.mail_fetcher.last_checked + mail_delta
def start_thread(log, check):
    inotify = INotify()
    i = 1
    while 1:
        if i == 1:
            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 = log + ' ERROR TERDETEKSI:\n' + perbedaan
                    for chatId in chatIds:
                        bot.send_message(chatId, kirim)
                    with open(check, "a+") as appendFile:
                        appendFile.write(perbedaan)
                except:
                    print("Gagal mengirim error")
            else:
                pass
            i += 1
        beda(log, check, inotify)
Esempio n. 30
0
File: consume.py Progetto: tjlx/rupa
    def main(self):
        # listen to when a new file is created
        listen_dir = 'data'

        #initialise files
        moFile = 'data/mo_20180316_034523.pkl'
        owFile = 'data/ow_20180316_034513.pkl'
        xuFile = 'data/xu_20180316_034508.pkl'

        while True:  #loop until user terminates
            inotify = INotify()
            watch_flags = flags.CREATE  # | flags.DELETE | flags.MODIFY | flags.DELETE_SELF
            wd = inotify.add_watch(listen_dir, watch_flags)
            print("Listening... (Ctrl-C to terminate)")
            for event in inotify.read():
                print("stop C")
                if flags.CREATE in event:
                    print('File created in', event)
                    newfile = event[3]
                    print('Filename = ', newfile, 'TYPE = ', type(newfile))

                    if 'mo' in newfile:
                        moFile = newfile
                    elif 'ow' in newfile:
                        owFile = newfile
                    elif 'xu' in newfile:
                        xuFile = newfile
                    else:
                        continue

                    with open(newfile,
                              'rb') as f:  # stdout of newfile that was created
                        pick = (pickle.load(f))
                        print('pick = ', pick)

                    self.plot(moFile, owFile, xuFile)