def process_IN_MOVED_FROM(self, event): # Is either delete dir or delete file evt = self.process_delete(event) # evt can be none whenever event points that a file that would be # ignored by @IncludeOnly if hasattr(event, 'cookie') and (evt != None): user().event_registry.register(evt)
def process_IN_MOVED_FROM(self, event): # Is either delete dir or delete file evt = self.process_delete(event) # evt can be none whenever event points that a file that would be # ignored by @IncludeOnly if hasattr(event,'cookie') and (evt != None): user().event_registry.register(evt)
def process_IN_MOVED_TO(self, event): if user().event_registry.registered(event): # We need this trick because we don't how to "expand" dir events # into file events until we know for sure if we deleted or moved morph = MoveDir(event) if event.dir else MoveFile(event) user().event_registry.matching(event).morph_into(morph) else: self.process_create(event)
def wrapped(self, event, *args, **kwargs): event.pathname = unicode(event.pathname, "utf-8") if user().file_mediator.is_ignored(event.pathname): user().file_mediator.logger.info("Ignoring: '%s' (once)" % event.pathname) user().file_mediator.unignore(event.pathname) else: return fn(self, event, *args, **kwargs)
def run(self): self.index_create() manager = Manager() apiclient = apc() config = user().mm_config WatchSyncer(signal=getsig('watch'), chunking_number=config['chunking_number'], timeout=config['request_max_wait']) airtime_receiver = AirtimeMessageReceiver(config, manager) airtime_notifier = AirtimeNotifier(config, airtime_receiver) adb = AirtimeDB(apiclient) store = { u'stor': adb.storage_path(), u'watched_dirs': adb.list_watched(), } self.logger.info("initializing mm with directories: %s" % str(store)) self.logger.info("Initing with the following airtime response:%s" % str(store)) airtime_receiver.change_storage({'directory': store[u'stor']}) for watch_dir in store[u'watched_dirs']: if not os.path.exists(watch_dir): # Create the watch_directory here try: os.makedirs(watch_dir) except Exception: self.logger.error("Could not create watch directory: '%s' \ (given from the database)." % watch_dir) if os.path.exists(watch_dir): airtime_receiver.new_watch({'directory': watch_dir}, restart=True) else: self.logger.info("Failed to add watch on %s" % str(watch_dir)) EventDrainer(airtime_notifier, interval=float(config['rmq_event_wait'])) # Launch the toucher that updates the last time when the script was # ran every n seconds. # TODO : verify that this does not interfere with bootstrapping because the # toucher thread might update the last_ran variable too fast ToucherThread(path=user().touch_file_path(), interval=int(config['touch_interval'])) success = False while not success: try: apiclient.register_component('media-monitor') success = True except Exception, e: self.logger.error(str(e)) import time time.sleep(10)
def run(self): self.index_create() manager = Manager() apiclient = apc() config = user().mm_config WatchSyncer(signal=getsig('watch'), chunking_number=config['chunking_number'], timeout=config['request_max_wait']) airtime_receiver = AirtimeMessageReceiver(config,manager) airtime_notifier = AirtimeNotifier(config, airtime_receiver) adb = AirtimeDB(apiclient) store = { u'stor' : adb.storage_path(), u'watched_dirs' : adb.list_watched(), } self.logger.info("initializing mm with directories: %s" % str(store)) self.logger.info( "Initing with the following airtime response:%s" % str(store)) airtime_receiver.change_storage({ 'directory':store[u'stor'] }) for watch_dir in store[u'watched_dirs']: if not os.path.exists(watch_dir): # Create the watch_directory here try: os.makedirs(watch_dir) except Exception: self.logger.error("Could not create watch directory: '%s' \ (given from the database)." % watch_dir) if os.path.exists(watch_dir): airtime_receiver.new_watch({ 'directory':watch_dir }, restart=True) else: self.logger.info("Failed to add watch on %s" % str(watch_dir)) EventDrainer(airtime_notifier, interval=float(config['rmq_event_wait'])) # Launch the toucher that updates the last time when the script was # ran every n seconds. # TODO : verify that this does not interfere with bootstrapping because the # toucher thread might update the last_ran variable too fast ToucherThread(path=user().touch_file_path(), interval=int(config['touch_interval'])) success = False while not success: try: apiclient.register_component('media-monitor') success = True except Exception, e: self.logger.error(str(e)) import time time.sleep(10)
def handle(self, sender, event): """ Intercept events where a new file has been added to the organize directory and place it in the correct path (starting with self.target_path) """ # Only handle this event type assert isinstance(event, OrganizeFile), \ "Organizer can only handle OrganizeFile events.Given '%s'" % event try: # We must select the target_path based on whether file was recorded # by airtime or not. # Do we need to "massage" the path using mmp.organized_path? target_path = self.recorded_path if event.metadata.is_recorded() \ else self.target_path # nasty hack do this properly owner_id = mmp.owner_id(event.path) if owner_id != -1: target_path = os.path.join(target_path, unicode(owner_id)) mdata = event.metadata.extract() new_path = mmp.organized_path(event.path, target_path, mdata) # See hack in mmp.magic_move def new_dir_watch(d): # TODO : rewrite as return lambda : dispatcher.send(... def cb(): dispatcher.send(signal=getsig("add_subwatch"), sender=self, directory=d) return cb mmp.magic_move(event.path, new_path, after_dir_make=new_dir_watch(dirname(new_path))) # The reason we need to go around saving the owner in this # backwards way is because we are unable to encode the owner id # into the file itself so that the StoreWatchListener listener can # detect it from the file user().owner.add_file_owner(new_path, owner_id) self.logger.info('Organized: "%s" into "%s"' % (event.path, new_path)) except BadSongFile as e: self.report_problem_file(event=event, exception=e) # probably general error in mmp.magic.move... except Exception as e: self.unexpected_exception(e) self.report_problem_file(event=event, exception=e)
def safe_pack(self): """ returns exceptions instead of throwing them to be consistent with events that must catch their own BadSongFile exceptions since generate a set of exceptions instead of a single one """ try: self._pack_hook() ret = self.pack() # Remove owner of this file only after packing. Otherwise packing # will not serialize the owner correctly into the airtime request user().owner.remove_file_owner(self.path) return ret except BadSongFile as e: return [e] except Exception as e: self.unexpected_exception(e) return[e]
def handle(self, sender, event): """ Intercept events where a new file has been added to the organize directory and place it in the correct path (starting with self.target_path) """ # Only handle this event type assert isinstance(event, OrganizeFile), \ "Organizer can only handle OrganizeFile events.Given '%s'" % event try: # We must select the target_path based on whether file was recorded # by airtime or not. # Do we need to "massage" the path using mmp.organized_path? target_path = self.recorded_path if event.metadata.is_recorded() \ else self.target_path # nasty hack do this properly owner_id = mmp.owner_id(event.path) if owner_id != -1: target_path = os.path.join(target_path, unicode(owner_id)) mdata = event.metadata.extract() new_path = mmp.organized_path(event.path, target_path, mdata) # See hack in mmp.magic_move def new_dir_watch(d): # TODO : rewrite as return lambda : dispatcher.send(... self.logger.debug('new_dir_watch(%s)' % d) def cb(): self.logger.debug('cb(%s)' % d) dispatcher.send(signal=getsig("add_subwatch"), sender=self, directory=d) return cb mmp.magic_move(event.path, new_path, after_dir_make=new_dir_watch(dirname(new_path))) # The reason we need to go around saving the owner in this # backwards way is because we are unable to encode the owner id # into the file itself so that the StoreWatchListener listener can # detect it from the file user().owner.add_file_owner(new_path, owner_id ) self.logger.info('Organized: "%s" into "%s"' % (event.path, new_path)) except BadSongFile as e: self.report_problem_file(event=event, exception=e) # probably general error in mmp.magic.move... except Exception as e: self.unexpected_exception( e ) self.report_problem_file(event=event, exception=e)
def safe_pack(self): """ returns exceptions instead of throwing them to be consistent with events that must catch their own BadSongFile exceptions since generate a set of exceptions instead of a single one """ try: self._pack_hook() ret = self.pack() # Remove owner of this file only after packing. Otherwise packing # will not serialize the owner correctly into the airtime request user().owner.remove_file_owner(self.path) return ret except BadSongFile as e: return [e] except Exception as e: self.unexpected_exception(e) return [e]
def __init__(self, raw_event): # TODO : clean up this idiotic hack # we should use keyword constructors instead of this behaviour checking # bs to initialize BaseEvent if hasattr(raw_event,"pathname"): self._raw_event = raw_event self.path = os.path.normpath(raw_event.pathname) else: self.path = raw_event self.owner = user().owner.get_owner(self.path) owner_re = re.search('stor/imported/(?P<owner>\d+)/', self.path) if owner_re: self.logger.info("matched path: %s" % self.path) self.owner = owner_re.group('owner') else: self.logger.info("did not match path: %s" % self.path) self._pack_hook = lambda: None # no op
def __init__(self, raw_event): # TODO : clean up this idiotic hack # we should use keyword constructors instead of this behaviour checking # bs to initialize BaseEvent if hasattr(raw_event, "pathname"): self._raw_event = raw_event self.path = os.path.normpath(raw_event.pathname) else: self.path = raw_event self.owner = user().owner.get_owner(self.path) owner_re = re.search('stor/imported/(?P<owner>\d+)/', self.path) if owner_re: self.logger.info("matched path: %s" % self.path) self.owner = owner_re.group('owner') else: self.logger.info("did not match path: %s" % self.path) self._pack_hook = lambda: None # no op
def index_create(self, index_create_attempt=False): config = user().mm_config if not index_create_attempt: if not os.path.exists(config['index_path']): self.logger.info("Attempting to create index file:...") try: with open(config['index_path'], 'w') as f: f.write(" ") except Exception as e: self.logger.info("Failed to create index file with exception: %s" \ % str(e)) else: self.logger.info("Created index file, reloading configuration:") self.index_create(index_create_attempt=True) else: self.logger.info("Already tried to create index. Will not try again ") if not os.path.exists(config['index_path']): raise CouldNotCreateIndexFile(config['index_path'])
def index_create(self, index_create_attempt=False): config = user().mm_config if not index_create_attempt: if not os.path.exists(config['index_path']): self.logger.info("Attempting to create index file:...") try: with open(config['index_path'], 'w') as f: f.write(" ") except Exception as e: self.logger.info("Failed to create index file with exception: %s" \ % str(e)) else: self.logger.info( "Created index file, reloading configuration:") self.index_create(index_create_attempt=True) else: self.logger.info( "Already tried to create index. Will not try again ") if not os.path.exists(config['index_path']): raise CouldNotCreateIndexFile(config['index_path'])
def reload_directories(self): """ this is the 'real' constructor, should be called if you ever want the class reinitialized. there's not much point to doing it yourself however, you should just create a new AirtimeDB instance. """ saas = user().root_path try: # dirs_setup is a dict with keys: # u'watched_dirs' and u'stor' which point to lists of corresponding # dirs dirs_setup = self.apc.setup_media_monitor() dirs_setup[u'stor'] = normpath( join(saas, dirs_setup[u'stor'] ) ) dirs_setup[u'watched_dirs'] = map(lambda p: normpath(join(saas,p)), dirs_setup[u'watched_dirs']) dirs_with_id = dict([ (k,normpath(v)) for k,v in self.apc.list_all_watched_dirs()['dirs'].iteritems() ]) self.id_to_dir = dirs_with_id self.dir_to_id = dict([ (v,k) for k,v in dirs_with_id.iteritems() ]) self.base_storage = dirs_setup[u'stor'] self.storage_paths = mmp.expand_storage( self.base_storage ) self.base_id = self.dir_to_id[self.base_storage] # hack to get around annoying schema of airtime db self.dir_to_id[ self.recorded_path() ] = self.base_id self.dir_to_id[ self.import_path() ] = self.base_id # We don't know from the x_to_y dict which directory is watched or # store... self.watched_directories = set([ os.path.normpath(p) for p in dirs_setup[u'watched_dirs'] ]) except Exception, e: self.logger.info(str(e))
def wrapped(self, event, *args,**kwargs): event.pathname = unicode(event.pathname, "utf-8") if user().file_mediator.is_ignored(event.pathname): user().file_mediator.logger.info("Ignoring: '%s' (once)" % event.pathname) user().file_mediator.unignore(event.pathname) else: return fn(self, event, *args, **kwargs)