Exemple #1
0
    def _inotify_watch_recursive(self, fd, watch_dir):
        self.msg.debug(self.name, 'inotify: Watching %s' % watch_dir)
        inotifyx.add_watch(fd, watch_dir.encode('utf-8'), inotifyx.IN_OPEN | inotifyx.IN_CLOSE)

        for root, dirs, files in os.walk(watch_dir):
            for dir_ in dirs:
                self._inotify_watch_recursive(fd, os.path.join(root, dir_))
    def __init__(self, paths, white_list, black_list, delay):
        """Creates a new file change monitor."""

        # Events of interest.
        self.WATCH_EVENTS = inotifyx.IN_CREATE | inotifyx.IN_MODIFY | inotifyx.IN_MOVE# | inotifyx.IN_DELETE_SELF | inotifyx.IN_DELETE

        # Remember params.
        self.white_list = white_list
        self.black_list = black_list
        self.delay = delay

        # Init inotify.
        self.fd = inotifyx.init()

        # Watch specified paths.
        self.watches = {}
        self.watches.update((inotifyx.add_watch(self.fd, path, self.WATCH_EVENTS), path)
                            for path in paths)

        # Watch sub dirs of specified paths.  Ensure we modify dirs
        # variable in place so that os.walk only traverses white
        # listed dirs.
        for path in paths:
            for root, dirs, files in os.walk(path):
                dirs[:] = [dir for dir in dirs if self.is_white_listed(dir)]
                self.watches.update((inotifyx.add_watch(self.fd, os.path.join(root, dir), self.WATCH_EVENTS), os.path.join(root, dir))
                                    for dir in dirs)
Exemple #3
0
    def __init__(self, paths, white_list, black_list, delay):
        """Creates a new file change monitor."""

        # Events of interest.
        self.WATCH_EVENTS = inotifyx.IN_CREATE | inotifyx.IN_MODIFY | inotifyx.IN_DELETE | inotifyx.IN_DELETE_SELF | inotifyx.IN_MOVE

        # Remember params.
        self.white_list = white_list
        self.black_list = black_list
        self.delay = delay

        # Init inotify.
        self.fd = inotifyx.init()

        # Watch specified paths.
        self.watches = {}
        self.watches.update(
            (inotifyx.add_watch(self.fd, path, self.WATCH_EVENTS), path)
            for path in paths)

        # Watch sub dirs of specified paths.  Ensure we modify dirs
        # variable in place so that os.walk only traverses white
        # listed dirs.
        for path in paths:
            for root, dirs, files in os.walk(path):
                dirs[:] = [dir for dir in dirs if self.is_white_listed(dir)]
                self.watches.update((inotifyx.add_watch(
                    self.fd, os.path.join(root, dir), self.WATCH_EVENTS),
                                     os.path.join(root, dir)) for dir in dirs)
Exemple #4
0
    def _inotify_watch_recursive(self, fd, watch_dir):
        self.msg.debug(self.name, 'inotify: Watching %s' % watch_dir)
        inotifyx.add_watch(fd, watch_dir.encode('utf-8'), inotifyx.IN_OPEN | inotifyx.IN_CLOSE)

        for path in os.listdir(watch_dir):
            if os.path.isdir(os.path.join(watch_dir, path)):
                self._inotify_watch_recursive(fd, os.path.join(watch_dir, path))
Exemple #5
0
def load_entities(charnames):
    wds = list()
    skins = list()

    for charname in charnames:
        path = os.path.join(usf_root, 'data', 'characters', charname)
        path2 = os.path.join(usf_root, 'data', 'characters_unfinished', charname)

        if os.path.exists(path):
            if not os.path.exists(
                os.path.join(path,path.split(os.path.sep)[-1])+".xml"
                ):
                create_character_xml(path)

            skins.append(EntitySkin(path))

            if inotifyx:
                wds.append(inotifyx.add_watch(fd, path, inotifyx.IN_MODIFY))

        elif os.path.exists(path2):
            if not os.path.exists(
                os.path.join(path2,path2.split(os.path.sep)[-1])+".xml"
                ):
                create_character_xml(path2)
            skins.append(EntitySkin(path2))

            if inotifyx:
                wds.append(inotifyx.add_watch(fd, path2, inotifyx.IN_MODIFY))

        else:
            logging.error("no directory of this name in characters.")

    return skins, wds
Exemple #6
0
    def _inotify_watch_recursive(self, fd, watch_dir):
        self.msg.debug(self.name, 'inotify: Watching %s' % watch_dir)
        inotifyx.add_watch(fd, watch_dir.encode('utf-8'),
                           inotifyx.IN_OPEN | inotifyx.IN_CLOSE)

        for root, dirs, files in os.walk(watch_dir):
            for dir_ in dirs:
                self._inotify_watch_recursive(fd, os.path.join(root, dir_))
Exemple #7
0
    def _inotify_watch_recursive(self, fd, watch_dir):
        self.msg.debug(self.name, 'inotify: Watching %s' % watch_dir)
        inotifyx.add_watch(fd, watch_dir.encode('utf-8'),
                           inotifyx.IN_OPEN | inotifyx.IN_CLOSE)

        for path in os.listdir(watch_dir):
            if os.path.isdir(os.path.join(watch_dir, path)):
                self._inotify_watch_recursive(fd,
                                              os.path.join(watch_dir, path))
Exemple #8
0
 def test_file_create(self):
     inotifyx.add_watch(self.fd, self.testdir, inotifyx.IN_CREATE)
     self._create_file('foo')
     try:
         events = inotifyx.get_events(self.fd)
         self.assertEqual(len(events), 1)
         self.assertEqual(events[0].mask, inotifyx.IN_CREATE)
         self.assertEqual(events[0].name, 'foo')
     finally:
         os.unlink('foo')
Exemple #9
0
 def test_file_create(self):
     inotifyx.add_watch(self.fd, self.test_dir, inotifyx.IN_CREATE)
     self._create_file('foo')
     try:
         events = inotifyx.get_events(self.fd)
         self.assertEqual(len(events), 1)
         self.assertEqual(events[0].mask, inotifyx.IN_CREATE)
         self.assertEqual(events[0].name, 'foo')
     finally:
         os.unlink('foo')
Exemple #10
0
  def waitDeletion(self):
    inotify_fd = inotifyx.init()
    try:
      inotifyx.add_watch(inotify_fd, self.filename, inotifyx.IN_DELETE)
      inotifyx.get_events(inotify_fd)
    except IOError: # add_watch failed
      pass
    finally:
      os.close(inotify_fd)

    self.__enter__()
    def waitDeletion(self):
        inotify_fd = inotifyx.init()
        try:
            inotifyx.add_watch(inotify_fd, self.filename, inotifyx.IN_DELETE)
            inotifyx.get_events(inotify_fd)
        except IOError:  # add_watch failed
            pass
        finally:
            os.close(inotify_fd)

        self.__enter__()
Exemple #12
0
 def run(self):
     logging.debug('starting watcher thread')
     mask = inotifyx.IN_MODIFY | inotifyx.IN_CLOSE_WRITE
     in_fd = inotifyx.init()
     for f in self.watch_files:
         inotifyx.add_watch(in_fd, f, mask)
         logging.debug('watching ' + f)
     while True:
         logging.debug('watcher waiting for events')
         inotifyx.get_events(in_fd)
         logging.debug('watcher got change event')
         self.mapfuse.read_list()
Exemple #13
0
 def test_file_remove(self):
     self._create_file('foo')
     try:
         inotifyx.add_watch(self.fd, self.test_dir, inotifyx.IN_DELETE)
     except:
         os.unlink('foo')
         raise
     os.unlink('foo')
     events = inotifyx.get_events(self.fd)
     self.assertEqual(len(events), 1)
     self.assertEqual(events[0].mask, inotifyx.IN_DELETE)
     self.assertEqual(events[0].name, 'foo')
Exemple #14
0
 def test_file_remove(self):
     self._create_file('foo')
     try:
         inotifyx.add_watch(self.fd, self.testdir, inotifyx.IN_DELETE)
     except:
         os.unlink('foo')
         raise
     os.unlink('foo')
     events = inotifyx.get_events(self.fd)
     self.assertEqual(len(events), 1)
     self.assertEqual(events[0].mask, inotifyx.IN_DELETE)
     self.assertEqual(events[0].name, 'foo')
Exemple #15
0
	def _trigger(self, fd, **params):
		"""
		We need events to fire on appearance because the code
		doesn't see the file until after it has been created.

		In WF_KQUEUE mode, this simulates triggering an event by firing
		a oneshot timer event to fire immediately (0 msecs).  Because
		this uses the file descriptor as the timer identity and get() doesn't
		care what filter actually fired the event, the outside world sees
		this as a file change.

		In WF_INOTIFYX mode, this triggers an event by setting IN_OPEN on
		the inotify watch, opening the file in read-only mode, closing it,
		and removing the IN_OPEN setting.  The file is not discovered unless
		it can be opened so this is reliable.

		In WF_POLLING mode, this resets our knowledge of the stat
		info, and then triggers file activity to wake up the caller.
	"""
		log = self._getparam('log', self._discard, **params)
		if self._mode == WF_KQUEUE:
			try:
				ev = select.kevent(fd, filter=select.KQ_FILTER_TIMER,
							flags=select.KQ_EV_ADD | select.KQ_EV_CLEAR | select.KQ_EV_ONESHOT, data=0)
				self._kq.control([ev], 0, 0)
				log.debug("Added timer event following pending file promotion")
			except Exception as e:
				log.error("Failed to add timer event following pending file promotion -- %s", str(e))
		elif self._mode == WF_INOTIFYX:
			if fd in self.fds_open:
				try:
					path = self.fds_open[fd]
					nfd = inotifyx.add_watch(self._inx_fd, path, self._inx_mask|inotifyx.IN_OPEN)
					if nfd != fd:
						raise Exception("Assertion failed: IN_OPEN add_watch() set gave new wd")
					tfd = os.open(path, os.O_RDONLY)
					try: os.close(tfd)
					except: pass
					nfd = inotifyx.add_watch(self._inx_fd, path, self._inx_mask)
					if nfd != fd:
						raise Exception("Assertion failed: IN_OPEN add_watch() clear gave new wd")
				except Exception as e:
					log.error("Failed to trigger event via os.open() following pending file promotion -- %s",
											str(e))
			else:
				log.error("Pending file promotion of unknown wd %d failed", fd)
		elif self._mode == WF_POLLING:
			self._poll_stat[fd] = ()
			self._poll_trigger()
Exemple #16
0
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

    fd = inotifyx.init()

    wd_to_path = {}

    try:
        wd = inotifyx.add_watch(fd, watch_dir)
        wd_to_path[wd] = watch_dir

        print("wd_to_path: ", wd_to_path)
    except Exception:
        print("stopped")
        os.close(fd)
        sys.exit(1)

    create_thread.start()

    try:
        while True:
            events = inotifyx.get_events(fd)

            for event in events:
                path = wd_to_path[event.wd]
                event_type = event.get_mask_description()
                event_type_array = event_type.split("|")

                # remember dir name
                if "IN_ISDIR" in event_type_array and "IN_CREATE" in event_type:
                    new_dir = os.path.join(path, event.name)
                    wd = inotifyx.add_watch(fd, new_dir)
                    wd_to_path[wd] = new_dir

                print("PATH=[{}] FILENAME=[{}] EVENT_TYPES={}".format(
                    path, event.name, event_type_array))

    except KeyboardInterrupt:
        pass
    finally:
        os.close(fd)
Exemple #17
0
 def start ( self ):
   if self._run: return
   self._run = True
   self._id = inotifyx.init()
   self._wd = inotifyx.add_watch(self._id, self._conf['dev_path'],
                                 inotifyx.IN_CREATE | inotifyx.IN_DELETE)
   threading.Thread.start(self)
Exemple #18
0
def livepdf():

    import inotifyx, sys, os, time, subprocess
    sys.path.insert(0, sourcedir)
    from conf import basic_filename

    subprocess.call(
        ['xdg-open', builddir + 'latex/' + basic_filename + '.pdf'])

    fd = inotifyx.init()

    try:
        wd = inotifyx.add_watch(fd, sourcedir, inotifyx.IN_CLOSE_WRITE)
        try:
            while True:

                events = []
                events += inotifyx.get_events(fd, 0)
                time.sleep(1)
                events += inotifyx.get_events(fd, 0)

                names = set()
                for event in events:
                    if event.name and event.name[-3:-1] != 'sw' and event.name[
                            -1] != '!':
                        names.add(sourcedir + event.name)
                if len(names) > 0:
                    print('%s modified' % ','.join(names))
                    subprocess.call(['make', 'latexpdf'])

        except KeyboardInterrupt:
            pass

    finally:
        os.close(fd)
Exemple #19
0
    def __init__(self, watch_dir, n_files):
        super().__init__(watch_dir, n_files)
        self.wd_to_path = {}
        self.inotify_binding = inotifyx.init()

        wd = inotifyx.add_watch(self.inotify_binding, self.watch_dir)
        self.wd_to_path[wd] = self.watch_dir
	def run(self):
		# watch intel vbtn for event to know when to parse dmesg for what it was
		self.log.info("StanceWatcher started")
		vbtnWatchFd = inotifyx.init()
		try:
			self.log.debug("Add Watch for Intel Virtual Button input...")
			vbtnWatch = inotifyx.add_watch(vbtnWatchFd, Config['intelVbtnInput'], inotifyx.IN_ACCESS)
			
			lastEventTime = 0.0
			while not self.stopEv.is_set():
				vbtnEvent = inotifyx.get_events(vbtnWatchFd,0.8)
				if len(vbtnEvent) > 0:
					now = time.time();
					# vbtn event file is modified multiple times on a lid rotation, a quick solution to fire stance change only once
					# assuming the user won't rotate the lid very frequently (like under a second...)
					if now - lastEventTime > 0.8:
						self.log.debug("lid rotate event occurred, parse syslog...")
						time.sleep(0.2) #give time for event to appear in syslog
						readStance = self.parseStanceFromSyslog()
						if readStance:
							self.stance = readStance
							self.log.debug("Stance updated to %s", self.stance)
							self.changeEvent.set()
						else:
							self.log.warning("Got None, stance NOT updated.")
					else:
						self.log.debug("event discarded, too soon")
					lastEventTime = now
			inotifyx.rm_watch(vbtnWatchFd, vbtnWatch)
			self.log.debug("Removed watch for Intel Virtual Button input")
		finally:
			os.close(vbtnWatchFd)
Exemple #21
0
 def start(self):
     if self._run: return
     self._run = True
     self._id = inotifyx.init()
     self._wd = inotifyx.add_watch(self._id, self._conf['dev_path'],
                                   inotifyx.IN_CREATE | inotifyx.IN_DELETE)
     threading.Thread.start(self)
Exemple #22
0
def main_linux(data_dir):
    """Notify about events triggered base on inotify.

    Args:
        data_dir: The directory to watch.
    """
    from inotifyx import init, add_watch, get_events

    inotify_fd = init()
    wd_to_path = {}

    try:
        # get all subdirs
        # do not register right away because it will trigger events
        dirs = []
        for root, _, _ in os.walk(data_dir):
            dirs.append(root)

        for i in dirs:
            wd_to_path[add_watch(inotify_fd, i)] = i

        try:
            while True:
                events = get_events(inotify_fd)
                for event in events:
                    path = wd_to_path[event.wd]
                    parts = event.get_mask_description()

                    logging.info("%s: %s/%s", parts, path, event.name)

                    #                    is_created = ("IN_CREATE" in parts)
                    #                    is_dir = ("IN_ISDIR" in parts)
                    #                    is_closed = ("IN_CLOSE" in a_array
                    #                                 or "IN_CLOSE_WRITE" in a_array)

                    # if a new directory is created inside the monitored one,
                    # this one has to be monitored as well
                    #                    if is_created and is_dir and event.name:
                    if ("IN_CREATE" in parts and "IN_ISDIR" in parts
                            and event.name):
                        dirname = path + os.sep + event.name
                        wd_to_path[add_watch(inotify_fd, dirname)] = dirname

        except KeyboardInterrupt:
            pass
    finally:
        os.close(inotify_fd)
Exemple #23
0
 def __init__(self):
     super(IOWatcher, self).__init__()
     self.daemon = True
     self.last_update = datetime.datetime(1970,1,1)
     self.callbacks = []
     self.fd = inotifyx.init()
     self.wd = inotifyx.add_watch(self.fd, FILE_DIR)
     self.NOTABLE = inotifyx.IN_CLOSE_WRITE | inotifyx.IN_MOVE
Exemple #24
0
 def add_observer(self, observer):
     self._observers.append(observer)
     observer._wd = inotifyx.add_watch(
         self._fd,
         observer.dir,
         inotifyx.IN_CREATE | inotifyx.IN_MOVE | inotifyx.IN_DELETE
         | inotifyx.IN_ATTRIB,
     )
     self._wd[observer._wd] = observer
Exemple #25
0
    def install_watcher(self):
        self.watch_fd = inotifyx.init()
        for d in self.base_dirs:
            if os.path.isdir(d):
                logging.info('adding watch for {0}'.format(d))
                self.watches[inotifyx.add_watch(self.watch_fd, d,
                                                inotifyx.IN_MODIFY |
                                                inotifyx.IN_CREATE)] = d

        glib.timeout_add_seconds(3, self.do_watch)
Exemple #26
0
    def parse_args(self):
        # Fails if path is not a directory.
        if not os.path.isdir(self.params.path):
            raise gc3libs.exceptions.InvalidUsage("%s is not a directory", self.params.path)

        # We start the inotify as soon as possible, so that if a new
        # file is created between the execution of this function and
        # the every_main_loop() call (which is called *after* the main
        # loop), we don't lose the newly created file.
        self.ifd = inotifyx.init()
        self.iwatch = inotifyx.add_watch(self.ifd, self.params.path, inotifyx.IN_CLOSE_WRITE)
Exemple #27
0
 def ivisit(self):
     try:
         fd = inotifyx.init()
         wd = inotifyx.add_watch(fd, self.items[0], inotifyx.IN_CLOSE)
         self.urlvisit()
         inotifyx.get_events(fd, self.keep)
         inotifyx.rm_watch(fd, wd)
         os.close(fd)
     except IOError:
         hint = "consider increasing " "/proc/sys/fs/inotify/max_user_watches"
         raise util.DeadMan("failed to enable inotify", hint=hint)
    def ssh(self, cmd, output_catcher=ForwardToStd(),
            remotes_stdin=None):
        if not cmd:
            # XXX: Where do these empty commands come from?
            return
        def assert_master_openssh_running():
            if self.master_openssh.poll() == 255:
                raise Offline(self)

        ssh_master_socket = self.ssh_master_socket
        if not ssh_master_socket:
            ssh_master_socket_dir = on_exit_vanishing_dtemp()
            ssh_master_socket = join(ssh_master_socket_dir, 'socket')
            self.ssh_master_socket = ssh_master_socket
            fd = inotifyx.init()
            try:
                inotifyx.add_watch(fd, ssh_master_socket_dir)
                self.master_openssh = master_openssh = self.openssh(
                        ['-M', '-N']
                        + remote_authorized_key_env(),
                        [], stderr=PIPE)
                filter_masters_stderr_thread = threading.Thread(
                        target=self.filter_masters_stderr,
                        args=(master_openssh.stderr,)
                        )
                filter_masters_stderr_thread.daemon = True
                filter_masters_stderr_thread.start()
                # Wait for termination in the case the target is
                # not available:
                while True:
                    if inotifyx.get_events(fd, 0.1):
                        register(master_openssh.kill)
                        break
                    assert_master_openssh_running()
            finally:
                os.close(fd)
        cmd_openssh = self.openssh(output_catcher.allocate_tty, [cmd],
                remotes_stdin, output_catcher.remotes_stdout,
                output_catcher.remotes_stdout)
        communicate_with_child(cmd_openssh, output_catcher,
                assert_master_openssh_running, cmd)
Exemple #29
0
def add_watch_dirs(topdir, fd, mask):
    '''
    add inotify watch for every directory in tree starting at 'topdir'
    '''
    try:
        path = None
        return dict( (
            (inotifyx.add_watch(fd, path, mask), path)
            for path in find_dirs(topdir)
        ) )
    except IOError, exc:
        sys.exit('{}: {}'.format(path, exc))
Exemple #30
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 !!!*"""
  watchfd = inotifyx.init()
  inotifyx.add_watch(watchfd, directory, inotifyx.IN_CREATE)
  try:

    subfiles = set(os.listdir(directory))
    subfiles |= set([file_.name for file_ in inotifyx.get_events(watchfd, 0)])

    while True:
      for file_ in subfiles:
        yield os.path.join(directory, file_)

      subfiles = [file_.name for file_ in inotifyx.get_events(watchfd)]

  finally:
    os.close(watchfd)
Exemple #31
0
 def ivisit(self):
     try:
         fd = inotifyx.init()
         wd = inotifyx.add_watch(fd, self.items[0], inotifyx.IN_CLOSE)
         self.urlvisit()
         inotifyx.get_events(fd, self.keep)
         inotifyx.rm_watch(fd, wd)
         os.close(fd)
     except IOError:
         hint = ('consider increasing '
                 '/proc/sys/fs/inotify/max_user_watches')
         raise util.DeadMan('failed to enable inotify', hint=hint)
Exemple #32
0
    def __init__(self, url, mask, recurse=False, **kw):
        Poller.__init__(self, url, mask, **kw)

        self._recurse = recurse
        self._ifds = {}
        # Ensure inbox directory exists
        if not os.path.exists(self.url.path):
            log.warning("Inbox directory `%s` does not exist,"
                        " creating it.", self.url.path)
            os.makedirs(self.url.path)

        ifd = inotifyx.init()
        inotifyx.add_watch(ifd, self.url.path, self.mask)
        log.debug("Adding watch for path %s" % self.url.path)
        self._ifds[self.url.path] = ifd

        if self._recurse:
            for dirpath, dirnames, filename in os.walk(self.url.path):
                for dirname in dirnames:
                    abspath = os.path.join(self.url.path, dirpath, dirname)
                    log.debug("Adding watch for path %s" % abspath)
                    self._add_watch(abspath)
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 !!!*"""
    watchfd = inotifyx.init()
    inotifyx.add_watch(watchfd, directory, inotifyx.IN_CREATE)
    try:

        subfiles = set(os.listdir(directory))
        subfiles |= set(
            [file_.name for file_ in inotifyx.get_events(watchfd, 0)])

        while True:
            for file_ in subfiles:
                yield os.path.join(directory, file_)

            subfiles = [file_.name for file_ in inotifyx.get_events(watchfd)]

    finally:
        os.close(watchfd)
Exemple #34
0
def ino_watch(file_to_watch, action, *action_args):
    ''' ``inotify``-based watcher, applying function upon
        *write-and-close* events '''
    watcher = inotifyx.init()
    #TODO: implement check like "it's a directory and you don't run a cmd!"
    if not os.path.isdir(file_to_watch):
        dirname = os.path.dirname(file_to_watch) or '.'
        basename = os.path.basename(file_to_watch)
    else:
        dirname = file_to_watch
        basename = None
    # we watch for CLOSE_WRITE events in directory and filter them by file name
    # because editors like vim do save&rename instead of simple modification
    inotifyx.add_watch(watcher, dirname, inotifyx.IN_CLOSE_WRITE)
    # wrap action to avoid code duplication
    action_lambda = lambda: action(file_to_watch, *action_args)
    # run the first time
    action_lambda()
    while True:
        events = inotifyx.get_events(watcher)
        if (basename is None) or (basename in (ev.name for ev in events)):
            action_lambda()
Exemple #35
0
    def __init__(self, url, mask, recurse=False, **kw):
        Poller.__init__(self, url, mask, **kw)

        self._recurse = recurse
        self._ifds = {}
        # Ensure inbox directory exists
        if not os.path.exists(self.url.path):
            log.warning("Inbox directory `%s` does not exist,"
                        " creating it.", self.url.path)
            os.makedirs(self.url.path)

        ifd = inotifyx.init()
        inotifyx.add_watch(ifd, self.url.path, self.mask)
        log.debug("Adding watch for path %s" % self.url.path)
        self._ifds[self.url.path] = ifd

        if self._recurse:
            for dirpath, dirnames, filename in os.walk(self.url.path):
                for dirname in dirnames:
                    abspath = os.path.join(self.url.path,
                                           dirpath,
                                           dirname)
                    log.debug("Adding watch for path %s" % abspath)
                    self._add_watch(abspath)
Exemple #36
0
    def tail(self):
        fd = inotifyx.init()
        try:
            wd = inotifyx.add_watch(fd, '/var/log/emerge.log', inotifyx.IN_MODIFY)

            self.log_currentcompile(1) # only wait for a second for the sandbox

            while True:
		self.log_heartbeat()
                events = inotifyx.get_events(fd, float(self.nap))
                if len(events) != 0: # not timeout
                    self.log_currentcompile(self.sandboxwait)

            inotifyx.rm_watch(fd, wd)
        finally:
            os.close(fd)
Exemple #37
0
  def start ( self ):
    self.notify('Started', 'BrytonSync started')
    if self._run: return

    # Create dirs
    tdir = os.path.expanduser(self._conf['track_dir'])
    if not os.path.exists(tdir):
      os.makedirs(tdir)

    # Setup inotify
    self._run = True
    self._id  = inotifyx.init()
    self._wd  = inotifyx.add_watch(self._id, tdir)

    # Start device monitor
    self._devmon.start()
    if not self._conf['nosync']: threading.Thread.start(self)
Exemple #38
0
    def _add_watch(self):
        """Add directories to inotify watch.

        Adds all existing directories found inside the source paths to the
        inotify watch.
        """

        for path in self._get_directory_structure():
            try:
                watch_descriptor = inotifyx.add_watch(self.file_descriptor,
                                                      path)
                self.wd_to_path[watch_descriptor] = path
                self.log.debug("Register watch for path: %s", path)
            except Exception:
                self.log.error("Could not register watch for path: %s",
                               path,
                               exc_info=True)
Exemple #39
0
    def watch(self):

        fd = inotifyx.init()
        wd = inotifyx.add_watch(fd, self.pickup_dir, inotifyx.constants["IN_CLOSE_WRITE"])

        keep_running = True

        while keep_running:
            try:
                events = inotifyx.get_events(fd, 1.0)
                for event in events:
                    self.pickup_event_processor.process(event)

                sleep(0.1)
            except KeyboardInterrupt:
                keep_running = False

        os.close(fd)
Exemple #40
0
    def __iter__(self):
        """Iterating a monitor returns the next set of changed files.

        When requesting the next item from a monitor it will block
        until file changes are detected and then return the set of
        changed files.
        """

        while True:
            # Block until events arrive.
            events = inotifyx.get_events(self.fd)

            # Collect any events that occur within the delay period.
            # This allows events that occur close to the trigger event
            # to be collected now rather than causing another run
            # immediately after this run.
            if self.delay:
                time.sleep(self.delay)
                events.extend(inotifyx.get_events(self.fd, 0))

            # Filter to events that are white listed.
            events = [
                event for event in events if self.is_white_listed(event.name)
            ]

            if events:
                # Track watched dirs.
                for event in events:
                    if event.mask & inotifyx.IN_ISDIR and event.mask & inotifyx.IN_CREATE:
                        self.watches[inotifyx.add_watch(
                            self.fd,
                            os.path.join(self.watches.get(event.wd),
                                         event.name),
                            self.WATCH_EVENTS)] = os.path.join(
                                self.watches.get(event.wd), event.name)
                    elif event.mask & inotifyx.IN_DELETE_SELF:
                        self.watches.pop(event.wd, None)

                # Supply this set of changes to the caller.
                change_set = set(
                    os.path.join(self.watches.get(event.wd, ''), event.name
                                 or '') for event in events)
                yield change_set
def _main():
    watch_dir = "/tmp/watch_tree"

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

    fd = inotifyx.init()

    wd_to_path = {}

    try:
        wd = inotifyx.add_watch(fd, watch_dir)
        wd_to_path[wd] = watch_dir
        print("wd_to_path: ", wd_to_path)
    except Exception as excp:
        print("stopped")
        print("Exception was", excp)
        os.close(fd)
        sys.exit(1)

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

    try:
        while True:
            events = inotifyx.get_events(fd)

            for event in events:
                path = wd_to_path[event.wd]
                event_type = event.get_mask_description()
                event_type_array = event_type.split("|")

                print("PATH=[{}] FILENAME=[{}] EVENT_TYPES={}".format(
                    path, event.name, event_type_array))

    except KeyboardInterrupt:
        pass
    finally:
        os.close(fd)
Exemple #42
0
	def __init__(self, directory):
		gobject.GObject.__init__(self)
		self.directory = gio.File(directory)
		self.objects = {}
		if not self.directory.query_exists(None):
			raise RuntimeError("%s does not exist!" % directory)
		self.monitor_fd = None

		## inotify file monitor
		if inotifyx:
			fd = inotifyx.init()
			ix = inotifyx
			wd = inotifyx.add_watch(fd, self.directory.get_path(),
			                        ix.IN_CLOSE_WRITE | ix.IN_DELETE |
			                        ix.IN_MODIFY)
			glib.io_add_watch(fd, glib.IO_IN, self.on_inotifyx, fd)
			self.monitor_fd = fd
		self.monitor = self.directory.monitor_directory()
		if self.monitor is not None:
			self.monitor.connect("changed", self.on_file_monitor_changed)
Exemple #43
0
def watchFiles(queue):
    mediaFound = False
    fd = inotifyx.init()
    wd = inotifyx.add_watch(fd, watchPath, inotifyx.IN_CLOSE)

    while (1):
	# Wait for an event with timeout.
	event = inotifyx.get_events(fd, eventTimeout)
	print("Event caught, or timed-out.")

	# Wait before reading files
	time.sleep(readDelay)

	for fname in os.listdir(watchPath):
	    fpath = os.path.join(watchPath, fname)
	    if (os.path.isfile(fpath) and fname.startswith(watchFilePrefix)
			and fname.endswith(watchFileSuffix)):
		mediaFound = False
		print ("Processing file: " + fpath)
		f = open(fpath, "r")

		for line in f:
		    pieces = shlex.split(line.strip())
		    for p in pieces:
			queue.put(p)
			print ("Found: " + p)
			mediaFound = True
		f.close()

		# Only remove the file if we found something in it
		if(mediaFound):
		    os.remove(fpath)
		    print("Deleting file.")

	# Drain events from file operations.
	e = inotifyx.get_events(fd, 0)
	while e:
	    e = inotifyx.get_events(fd, 0)

    inotifyx.rm_watch(fd, wd)
    os.close(fd)
Exemple #44
0
def _wait_files_creation(file_list):
  # Etablish a list of directory and subfiles
  directories = dict()
  for dirname, filename in [os.path.split(f) for f in file_list]:
    directories.setdefault(dirname, dict())
    directories[dirname][filename] = False

  def all_files_exists():
    return all([all(files.values()) for files in directories.values()])

  fd = inotifyx.init()
  try:
    # Watch every directories where the file are
    watchdescriptors = dict()
    for dirname in directories.keys():
      wd = inotifyx.add_watch(fd,
                              dirname,
                              inotifyx.IN_CREATE | inotifyx.IN_DELETE)
      watchdescriptors[wd] = dirname

    # Set to True the file wich exists
    for dirname, filename in [os.path.split(f) for f in file_list]:
      directories[dirname][filename] = os.path.exists(os.path.join(dirname,
                                                                   filename))
    # Let's wait for every file creation
    while not all_files_exists():
      events_list = inotifyx.get_events(fd)
      for event in events_list:
        dirname = watchdescriptors[event.wd]
        if event.name in directories[dirname]:
          # One of watched file was created or deleted
          if event.mask & inotifyx.IN_DELETE:
            directories[dirname][event.name] = False
          else:
            directories[dirname][event.name] = True

  finally:
    os.close(fd)
def _wait_files_creation(file_list):
    # Etablish a list of directory and subfiles
    directories = dict()
    for dirname, filename in [os.path.split(f) for f in file_list]:
        directories.setdefault(dirname, dict())
        directories[dirname][filename] = False

    def all_files_exists():
        return all([all(files.values()) for files in directories.values()])

    fd = inotifyx.init()
    try:
        # Watch every directories where the file are
        watchdescriptors = dict()
        for dirname in directories.keys():
            wd = inotifyx.add_watch(fd, dirname,
                                    inotifyx.IN_CREATE | inotifyx.IN_DELETE)
            watchdescriptors[wd] = dirname

        # Set to True the file wich exists
        for dirname, filename in [os.path.split(f) for f in file_list]:
            directories[dirname][filename] = os.path.exists(
                os.path.join(dirname, filename))
        # Let's wait for every file creation
        while not all_files_exists():
            events_list = inotifyx.get_events(fd)
            for event in events_list:
                dirname = watchdescriptors[event.wd]
                if event.name in directories[dirname]:
                    # One of watched file was created or deleted
                    if event.mask & inotifyx.IN_DELETE:
                        directories[dirname][event.name] = False
                    else:
                        directories[dirname][event.name] = True

    finally:
        os.close(fd)
    def __iter__(self):
        """Iterating a monitor returns the next set of changed files.

        When requesting the next item from a monitor it will block
        until file changes are detected and then return the set of
        changed files.
        """

        while True:
            # Block until events arrive.
            events = inotifyx.get_events(self.fd)

            # Collect any events that occur within the delay period.
            # This allows events that occur close to the trigger event
            # to be collected now rather than causing another run
            # immediately after this run.
            if self.delay:
                time.sleep(self.delay)
                events.extend(inotifyx.get_events(self.fd, 0))

            # Filter to events that are white listed.
            events = [event for event in events if self.is_white_listed(event.name)]

            if events:
                # Track watched dirs.
                for event in events:
                    if event.mask & inotifyx.IN_ISDIR and event.mask & inotifyx.IN_CREATE:
                        self.watches[inotifyx.add_watch(self.fd, os.path.join(self.watches.get(event.wd), event.name), self.WATCH_EVENTS)] = os.path.join(self.watches.get(event.wd), event.name)
                    elif event.mask & inotifyx.IN_DELETE_SELF:
                        self.watches.pop(event.wd, None)

                # Supply this set of changes to the caller.
                change_set = set(os.path.join(self.watches.get(event.wd, ''),
                                              event.name or '')
                                 for event in events)
                yield change_set
Exemple #47
0
 def _add_watch(self, path):
     if path not in self._ifds:
         log.debug("Adding watch for path %s" % path)
         ifd = inotifyx.init()
         inotifyx.add_watch(ifd, path, self.mask)
         self._ifds[path] = ifd
Exemple #48
0
					print "Error build"
					#sys.exit(0)
			else:
				self.start()


if __name__ == '__main__':
	dub = DUB()

	if not dub.build():
		sys.exit(1)

	try:
		dub.start()
		fd = inotifyx.init()
		inotifyx.add_watch(fd, SOURCE_PATH, binding.IN_MODIFY)
		inotifyx.add_watch(fd, TEMPLATE_PATH, binding.IN_MODIFY)

		start_time = datetime.now()
		while True:
			events = inotifyx.get_events(fd)
			end_time = datetime.now()
			isrestart = False
			if (end_time - start_time) > TIME_INTERVAL:
				for event in events:
					if event.name and event.name.split(".")[-1] in EXTENSIONS:
						isrestart = True
			start_time = end_time
			if isrestart:
				dub.restart(True)
    def dump(self):
        changed = False
        for weekid, week in self.weeks.iteritems():
            if not week.changed:
                continue
            writeJSON(os.path.join(OUTDIR, weekid + ".json"), week.dump())
            changed = True

        if changed:
            writeJSON(os.path.join(OUTDIR, "weeks.json"), self.weeks.keys())

[LOG, OUTDIR] =  sys.argv[1:]
weeks = Weeks(OUTDIR)
ifd = inotifyx.init()
log = inotifyx.add_watch(ifd, LOG, inotifyx.IN_MODIFY|inotifyx.IN_ATTRIB|inotifyx.IN_DELETE_SELF|inotifyx.IN_MOVE_SELF)
f = open(LOG)
sessions = set()
prefix = ''
while True:
    line = f.readline()
    if line == '':
        weeks.dump()
        # we've reached end of log, inotify avoids polling for growth
        # suspend if after 60seconds after activity in auth log stopped, there are still 0 ssh sessions
        ls = []
        while len(ls) == 0:
            ls = inotifyx.get_events(ifd, 60)
            if len(ls) + len(sessions) == 0:
                """nothing changed in 60s"""
                continue
Exemple #50
0
miCliente = interfazGDrive()
nom_fichero = "registro_drive"

if not os.path.exists(ruta):
    print("La ruta expecificada en el fichero de configuracion no existe.")
    exit()

#Guarda los datos de GDrive en el fichero local al principio del programa
lista_archivos = miCliente.getListaArchivos()
guardarEnFichero(lista_archivos, nom_fichero)

#Ciclo principal
fd = inotifyx.init()
while True:
    try:
        wd = inotifyx.add_watch(fd, ruta, inotifyx.IN_CREATE)
        events = inotifyx.get_events(fd)
        archivos_a_subir = comprobarCambios(ruta, nom_fichero)
        if archivos_a_subir:
            print("Se subiran estos archivos: ")
            print(archivos_a_subir)
            #Sube cada archivo de la lista a Drive
            for i in archivos_a_subir:
                ruta_archivo = os.path.join(ruta, i)
                miCliente.subirArchivo(i, ruta_archivo, tipoArchivo(i))
                print(
                    str(archivos_a_subir.index(i) + 1) + ") " + i +
                    ": subido.")
            #Registra los cambios en el fichero
            lista_archivos += archivos_a_subir
            guardarEnFichero(lista_archivos, nom_fichero)
Exemple #51
0
def xzCompress(inputFile, outputFile):
    # Compresses a file, that may be actively written to, with xz.
    # Returns the file name on success, or None on failure

    # command uses custom streaming build of xz
    xzCommand = "export LD_LIBRARY_PATH=/usr/local/lib; /usr/local/bin/xz2 -z1 > %s" % (outputFile)
    # xzCommand = "/usr/local/bin/xz -z1 | pv -B 1024 -L 100 > %s" % (outputFile)

    IN_WATCH_EVENTS = inotifyx.IN_MODIFY

    try:
        # Sets up the main inotify watcher
        fd = inotifyx.init()
        watcher = inotifyx.add_watch(fd, inputFile, IN_WATCH_EVENTS)
        with io.open(inputFile, mode='r+b') as fileStream:
            # Loop until no more data
            try:
                xzp = subprocess.Popen(
                    xzCommand, stdin=subprocess.PIPE, shell=True,
                    close_fds=False, preexec_fn=subprocessSetup)

                # Counter for retrys
                trycount = 0
                while 1:
                    # Main loop which reads the file and writes to xz stdin
                    data = fileStream.read(1024000)
                    current = False
                    # Assume reading a normal file until we get to the end

                    if len(data) == 0:
                        current = currentFile(inputFile, fd)
                        if not current:
                            # Reached EOF, check next file exists
                            sleep(0.1)  # Prevent race condition

                            if nextFile(inputFile) is not None:
                                logger.debug("Breaking, next file exists!")
                                break

                            trycount += 1
                            logger.debug("Waiting for next file or more data.."
                                         + str(trycount))
                            sleep(1)

                    logger.debug("Writing %s" % len(data))
                    xzp.stdin.write(data)

                    if current:
                        # Reduce looping, wait a bit for more data
                        sleep(0.5)
            except(KeyboardInterrupt, SystemExit):
                raise
            finally:
                xzp.stdin.flush()
                xzp.stdin.close()

            position = fileStream.tell()

        inotifyx.rm_watch(fd, watcher)
    finally:
        os.close(fd)

    # Get return code
    xzp.wait()
    if xzp.returncode is not 0:
        logger.error("xz gave non-zero exit status")
        return None
    # logger.debug("xz return code: %s" % (returnCode))

    # Check new compressed file exists (before this we don't *actually* know
    # it does because it's a shell redirect)
    try:
        with open(outputFile):
            pass
    except IOError:
        logger.error("Failed to create xz file")
        return None

    return (outputFile, position)
Exemple #52
0
# The 5 music selection buttons
b1 = TardisButton(11, m)
b2 = TardisButton(13, m)
b3 = TardisButton(12, m)
b4 = TardisButton(16, m)
b5 = TardisButton(18, m)

# Setup some watches...
# Sound files
mask = inotifyx.IN_CLOSE_WRITE | inotifyx.IN_CREATE | \
       inotifyx.IN_DELETE | inotifyx.IN_MODIFY | inotifyx.IN_MOVE | \
       inotifyx.IN_MOVED_TO

for b in range(1,6):
    inotifyx.add_watch(fd, os.path.join(dir, "Sounds", str(b)), mask)

# Volume
inotifyx.add_watch(fd, os.path.join(dir, "Sounds", "Volume", "Level.txt"),  mask)


# The 10mm LEDs: White, Red, Green, Blue

# Firstly, do we have a config file?
configFile = os.path.join(dir, "Sounds", "10mm.json")
if glob.glob(configFile):
    cf = open(configFile, "rb")
    settings = json.load(cf)
    cf.close()
    white = settings['white']
    red   = settings['red']
Exemple #53
0
 def _add (self, path):
     return inotifyx.add_watch (self.fd, path,
             inotifyx.IN_MODIFY | inotifyx.IN_CREATE | inotifyx.IN_DELETE | inotifyx.IN_MOVE
             )
Exemple #54
0
 elif watch_symlink != '':
     # If we are tasked with watching a specific symlink for changes then
     # we do it here. get_events will be interrupted by a SIGHUP signal
     # which we can catch to start a deploy. A return from get_events will
     # also be evaluated and if the file has changed then a deploy will be
     # started.
     import inotifyx
     dirname, filename = os.path.split(watch_symlink)
     fd = inotifyx.init()
     try:
         # Watch the directory, if it changes then we need to see if the
         # change was in the file we specifically care about. While we
         # have the watch in place we compare the symlink to what was last
         # deployed, if it is different then we don't bother waiting, we
         # just set start_deploy and then loop.
         wd = inotifyx.add_watch(fd, dirname, inotifyx.IN_DELETE)
         if deployed_symlink_value != read_symlink():
             log.info('Symlink changed from "%s" to "%s", deploying.',
                      deployed_symlink_value, read_symlink())
             start_deploy = True
         else:
             events = inotifyx.get_events(fd, 24 * 60 * 60)
             if deployed_symlink_value != read_symlink():
                 log.info('Symlink changed from "%s" to "%s", deploying.',
                          deployed_symlink_value, read_symlink())
                 start_deploy = True
         inotifyx.rm_watch(fd, wd)
     except IOError as e:
         if e.errno == 2:
             # Errno2 is thrown when the file we are attempting to watch
             # does not exist. If this happens then we fall back to a
