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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    def _observe_inotify(self, watch_dir):
        self.msg.info(self.name, 'Using inotify.')

        timeout = -1
        fd = inotifyx.init()
        try:
            self._inotify_watch_recursive(fd, watch_dir)
            while True:
                events = inotifyx.get_events(fd, timeout)
                if events:
                    for event in events:
                        if not event.mask & inotifyx.IN_ISDIR:
                            filename = self._get_playing_file_lsof(self.process_name)
                            (state, show_tuple) = self._get_playing_show(filename)
                            self.update_show_if_needed(state, show_tuple)

                            if self.last_state == STATE_NOVIDEO:
                                # Make get_events block indifinitely
                                timeout = -1
                            else:
                                timeout = 1
                else:
                    self.update_show_if_needed(self.last_state, self.last_show_tuple)
        except IOError:
            self.msg.warn(self.name, 'Watch directory not found! Tracker will stop.')
        finally:
            os.close(fd)
Ejemplo n.º 4
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
Ejemplo n.º 5
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)
Ejemplo n.º 6
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__()
Ejemplo n.º 7
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)
Ejemplo n.º 8
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)
Ejemplo n.º 9
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()
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
Archivo: linux.py Proyecto: brtsz/zato
    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)
Ejemplo n.º 12
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)
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
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)
Ejemplo n.º 15
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)
Ejemplo n.º 16
0
def scan(topdir):
    '''
    print filesystem events for tree of files
    '''
    fd = inotifyx.init()

    mask = inotifyx.IN_MODIFY | inotifyx.IN_CLOSE_WRITE
    if 0:
        mask = inotifyx.IN_ALL_EVENTS

    wd_to_path = add_watch_dirs(
        topdir=topdir,
        fd=fd,
        mask=mask,
        )

    try:
        oldpath = None
        while True:
            for event in inotifyx.get_events(fd):
                path = os.path.join(
                    wd_to_path[event.wd],
                    event.name if event.name else '',
                    )

                parts = [ event.get_mask_description() ]
                parts = [ word.replace('IN_ALL_EVENTS|', '')
                          for word in parts
                          ]

                if path != oldpath:
                    print path
                oldpath = path
                print '\t', ' '.join(parts)

    except KeyboardInterrupt:
        pass
    finally:
        os.close(fd)
Ejemplo n.º 17
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)
Ejemplo n.º 18
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)
Ejemplo n.º 19
0
 def getFd(self):
     if not hasattr(self, '_fd'):
         self._fd = init()
     return self._fd
Ejemplo n.º 20
0
def watch_dhcp_leases(path):
    """
    This is a generator for events that happen to the file at the
    given path. It assumes ISC DHCPD -like behavior; that is, ISC
    dhcpd man page dhcpd.leases(5) promises that the leases file is
    only updated in exactly two ways:

    1. new entries are appended

    2. a new (pruned) lease file is renamed to the monitored filename
       (the old file is first moved out of the way, but that's not
       relevant here)

    So we trigger events on appends and lease file replacement, and
    start monitoring the new file on the latter.

    Example::

        for _ in watch('/var/lib/dhcp/dhcpd.leases'):
            print "Stuff happened!"

    """
    fd = inotifyx.init()
    parent = os.path.dirname(path)
    if parent == "":
        # this happens if path had no slashes in it; mostly when
        # testing manually
        parent = "."

    try:
        wd_new = inotifyx.add_watch(fd, parent, inotifyx.IN_MOVED_TO | inotifyx.IN_ONLYDIR)

        while True:
            # the only real reason for `path` not to exist here is on
            # first loop (assuming dhcpd behavior); `replaced` only
            # becomes True after a file is renamed to this name, so
            # there's no race there (barring un-dhcpd like behavior
            # like deleting the file)
            wd_journal = inotifyx.add_watch(fd, path, inotifyx.IN_MODIFY)

            # yield once in order to 1) make caller read the existing
            # content of the file and 2) make writing unit tests
            # easier; now we already have an open inotify fd with the
            # right watches
            yield None

            replaced = False
            while not replaced:
                for e in inotifyx.get_events(fd):
                    if e.mask & inotifyx.IN_Q_OVERFLOW:
                        yield e
                        # we could have lost an IN_MOVED_TO event,
                        # do this just in case
                        replaced = True
                    elif e.wd == wd_journal:
                        yield e
                    elif e.wd == wd_new and e.mask & inotifyx.IN_MOVED_TO and e.name == os.path.basename(path):
                        # the old watch is implicitly removed as the
                        # file is deleted, no need to clean that up;
                        # not 100% sure if this will leak wds or not
                        replaced = True
    finally:
        os.close(fd)
Ejemplo n.º 21
0
import time
import RPi.GPIO as GPIO
from pulsatingLED import PulsatingLED
from tardisButton import TardisButton
from indicators import Indicators
from volume import Volume
import subprocess
import sys
import os
import glob
import inspect
import inotifyx
import json

# Setup a watch
fd = inotifyx.init()

# Where are we?
dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

# Initial volume (from file if exists, else 80)
vol = Volume(dir)

# RGB display and music player
m = SonicRGB(red=7, green=24, blue=26,
             pwmFrequency=500, cutoffs=[50, 500, 2000, 15000])

# The 5 music selection buttons
b1 = TardisButton(11, m)
b2 = TardisButton(13, m)
b3 = TardisButton(12, m)
Ejemplo n.º 22
0
	def __init__(self):
		super(Monitor, self).__init__()
		self.fd = inotifyx.init()
		self.dirs = {}
		self.wds = {}
		self.queue = []
Ejemplo n.º 23
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
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)
Ejemplo n.º 25
0
 def __init__ (self):
     self.fd = inotifyx.init()
     self.wd = self._add (vmbase)
     self.paths = {}
Ejemplo n.º 26
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)
Ejemplo n.º 27
0
 def setUp(self):
     os.mkdir(self.test_dir)
     os.chdir(self.test_dir)
     self.fd = inotifyx.init()