From 3dd5b2869db158a3e77f238f883550ac67f343fa Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Thu, 25 Oct 2012 18:38:15 -0400 Subject: [PATCH] Added lsof to check if file is locked --- .../media-monitor2/media/monitor/listeners.py | 17 ++++++----------- .../media-monitor2/media/monitor/manager.py | 4 ++-- .../media-monitor2/media/monitor/organizer.py | 5 ----- .../media-monitor2/media/monitor/pure.py | 7 +++++++ 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/python_apps/media-monitor2/media/monitor/listeners.py b/python_apps/media-monitor2/media/monitor/listeners.py index d30a65147..4c860a97e 100644 --- a/python_apps/media-monitor2/media/monitor/listeners.py +++ b/python_apps/media-monitor2/media/monitor/listeners.py @@ -48,17 +48,11 @@ class BaseListener(object): class OrganizeListener(BaseListener, pyinotify.ProcessEvent, Loggable): def process_IN_CLOSE_WRITE(self, event): #self.logger.info("===> handling: '%s'" % str(event)) - #self.process_to_organize(event) - pass - # got cookie + self.process_to_organize(event) + def process_IN_MOVED_TO(self, event): #self.logger.info("===> handling: '%s'" % str(event)) - #self.process_to_organize(event) - pass - - def process_default(self, event): - pass - #self.logger.info("===> Not handling: '%s'" % str(event)) + self.process_to_organize(event) def flush_events(self, path): """ @@ -69,8 +63,9 @@ class OrganizeListener(BaseListener, pyinotify.ProcessEvent, Loggable): for f in mmp.walk_supported(path, clean_empties=True): self.logger.info("Bootstrapping: File in 'organize' directory: \ '%s'" % f) - dispatcher.send(signal=self.signal, sender=self, - event=OrganizeFile(f)) + if not mmp.file_locked(f): + dispatcher.send(signal=self.signal, sender=self, + event=OrganizeFile(f)) flushed += 1 #self.logger.info("Flushed organized directory with %d files" % flushed) diff --git a/python_apps/media-monitor2/media/monitor/manager.py b/python_apps/media-monitor2/media/monitor/manager.py index ec705d5af..33dc90468 100644 --- a/python_apps/media-monitor2/media/monitor/manager.py +++ b/python_apps/media-monitor2/media/monitor/manager.py @@ -26,7 +26,7 @@ class ManagerTimeout(threading.Thread,Loggable): self.interval = interval def run(self): while True: - time.sleep(self.interval) # every 3 seconds + time.sleep(self.interval) self.manager.flush_organize() class Manager(Loggable): @@ -178,7 +178,7 @@ class Manager(Loggable): # the OrganizeListener instance will walk path and dispatch an organize # event for every file in that directory self.organize['organize_listener'].flush_events(new_path) - self.__add_watch(new_path, self.organize['organize_listener']) + #self.__add_watch(new_path, self.organize['organize_listener']) def flush_organize(self): path = self.organize['organize_path'] diff --git a/python_apps/media-monitor2/media/monitor/organizer.py b/python_apps/media-monitor2/media/monitor/organizer.py index c6bda1cfa..ea6851356 100644 --- a/python_apps/media-monitor2/media/monitor/organizer.py +++ b/python_apps/media-monitor2/media/monitor/organizer.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -import time import media.monitor.pure as mmp import media.monitor.owners as owners from media.monitor.handler import ReportHandler @@ -72,13 +71,9 @@ class Organizer(ReportHandler,Loggable): directory=d) return cb - time.sleep(0.02) - mmp.magic_move(event.path, new_path, after_dir_make=new_dir_watch(dirname(new_path))) - time.sleep(0.02) - # The reason we need to go around saving the owner in this ass # backwards way is bewcause we are unable to encode the owner id # into the file itself so that the StoreWatchListener listener can diff --git a/python_apps/media-monitor2/media/monitor/pure.py b/python_apps/media-monitor2/media/monitor/pure.py index ec8f1695a..877cbdc72 100644 --- a/python_apps/media-monitor2/media/monitor/pure.py +++ b/python_apps/media-monitor2/media/monitor/pure.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import copy +from subprocess import Popen, PIPE import subprocess import os import math @@ -165,6 +166,12 @@ def walk_supported(directory, clean_empties=False): for fp in full_paths: yield fp if clean_empties: clean_empty_dirs(directory) + +def file_locked(path): + cmd = "lsof %s" % path + f = Popen(cmd, shell=True, stdout=PIPE).stdout + return bool(f.readlines()) + def magic_move(old, new, after_dir_make=lambda : None): """ Moves path old to new and constructs the necessary to directories for new