def main():
    '''
    The main function
    '''    
    try:
        if len(sys.argv) < 2: 
            print_help()
        else:
            command = sys.argv[1]
            # Start the system
            if command == "start":
                # Set keyboard to use
                steelsquid_utils.execute_system_command_blind(["/usr/bin/termfix", steelsquid_utils.get_parameter("keyboard")], wait_for_finish=True)
                # Redirect sys.stdout to shout
                sys.stdout = Logger()
                # Create the task event dir 
                steelsquid_utils.make_dirs(system_event_dir)
                # Print welcome message
                steelsquid_utils.shout("Steelsquid Kiss OS "+steelsquid_utils.steelsquid_kiss_os_version()[1], to_lcd=False, wait_for_finish=False)
                # Use locking on the I2C bus
                if steelsquid_utils.get_flag("i2c_lock"):
                    steelsquid_i2c.enable_locking(True)
                else:
                    steelsquid_i2c.enable_locking(False)
                # Disable the monitor
                if steelsquid_utils.get_flag("disable_monitor"):
                    steelsquid_utils.execute_system_command_blind(["/opt/vc/bin/tvservice", "-o"], wait_for_finish=False)
                # Listen for shutdown on GPIO
                if steelsquid_utils.has_parameter("power_gpio"):
                    gpio = steelsquid_utils.get_parameter("power_gpio")
                    steelsquid_utils.shout("Listen for clean shutdown on GPIO " + gpio)
                    steelsquid_pi.gpio_click(gpio, poweroff, steelsquid_pi.PULL_DOWN)
                # Load all modules
                pkgpath = os.path.dirname(modules.__file__)
                for name in pkgutil.iter_modules([pkgpath]):
                    if steelsquid_utils.get_flag("module_"+name[1]):
                        steelsquid_utils.shout("Load module: " +name[1], debug=True)    
                        n = name[1]
                        steelsquid_kiss_global.loaded_modules[n]=import_module('modules.'+n)
                # Enable the download manager
                if steelsquid_utils.get_flag("download"):
                    if steelsquid_utils.get_parameter("download_dir") == "":
                        steelsquid_utils.set_parameter("download_dir", "/root")
                    steelsquid_utils.execute_system_command_blind(['steelsquid', 'download-on'], wait_for_finish=False)
                # Enable NRF24L01+ as server
                if steelsquid_utils.get_flag("nrf24_server"):
                    steelsquid_utils.shout("Enable NRF24L01+ server")
                    steelsquid_nrf24.server()
                    thread.start_new_thread(nrf24_server_thread, ())
                # Enable NRF24L01+ as client
                elif steelsquid_utils.get_flag("nrf24_client"):
                    steelsquid_utils.shout("Enable NRF24L01+ client")
                    steelsquid_nrf24.client()
                # Enable NRF24L01+ as master
                if steelsquid_utils.get_flag("nrf24_master"):
                    steelsquid_utils.shout("Enable NRF24L01+ master")
                    steelsquid_nrf24.master(nrf24_callback)
                # Enable NRF24L01+ as slave
                elif steelsquid_utils.get_flag("nrf24_slave"):
                    steelsquid_utils.shout("Enable NRF24L01+ slave")
                    steelsquid_nrf24.slave()
                    thread.start_new_thread(nrf24_slave_thread, ())
                # Enable HM-TRLR-S as server
                if steelsquid_utils.get_flag("hmtrlrs_server"):
                    config_gpio = int(steelsquid_utils.get_parameter("hmtrlrs_config_gpio", "25"))
                    reset_gpio = int(steelsquid_utils.get_parameter("hmtrlrs_reset_gpio", "23"))
                    steelsquid_utils.shout("Enable HM-TRLR-S server")
                    steelsquid_hmtrlrs.setup(config_gpio=config_gpio, reset_gpio=reset_gpio)
                    thread.start_new_thread(hmtrlrs_server_thread, ())
                # Enable HM-TRLR-S as client
                elif steelsquid_utils.get_flag("hmtrlrs_client"):
                    config_gpio = int(steelsquid_utils.get_parameter("hmtrlrs_config_gpio", "25"))
                    reset_gpio = int(steelsquid_utils.get_parameter("hmtrlrs_reset_gpio", "23"))
                    steelsquid_utils.shout("Enable HM-TRLR-S client")
                    steelsquid_hmtrlrs.setup(config_gpio=config_gpio, reset_gpio=reset_gpio)
                    thread.start_new_thread(hmtrlrs_client_thread, ())
                # Start the modules
                for obj in steelsquid_kiss_global.loaded_modules.itervalues():
                    thread.start_new_thread(import_file_dyn, (obj,)) 
                # Enable the webserver
                if steelsquid_utils.get_flag("web"):
                    port = None
                    if steelsquid_utils.has_parameter("web_port"):
                        port = steelsquid_utils.get_parameter("web_port")
                    steelsquid_kiss_global.http_server = steelsquid_kiss_http_server.SteelsquidKissHttpServer(port, steelsquid_utils.STEELSQUID_FOLDER+"/web/", steelsquid_utils.get_flag("web_authentication"), steelsquid_utils.get_flag("web_local"), steelsquid_utils.get_flag("web_authentication"), steelsquid_utils.get_flag("web_https"))
                    for obj in steelsquid_kiss_global.loaded_modules.itervalues():
                        if hasattr(obj, "WEB"):
                            steelsquid_kiss_global.http_server.external_objects.append(getattr(obj, "WEB"))
                    steelsquid_kiss_global.http_server.start_server()
                # Enable the socket server as server
                if steelsquid_utils.get_flag("socket_server"):
                    steelsquid_kiss_global.socket_connection = steelsquid_kiss_socket_connection.SteelsquidKissSocketConnection(True)
                    for obj in steelsquid_kiss_global.loaded_modules.itervalues():
                        if hasattr(obj, "SOCKET"):
                            steelsquid_kiss_global.socket_connection.external_objects.append(getattr(obj, "SOCKET"))
                    steelsquid_kiss_global.socket_connection.start()
                # Enable the socket server as client
                elif steelsquid_utils.has_parameter("socket_client"):
                    steelsquid_kiss_global.socket_connection = steelsquid_kiss_socket_connection.SteelsquidKissSocketConnection(False, steelsquid_utils.get_parameter("socket_client"))
                    for obj in steelsquid_kiss_global.loaded_modules.itervalues():
                        if hasattr(obj, "SOCKET"):
                            steelsquid_kiss_global.socket_connection.external_objects.append(getattr(obj, "SOCKET"))
                    steelsquid_kiss_global.socket_connection.start()
                # Enable the bluetooth
                if steelsquid_utils.get_flag("bluetooth_pairing"):
                    if not steelsquid_utils.has_parameter("bluetooth_pin"):
                        steelsquid_utils.set_parameter("bluetooth_pin", "1234")
                    thread.start_new_thread(bluetooth_agent, ()) 
                fd = inotifyx.init()
                inotifyx.add_watch(fd, system_event_dir, inotifyx.IN_CLOSE_WRITE)
                # Execute a network event so the IP is shown
                execute_task_event("network")
                # Delete old stop eventa and execute others...
                for f in os.listdir(system_event_dir):
                    if f=="stop" or f=="shutdown":
                        steelsquid_utils.deleteFileOrFolder(system_event_dir+"/"+f)
                    else:
                        read_task_event(f)
                # Listen for events
                try:
                    while running:
                        events = inotifyx.get_events(fd)
                        for event in events:
                            read_task_event(event.name)
                except KeyboardInterrupt:
                    pass
                os.close(fd)
                _cleanup()
                # Delete old eventa
                for f in os.listdir(system_event_dir):
                    steelsquid_utils.deleteFileOrFolder(system_event_dir+"/"+f)
            # Broadcast the event
            else:
                if len(sys.argv)>2:
                    broadcast_task_event(command, sys.argv[2:])
                else:
                    broadcast_task_event(command)
    except:
        steelsquid_utils.shout("Fatal error when on boot steelsquid service", is_error=True)
        os._exit(0)
Exemple #56
0
    def get_new_event(self):
        """Implementation of the abstract method get_new_event.

        Returns:
            A list of event messages generated from inotify events.
        """

        remaining_events = self.get_remaining_events()

        # only take the events which are not handles yet
        event_message_list = [
            event for event in remaining_events if [
                os.path.join(event["source_path"], event["relative_path"]),
                event["filename"]
            ] not in self.history
        ]
        self.history += [[
            os.path.join(event["source_path"], event["relative_path"]),
            event["filename"]
        ] for event in remaining_events]

        # event_message_list = self.get_remaining_events()
        event_message = {}

        events = inotifyx.get_events(self.file_descriptor, self.timeout)
        removed_wd = None

        for event in events:

            if not event.name:
                continue

            try:
                path = self.wd_to_path[event.wd]
            except Exception:
                path = removed_wd
            parts = event.get_mask_description()
            parts_array = parts.split("|")

            is_dir = ("IN_ISDIR" in parts_array)
            is_created = ("IN_CREATE" in parts_array)
            is_moved_from = ("IN_MOVED_FROM" in parts_array)
            is_moved_to = ("IN_MOVED_TO" in parts_array)

            current_mon_event = None
            current_mon_regex = None
            for key, value in iteritems(self.mon_regex_per_event):
                if key in parts_array:
                    current_mon_event = key
                    current_mon_regex = value
#                    current_mon_regex = self.mon_regex_per_event[key]

#            if not is_dir:
#                self.log.debug("{} {} {}".format(path, event.name, parts))
#                self.log.debug("current_mon_event: {}"
#                               .format(current_mon_event))
#            self.log.debug(event.name)
#            self.log.debug("is_dir: {}".format(is_dir))
#            self.log.debug("is_created: {}".format(is_created))
#            self.log.debug("is_moved_from: {}".format(is_moved_from))
#            self.log.debug("is_moved_to: {}".format(is_moved_to))

# if a new directory is created or a directory is renamed inside
# the monitored one, this one has to be monitored as well
            if is_dir and (is_created or is_moved_to):

                # self.log.debug("is_dir and is_created: {} or is_moved_to: "
                #                "{}".format(is_created, is_moved_to))
                # self.log.debug("{} {} {}".format(path, event.name, parts)
                # self.log.debug(event.name)

                dirname = os.path.join(path, event.name)
                self.log.info("Directory event detected: %s, %s", dirname,
                              parts)
                if dirname in self.paths:
                    self.log.debug(
                        "Directory already contained in path list:"
                        " %s", dirname)
                else:
                    watch_descriptor = inotifyx.add_watch(  # noqa E501
                        self.file_descriptor, dirname)
                    self.wd_to_path[watch_descriptor] = dirname
                    self.log.info("Added new directory to watch: %s", dirname)

                    # because inotify misses subdirectory creations if they
                    # happen to fast, the newly created directory has to be
                    # walked to get catch this misses
                    # http://stackoverflow.com/questions/15806488/
                    #        inotify-missing-events
                    for dirpath, directories, files in os.walk(dirname):
                        # Add the found dirs to the list for the inotify-watch
                        for dname in directories:
                            subdir = os.path.join(dirpath, dname)
                            watch_descriptor = inotifyx.add_watch(
                                self.file_descriptor, subdir)
                            self.wd_to_path[watch_descriptor] = subdir
                            self.log.info(
                                "Added new subdirectory to watch: "
                                "%s", subdir)
                        self.log.debug("files: %s", files)

                        for filename in files:
                            # self.log.debug("filename: {}".format(filename))
                            # pylint: disable=no-member
                            if self.mon_regex.match(filename) is None:
                                self.log.debug(
                                    "File does not match monitored "
                                    "regex: %s", filename)
                                self.log.debug("detected events were: %s",
                                               parts)
                                continue

                            event_message = get_event_message(
                                dirpath, filename, self.paths)
                            self.log.debug("event_message: %s", event_message)
                            event_message_list.append(event_message)
#                            self.log.debug("event_message_list: {}"
#                                           .format(event_message_list))
                continue

            # if a directory is renamed the old watch has to be removed
            if is_dir and is_moved_from:

                # self.log.debug("is_dir and is_moved_from")
                # self.log.debug("{} {} {}".format(path, event.name, parts)
                # self.log.debug(event.name)

                dirname = os.path.join(path, event.name)
                found_watch = None
                for watch, watch_path in iteritems(self.wd_to_path):
                    if watch_path == dirname:
                        found_watch = watch
                        break
                inotifyx.rm_watch(self.file_descriptor, found_watch)
                self.log.info("Removed directory from watch: %s", dirname)
                # the IN_MOVE_FROM event always apears before the IN_MOVE_TO
                # (+ additional) events and thus has to be stored till loop
                # is finished
                removed_wd = self.wd_to_path[found_watch]
                # removing the watch out of the dictionary cannot be done
                # inside the loop (would throw error: dictionary changed size
                # during iteration)
                del self.wd_to_path[found_watch]
                continue

            # only files of the configured event type are send
            if (not is_dir and current_mon_event
                    and [path, event.name] not in self.history):

                # only files matching the regex specified with the current
                # event are monitored
                if current_mon_regex.match(event.name) is None:
                    # self.log.debug("File ending not in monitored Suffixes: "
                    #                "%s", event.name)
                    # self.log.debug("detected events were: %s", parts)
                    continue

                event_message = get_event_message(path, event.name, self.paths)
                self.log.debug("event_message %s", event_message)
                event_message_list.append(event_message)

                self.history.append([path, event.name])

        return event_message_list
Exemple #57
0
#!/usr/bin/python
import inotifyx, re, syslog, os
"""
TODO: Deal with log rotation, 9-5 no sleep schedule, xbmc/vlc(via psutil?) no sleep
"""
LOG = "/var/log/auth.log"
ifd = inotifyx.init()
log = inotifyx.add_watch(
    ifd, LOG, inotifyx.IN_MODIFY | inotifyx.IN_ATTRIB | inotifyx.IN_DELETE_SELF
    | inotifyx.IN_MOVE_SELF)
f = open(LOG)
sessions = set()
while True:
    line = f.readline()
    if line == '':
        # we've reached end of log, inotify avoids polling for growth
        # suspend if after 60seconds after activity in auth log stopped, there are still 0 ssh sessions
        ls = []
        while len(ls) == 0:
            ls = inotifyx.get_events(ifd, 60)
            # sometimes the log is missing an entry
            # make sure that we only wait for live processes
            for pid in list(sessions):
                if not os.path.exists("/proc/%s" % pid):
                    print "Removing stale pid %s" % pid
                    sessions.remove(pid)
            if len(ls) + len(sessions) == 0:
                syslog.syslog("No more ssh sessions, going to sleep")
                os.system("/home/taras/work/sleepyscripts/suspend.sh")
        for e in ls:
            print e.get_mask_description()
Exemple #58
0
 def _add_watch(self, path):
     if path not in self._ifds:
         log.debug("Adding watch for path %s" % path)
         ifd = inotifyx.init()
         inotifyx.add_watch(ifd, path, self.mask)
         self._ifds[path] = ifd