Added lsof to check if file is locked
This commit is contained in:
parent
5b051aae32
commit
3dd5b2869d
4 changed files with 15 additions and 18 deletions
|
@ -48,17 +48,11 @@ class BaseListener(object):
|
||||||
class OrganizeListener(BaseListener, pyinotify.ProcessEvent, Loggable):
|
class OrganizeListener(BaseListener, pyinotify.ProcessEvent, Loggable):
|
||||||
def process_IN_CLOSE_WRITE(self, event):
|
def process_IN_CLOSE_WRITE(self, event):
|
||||||
#self.logger.info("===> handling: '%s'" % str(event))
|
#self.logger.info("===> handling: '%s'" % str(event))
|
||||||
#self.process_to_organize(event)
|
self.process_to_organize(event)
|
||||||
pass
|
|
||||||
# got cookie
|
|
||||||
def process_IN_MOVED_TO(self, event):
|
def process_IN_MOVED_TO(self, event):
|
||||||
#self.logger.info("===> handling: '%s'" % str(event))
|
#self.logger.info("===> handling: '%s'" % str(event))
|
||||||
#self.process_to_organize(event)
|
self.process_to_organize(event)
|
||||||
pass
|
|
||||||
|
|
||||||
def process_default(self, event):
|
|
||||||
pass
|
|
||||||
#self.logger.info("===> Not handling: '%s'" % str(event))
|
|
||||||
|
|
||||||
def flush_events(self, path):
|
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):
|
for f in mmp.walk_supported(path, clean_empties=True):
|
||||||
self.logger.info("Bootstrapping: File in 'organize' directory: \
|
self.logger.info("Bootstrapping: File in 'organize' directory: \
|
||||||
'%s'" % f)
|
'%s'" % f)
|
||||||
dispatcher.send(signal=self.signal, sender=self,
|
if not mmp.file_locked(f):
|
||||||
event=OrganizeFile(f))
|
dispatcher.send(signal=self.signal, sender=self,
|
||||||
|
event=OrganizeFile(f))
|
||||||
flushed += 1
|
flushed += 1
|
||||||
#self.logger.info("Flushed organized directory with %d files" % flushed)
|
#self.logger.info("Flushed organized directory with %d files" % flushed)
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ class ManagerTimeout(threading.Thread,Loggable):
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
time.sleep(self.interval) # every 3 seconds
|
time.sleep(self.interval)
|
||||||
self.manager.flush_organize()
|
self.manager.flush_organize()
|
||||||
|
|
||||||
class Manager(Loggable):
|
class Manager(Loggable):
|
||||||
|
@ -178,7 +178,7 @@ class Manager(Loggable):
|
||||||
# the OrganizeListener instance will walk path and dispatch an organize
|
# the OrganizeListener instance will walk path and dispatch an organize
|
||||||
# event for every file in that directory
|
# event for every file in that directory
|
||||||
self.organize['organize_listener'].flush_events(new_path)
|
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):
|
def flush_organize(self):
|
||||||
path = self.organize['organize_path']
|
path = self.organize['organize_path']
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import time
|
|
||||||
import media.monitor.pure as mmp
|
import media.monitor.pure as mmp
|
||||||
import media.monitor.owners as owners
|
import media.monitor.owners as owners
|
||||||
from media.monitor.handler import ReportHandler
|
from media.monitor.handler import ReportHandler
|
||||||
|
@ -72,13 +71,9 @@ class Organizer(ReportHandler,Loggable):
|
||||||
directory=d)
|
directory=d)
|
||||||
return cb
|
return cb
|
||||||
|
|
||||||
time.sleep(0.02)
|
|
||||||
|
|
||||||
mmp.magic_move(event.path, new_path,
|
mmp.magic_move(event.path, new_path,
|
||||||
after_dir_make=new_dir_watch(dirname(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
|
# 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
|
# backwards way is bewcause we are unable to encode the owner id
|
||||||
# into the file itself so that the StoreWatchListener listener can
|
# into the file itself so that the StoreWatchListener listener can
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import copy
|
import copy
|
||||||
|
from subprocess import Popen, PIPE
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
import math
|
import math
|
||||||
|
@ -165,6 +166,12 @@ def walk_supported(directory, clean_empties=False):
|
||||||
for fp in full_paths: yield fp
|
for fp in full_paths: yield fp
|
||||||
if clean_empties: clean_empty_dirs(directory)
|
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):
|
def magic_move(old, new, after_dir_make=lambda : None):
|
||||||
"""
|
"""
|
||||||
Moves path old to new and constructs the necessary to directories for new
|
Moves path old to new and constructs the necessary to directories for new
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue