-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
This commit is contained in:
martin 2011-07-11 12:21:30 -04:00
parent 98173d5e53
commit ed41969f0e
4 changed files with 32 additions and 23 deletions

View file

@ -50,9 +50,9 @@ try:
storage_directory = response["stor"].encode('utf-8') storage_directory = response["stor"].encode('utf-8')
logger.info("Storage Directory is: %s", storage_directory) logger.info("Storage Directory is: %s", storage_directory)
config.storage_directory = storage_directory config.storage_directory = os.path.normpath(storage_directory)
config.imported_directory = storage_directory + '/imported' config.imported_directory = os.path.normpath(storage_directory + '/imported')
config.organize_directory = storage_directory + '/organize' config.organize_directory = os.path.normpath(storage_directory + '/organize')
multi_queue = mpQueue() multi_queue = mpQueue()
logger.info("Initializing event processor") logger.info("Initializing event processor")

View file

@ -52,11 +52,10 @@ class AirtimeMediaMonitorBootstrap():
if len(file_path.strip(" \n")) > 0: if len(file_path.strip(" \n")) > 0:
all_files_set.add(file_path[len(dir)+1:]) all_files_set.add(file_path[len(dir)+1:])
if os.path.exists("/var/tmp/airtime/.last_index"):
if os.path.exists("/var/tmp/airtime/.media_monitor_boot"):
#find files that have been modified since the last time #find files that have been modified since the last time
#media-monitor process started. #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) command = "find %s -type f -iname '*.ogg' -o -iname '*.mp3' -readable -mmin -%d" % (dir, time_diff_sec/60+1)
else: else:
command = "find %s -type f -iname '*.ogg' -o -iname '*.mp3' -readable" % dir 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) self.logger.info("Modified files: \n%s\n\n"%modified_files_set)
#"touch" file timestamp #"touch" file timestamp
open("/var/tmp/airtime/.media_monitor_boot","w") self.pe.touch_index_file()
for file_path in deleted_files_set: for file_path in deleted_files_set:
self.pe.handle_removed_file(False, "%s/%s" % (dir, file_path)) self.pe.handle_removed_file(False, "%s/%s" % (dir, file_path))

View file

@ -94,28 +94,15 @@ class AirtimeNotifier(Notifier):
self.bootstrap.check_for_diff(new_storage_directory_id, new_storage_directory) self.bootstrap.check_for_diff(new_storage_directory_id, new_storage_directory)
self.config.storage_directory = new_storage_directory self.config.storage_directory = os.path.normpath(new_storage_directory)
self.config.imported_directory = new_storage_directory + '/imported' self.config.imported_directory = os.path.normpath(new_storage_directory + '/imported')
self.config.organize_directory = new_storage_directory + '/organize' 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.storage_directory)
mm.ensure_is_dir(self.config.imported_directory) mm.ensure_is_dir(self.config.imported_directory)
mm.ensure_is_dir(self.config.organize_directory) mm.ensure_is_dir(self.config.organize_directory)
mm.watch_directory(new_storage_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": elif m['event_type'] == "file_delete":
self.logger.info("Deleting file: %s ", m['filepath']) self.logger.info("Deleting file: %s ", m['filepath'])
mm = self.proc_fun() mm = self.proc_fun()

View file

@ -57,6 +57,8 @@ class AirtimeProcessEvent(ProcessEvent):
return self.wm.add_watch(directory, self.mask, rec=True, auto_add=True) return self.wm.add_watch(directory, self.mask, rec=True, auto_add=True)
def is_parent_directory(self, filepath, directory): def is_parent_directory(self, filepath, directory):
filepath = os.path.normpath(filepath)
directory = os.path.normpath(directory)
return (directory == filepath[0:len(directory)]) return (directory == filepath[0:len(directory)])
def is_temp_file(self, filename): def is_temp_file(self, filename):
@ -130,6 +132,8 @@ class AirtimeProcessEvent(ProcessEvent):
finally: finally:
os.umask(omask) 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): def move_file(self, source, dest):
try: try:
@ -139,6 +143,21 @@ class AirtimeProcessEvent(ProcessEvent):
self.logger.error("failed to move file. %s", e) self.logger.error("failed to move file. %s", e)
finally: finally:
os.umask(omask) 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 #checks if path exists already in stor. If the path exists and the md5s are the
#same just overwrite. #same just overwrite.
@ -371,11 +390,15 @@ class AirtimeProcessEvent(ProcessEvent):
stdout = unicode(stdout, "utf_8") stdout = unicode(stdout, "utf_8")
return stdout.splitlines() return stdout.splitlines()
def touch_index_file(self):
open("/var/tmp/airtime/.last_index", "w")
def notifier_loop_callback(self, notifier): def notifier_loop_callback(self, notifier):
if len(self.file_events) > 0: if len(self.file_events) > 0:
for event in self.file_events: for event in self.file_events:
self.multi_queue.put(event) self.multi_queue.put(event)
self.touch_index_file()
self.file_events = [] self.file_events = []