From ed41969f0eb4feb3372ce86ca39b184703204827 Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 11 Jul 2011 12:21:30 -0400 Subject: [PATCH] -touch index file after each inotify event -make sure to normalize all paths, since we are string comparing them -remove all empty dirs after moving files out of them --- python_apps/media-monitor/MediaMonitor.py | 6 ++--- .../airtimemediamonitorbootstrap.py | 7 +++--- .../airtimefilemonitor/airtimenotifier.py | 19 +++------------ .../airtimefilemonitor/airtimeprocessevent.py | 23 +++++++++++++++++++ 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/python_apps/media-monitor/MediaMonitor.py b/python_apps/media-monitor/MediaMonitor.py index 8204364fc..c4429b0f6 100644 --- a/python_apps/media-monitor/MediaMonitor.py +++ b/python_apps/media-monitor/MediaMonitor.py @@ -50,9 +50,9 @@ try: storage_directory = response["stor"].encode('utf-8') logger.info("Storage Directory is: %s", storage_directory) - config.storage_directory = storage_directory - config.imported_directory = storage_directory + '/imported' - config.organize_directory = storage_directory + '/organize' + config.storage_directory = os.path.normpath(storage_directory) + config.imported_directory = os.path.normpath(storage_directory + '/imported') + config.organize_directory = os.path.normpath(storage_directory + '/organize') multi_queue = mpQueue() logger.info("Initializing event processor") diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py b/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py index d0b8df2fe..2761f3a5d 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py @@ -52,11 +52,10 @@ class AirtimeMediaMonitorBootstrap(): if len(file_path.strip(" \n")) > 0: all_files_set.add(file_path[len(dir)+1:]) - - if os.path.exists("/var/tmp/airtime/.media_monitor_boot"): + if os.path.exists("/var/tmp/airtime/.last_index"): #find files that have been modified since the last time #media-monitor process started. - time_diff_sec = time.time() - os.path.getmtime("/var/tmp/airtime/.media_monitor_boot") + time_diff_sec = time.time() - os.path.getmtime("/var/tmp/airtime/.last_index") command = "find %s -type f -iname '*.ogg' -o -iname '*.mp3' -readable -mmin -%d" % (dir, time_diff_sec/60+1) else: command = "find %s -type f -iname '*.ogg' -o -iname '*.mp3' -readable" % dir @@ -86,7 +85,7 @@ class AirtimeMediaMonitorBootstrap(): self.logger.info("Modified files: \n%s\n\n"%modified_files_set) #"touch" file timestamp - open("/var/tmp/airtime/.media_monitor_boot","w") + self.pe.touch_index_file() for file_path in deleted_files_set: self.pe.handle_removed_file(False, "%s/%s" % (dir, file_path)) diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py b/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py index 8f58192cc..0eb403fcd 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py @@ -94,28 +94,15 @@ class AirtimeNotifier(Notifier): self.bootstrap.check_for_diff(new_storage_directory_id, new_storage_directory) - self.config.storage_directory = new_storage_directory - self.config.imported_directory = new_storage_directory + '/imported' - self.config.organize_directory = new_storage_directory + '/organize' + self.config.storage_directory = os.path.normpath(new_storage_directory) + self.config.imported_directory = os.path.normpath(new_storage_directory + '/imported') + self.config.organize_directory = os.path.normpath(new_storage_directory + '/organize') mm.ensure_is_dir(self.config.storage_directory) mm.ensure_is_dir(self.config.imported_directory) mm.ensure_is_dir(self.config.organize_directory) mm.watch_directory(new_storage_directory) - - """ - old_storage_contents = os.listdir(storage_directory) - for item in old_storage_contents: - fp = "%s/%s" % (storage_directory, item) - nfp = "%s/%s" % (new_storage_directory, item) - self.logger.info("Moving %s to %s", fp, nfp) - mm.move_file(fp, nfp) - - """ - - - elif m['event_type'] == "file_delete": self.logger.info("Deleting file: %s ", m['filepath']) mm = self.proc_fun() diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py b/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py index 6b0526468..687b201b7 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py @@ -57,6 +57,8 @@ class AirtimeProcessEvent(ProcessEvent): return self.wm.add_watch(directory, self.mask, rec=True, auto_add=True) def is_parent_directory(self, filepath, directory): + filepath = os.path.normpath(filepath) + directory = os.path.normpath(directory) return (directory == filepath[0:len(directory)]) def is_temp_file(self, filename): @@ -130,6 +132,8 @@ class AirtimeProcessEvent(ProcessEvent): finally: os.umask(omask) + #moves file from source to dest but also recursively removes the + #the source file's parent directories if they are now empty. def move_file(self, source, dest): try: @@ -139,6 +143,21 @@ class AirtimeProcessEvent(ProcessEvent): self.logger.error("failed to move file. %s", e) finally: os.umask(omask) + + dir = os.path.dirname(source) + self.cleanup_empty_dirs(dir) + + #keep moving up the file hierarchy and deleting parent + #directories until we hit a non-empty directory, or we + #hit the organize dir. + def cleanup_empty_dirs(self, dir): + if os.path.normpath(dir) != self.config.organize_directory: + if len(os.listdir(dir)) == 0: + os.rmdir(dir) + + pdir = os.path.dirname(dir) + self.cleanup_empty_dirs(pdir) + #checks if path exists already in stor. If the path exists and the md5s are the #same just overwrite. @@ -371,11 +390,15 @@ class AirtimeProcessEvent(ProcessEvent): stdout = unicode(stdout, "utf_8") return stdout.splitlines() + + def touch_index_file(self): + open("/var/tmp/airtime/.last_index", "w") def notifier_loop_callback(self, notifier): if len(self.file_events) > 0: for event in self.file_events: self.multi_queue.put(event) + self.touch_index_file() self.file_events = []