From 851d5c12b25cebf9735443c06309cff1d9fbaf63 Mon Sep 17 00:00:00 2001 From: martin Date: Thu, 30 Jun 2011 11:56:48 -0400 Subject: [PATCH] cc-2419: media monitor import on start -files importing working... --- python_apps/media-monitor/MediaMonitor.py | 32 +++++++-------- .../airtimemediamonitorbootstrap.py | 40 +++++++++++++++---- .../airtimefilemonitor/airtimeprocessevent.py | 39 +++++++++++------- 3 files changed, 72 insertions(+), 39 deletions(-) diff --git a/python_apps/media-monitor/MediaMonitor.py b/python_apps/media-monitor/MediaMonitor.py index f9d4e9643..eee4b8ca9 100644 --- a/python_apps/media-monitor/MediaMonitor.py +++ b/python_apps/media-monitor/MediaMonitor.py @@ -37,33 +37,31 @@ try: config = AirtimeMediaConfig(logger) multi_queue = mpQueue() - - - bootstrap = AirtimeMediaMonitorBootstrap(logger, multi_queue) - bootstrap.scan() - logger.info("Initializing event processor") - pe = AirtimeProcessEvent(multi_queue, airtime_config=config) + pe = AirtimeProcessEvent(queue=multi_queue, airtime_config=config) notifier = AirtimeNotifier(pe.wm, pe, read_freq=0.1, timeout=0.1, airtime_config=config) notifier.coalesce_events() + + logger.info("Setting up monitor") + response = None + while response is None: + response = notifier.api_client.setup_media_monitor() + time.sleep(5) + + storage_directory = response["stor"].encode('utf-8') + logger.info("Storage Directory is: %s", storage_directory) + config.storage_directory = storage_directory + + bootstrap = AirtimeMediaMonitorBootstrap(logger, multi_queue, pe) + bootstrap.scan() #create 5 worker processes for i in range(5): p = Process(target=notifier.process_file_events, args=(multi_queue,)) processes.append(p) p.start() - - logger.info("Setting up monitor") - response = None - while response is None: - response = notifier.api_client.setup_media_monitor() - time.sleep(5) - - storage_directory = response["stor"].encode('utf-8') - logger.info("Storage Directory is: %s", storage_directory) - config.storage_directory = storage_directory - + wdd = pe.watch_directory(storage_directory) logger.info("Added watch to %s", storage_directory) logger.info("wdd result %s", wdd[storage_directory]) diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py b/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py index 70f5a8f40..cc22dc45d 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py @@ -5,9 +5,10 @@ from subprocess import Popen, PIPE class AirtimeMediaMonitorBootstrap(): - def __init__(self, logger, multi_queue): + def __init__(self, logger, multi_queue, pe): self.logger = logger self.multi_queue = multi_queue + self.pe = pe """ on bootup we want to scan all directories and look for files that @@ -22,16 +23,24 @@ class AirtimeMediaMonitorBootstrap(): def check_for_diff(self, dir): airtime_tmp = '/var/tmp/airtime' + + #set to hold new and/or modified files. We use a set to make it ok if files are added + #twice. This is become some of the tests for new files return result sets that are not + #mutually exclusive from each other. + modified_files = set() #find files that have been modified since the last time #media-monitor process was running. command = "find %s -type f -iname '*.ogg' -o -iname '*.mp3' -readable -mmin -30" % dir stdout = self.execCommandAndReturnStdOut(command) self.logger.info("Files modified since last checkin: \n%s\n", stdout) - - #TODO: notify about modified and newly created files (not including copied files) + + new_files = stdout.split('\n') + + for file_path in new_files: + modified_files.add(file_path) - if os.path.exists(airtime_tmp + '/.airtime_media_index'): + if os.path.exists(airtime_tmp + '/.airtime_media_index') and False: #a previous index exists, we can do a diff between this #file and the current state to see whether anything has #changed. @@ -54,11 +63,26 @@ class AirtimeMediaMonitorBootstrap(): self.logger.info("Previous index file does not exist. Creating a new one") #create a new index file. - command = "find %s -type f -iname '*.ogg' -o -iname '*.mp3' -readable > %s/.airtime_media_index" % (dir, airtime_tmp) - self.execCommand(command) + command = "find %s -type f -iname '*.ogg' -o -iname '*.mp3' -readable" % dir + stdout = self.execCommandAndReturnStdOut(command) + self.logger.info("New files found: \n%s\n", stdout) + self.write_file(airtime_tmp + '/.airtime_media_index', stdout) + + new_files = stdout.split('\n') + + for file_path in new_files: + modified_files.add(file_path) + + self.logger.debug("set size: %d", len(modified_files)) - #TODO: notify about all files in this directory. - self.multi_queue.put(event) + for file_path in modified_files: + if os.path.exists(file_path): + self.pe.handle_created_file(False, os.path.basename(file_path), file_path) + + def write_file(self, file, string): + f = open(file, 'w') + f.write(string) + f.close() def execCommand(self, command): p = Popen(command, shell=True) diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py b/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py index a736d85b1..994ad2c37 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py @@ -138,6 +138,7 @@ class AirtimeProcessEvent(ProcessEvent): return filepath + #create path in /srv/airtime/stor/imported/[song-metadata] def create_file_path(self, imported_filepath, orig_md): storage_directory = self.config.storage_directory @@ -192,29 +193,39 @@ class AirtimeProcessEvent(ProcessEvent): return filepath, is_recorded_show + #event.dir: True if the event was raised against a directory. + #event.name + #event.pathname: pathname (str): Concatenation of 'path' and 'name'. def process_IN_CREATE(self, event): - #self.logger.info("%s: %s", event.maskname, event.pathname) - storage_directory = self.config.storage_directory + self.logger.debug("PROCESS_IN_CREATE") + self.handle_created_file(event.dir, event.name, event.pathname) - if not event.dir: + + def handle_created_file(self, dir, name, pathname): + + self.logger.debug("dir: %s, name: %s, pathname: %s ", dir, name, pathname) + storage_directory = self.config.storage_directory + if not dir: #file created is a tmp file which will be modified and then moved back to the original filename. - if self.is_temp_file(event.name) : - self.temp_files[event.pathname] = None + if self.is_temp_file(name) : + self.temp_files[pathname] = None #This is a newly imported file. - elif self.is_audio_file(event.pathname): - if self.is_parent_directory(event.pathname, storage_directory): - self.set_needed_file_permissions(event.pathname, event.dir) + elif self.is_audio_file(pathname): + if self.is_parent_directory(pathname, storage_directory): + self.set_needed_file_permissions(pathname, dir) - self.process_new_file(event.pathname) + self.process_new_file(pathname) else: - self.file_events.append({'mode': self.config.MODE_CREATE, 'filepath': event.pathname, 'is_recorded_show': False}) + self.file_events.append({'mode': self.config.MODE_CREATE, 'filepath': pathname, 'is_recorded_show': False}) else: - if self.is_parent_directory(event.pathname, storage_directory): - self.set_needed_file_permissions(event.pathname, event.dir) - - def process_new_file(pathname): + if self.is_parent_directory(pathname, storage_directory): + self.set_needed_file_permissions(pathname, dir) + + + def process_new_file(self, pathname): + self.logger.info("Processing new file: %s", pathname) file_md = self.md_manager.get_md_from_file(pathname) if file_md is not None: