def process_event(self, event): if not self._config.has_key('replica_destination'): # Bad config! raise iPluginError("Missing replica_destination directive.") return if event.event_name == 'WATCH_INIT': self._cache.push('mirror_config_' + self._watch.get_path(), self._config, True) elif event.event_name == 'WATCH_RECONFIG': # Configuration might have changed! cached_config = self._cache.get('mirror_config_' + self._watch.get_path()) if cached_config['replica_destination'] != self._config['mirror_destination']: # Our target has changed - reinit self._cache.push('mirror_config_' + self._watch.get_path(), self._config, True) self._init_mirror(event) if self._events.has_key(event.event_name): # Check if we have a delayed move event: cached_event = self._cache.pop('mirror_'+self._watch.get_path()) if cached_event and event.event_name == 'IN_MOVED_TO' and event.cookie == cached_event.cookie: # A matching MOVE event self._finish_move(event, cached_event) return elif cached_event: # Not a matching event - object should be deleted self._delete(cached_event) if self._events[event.event_name]: self._events[event.event_name](event)
def _log(self, msg): if not self._config.has_key('scribe_log'): raise iPluginError("Missing scribe_log directive.") return if self._config['scribe_log'] != '-': log_file = self._cache.pop('scribe_' + self._config['scribe_log']) if not log_file: try: log_file = file(self._config['scribe_log'], 'a') self._cache.push('scribe_' + self._config['scribe_log'], log_file, True) except IOError, data: raise iPluginError("Could not open log file '%s': %s" % (self._config['scribe_log'], data)) try: print >>log_file, "%s " % datetime.datetime.now() + msg except IOError, data: raise iPluginError("Could not write to log file '%s': %s" % (self._config['scribe_log'], data))
def _finish_move(self, event, cached_event=None): """ A matching MOVED_TO event received - do the move. """ try: source = os.path.join(cached_event.path, cached_event.name) source = self._form_destination(source) destination = os.path.join(event.path, event.name) destination = self._form_destination(destination) shutil.move(source, destination) except shutil.Error, data: raise iPluginError("Error moving '%s' to '%s'." % (source, destination))
def _init_mirror(self, event): """ First event ever - do first time sync. """ try: if os.path.exists(self._config['replica_destination']): self._delete_target(self._config['replica_destination']) shutil.copytree(self._watch.get_path(), self._config['replica_destination']) except iPluginError: raise except (IOError, shutil.Error), data: raise iPluginError("Error creating initial mirror: %s" % data)
def _delete_target(self, target): """ Delete target file/directory. """ try: if os.path.exists(target): if os.path.isdir(target): shutil.rmtree(target) else: os.unlink(target) else: # We are getting the DELETE event of a file that was actually # deleted before we can even recreate it. # So do nothing... pass except Exception, data: raise iPluginError("Error deleting '%s': %s" % (target, data))
iPlugin.__init__(self, *args, **kwargs) def _init_mirror(self, event): """ First event ever - do first time sync. """ try: if os.path.exists(self._config['replica_destination']): self._delete_target(self._config['replica_destination']) shutil.copytree(self._watch.get_path(), self._config['replica_destination']) except iPluginError: raise except (IOError, shutil.Error), data: raise iPluginError("Error creating initial mirror: %s" % data) except Exception, data: raise iPluginError("Unexpected error while creating initial mirror: %s" % data) except: raise iPluginError("Unexpected error while creating initial mirror.") def _prepare_move(self, event): """ Prepare a move from a MOVED_FROM event. """ # An object was moved out. Wait to see if the next # event that we'll get is going to be an IN_MOVED_TO one. self._cache.push('mirror_' + self._watch.get_path(), event) def _finish_move(self, event, cached_event=None): """ A matching MOVED_TO event received - do the move. """ try: source = os.path.join(cached_event.path, cached_event.name) source = self._form_destination(source) destination = os.path.join(event.path, event.name) destination = self._form_destination(destination) shutil.move(source, destination)