首页 > pyinotify监控cp操作-存在子目录时仅有create事件检测不到WRITE事件

pyinotify监控cp操作-存在子目录时仅有create事件检测不到WRITE事件

我用pyinotify监控一个目录,当我用cp拷贝一个文件夹(内含多级子目录)到监控目录时,仅仅监控到create事件,我需要的IN_CLOSE_WRITE事件没有监控到,

但是如果我cp的目录没有子目录,其下仅有文件时又能获取IN_CLOSE_WRITE事件。。

我这里是在事件里面加了些过滤以及判断,如果去掉这些过滤和判断,单独针对某一个事件设定执行动作就木有问题。

请问有什么合适的方式实现过滤掉某些文件?

监控的事件样例输出如下:

root@gizl:~/python# python pyinotify_cp_dir_filter.py /root/python/
daemon...
DEBUG:PTmp:event name:IN_CREATE|IN_ISDIR,dir /root/python/test6 is created.
DEBUG:PTmp:event name:IN_CREATE,file /root/python/test6/test.txt is created.
DEBUG:PTmp:event name:IN_CLOSE_WRITE,action of /root/python/test6/test.txt is edit
DEBUG:mirror:s3sync edit file /root/python/test6/test.txt
DEBUG:PTmp:event name:IN_CREATE|IN_ISDIR,dir /root/python/test5 is created.
DEBUG:PTmp:event name:IN_CREATE,file /root/python/test5/test6.txt is created.
DEBUG:PTmp:event name:IN_CLOSE_WRITE,action of /root/python/test5/test6.txt is edit
DEBUG:mirror:s3sync edit file /root/python/test5/test6.txt
DEBUG:PTmp:event name:IN_CREATE|IN_ISDIR,dir /root/python/test5/test6 is created.
DEBUG:PTmp:event name:IN_CREATE,file /root/python/test5/test6/test.txt is created.
DEBUG:PTmp:event name:IN_CLOSE_WRITE,action of /root/python/test5/test6/test.txt is edit
DEBUG:mirror:s3sync edit file /root/python/test5/test6/test.txt
DEBUG:PTmp:event name:IN_CREATE|IN_ISDIR,dir /root/python/test4 is created.
DEBUG:PTmp:event name:IN_CREATE,file /root/python/test4/test5.txt is created.
DEBUG:PTmp:event name:IN_CREATE|IN_ISDIR,dir /root/python/test4/test5 is created.
DEBUG:PTmp:event name:IN_CREATE,file /root/python/test4/test5/test6.txt is created.
DEBUG:PTmp:event name:IN_CREATE|IN_ISDIR,dir /root/python/test4/test5/test6 is created.
DEBUG:PTmp:event name:IN_CREATE,file /root/python/test4/test5/test6/test.txt is created.
DEBUG:PTmp:event name:IN_CREATE|IN_ISDIR,dir /root/python/test3 is created.
DEBUG:PTmp:event name:IN_CREATE,file /root/python/test3/test4.txt is created.
DEBUG:PTmp:event name:IN_CREATE|IN_ISDIR,dir /root/python/test3/test4 is created.
DEBUG:PTmp:event name:IN_CREATE,file /root/python/test3/test4/test5.txt is created.
DEBUG:PTmp:event name:IN_CREATE|IN_ISDIR,dir /root/python/test3/test4/test5 is created.
DEBUG:PTmp:event name:IN_CREATE,file /root/python/test3/test4/test5/test6.txt is created.
DEBUG:PTmp:event name:IN_CREATE|IN_ISDIR,dir /root/python/test3/test4/test5/test6 is created.
DEBUG:PTmp:event name:IN_CREATE,file /root/python/test3/test4/test5/test6/test.txt is created.

程序源码如下:

import sys
import pyinotify
import re
import logging
import os

def mirror(command):
    logger=logging.getLogger('mirror')
    try:
        logger.debug(command)
    except OSError as ose:
        logger.debug( 'Execution failed:')
        logger.debug(  ose )

class PTmp(pyinotify.ProcessEvent):    
    def process_IN_MOVED_FROM(self, event):
        logger=logging.getLogger('PTmp')
        if not EXCLUDE_PAT.match(event.pathname):
            if os.path.isdir(event.pathname):
                logger.debug('event name:'+event.maskname+',action of dir ' + event.pathname + ' is delete')
                S3_COMMAND = S3_PATH + ' delete dir ' + event.pathname
                mirror(S3_COMMAND)
            else:
                logger.debug('event name:'+event.maskname+',action of file ' + event.pathname + ' is delete')
                S3_COMMAND = S3_PATH + ' delete file ' + event.pathname
                mirror(S3_COMMAND)

    def process_IN_MOVED_TO(self, event):
        logger=logging.getLogger('PTmp')
        if not EXCLUDE_PAT.match(event.pathname):
            if os.path.isdir(event.pathname):
                logger.debug('event name:'+event.maskname+',action of dir ' + event.pathname + ' is edit')
                S3_COMMAND = S3_PATH + ' edit dir ' + event.pathname
                mirror(S3_COMMAND)
            else:
                logger.debug('event name:'+event.maskname+',action of file ' + event.pathname + ' is edit')
                S3_COMMAND = S3_PATH + ' edit file ' + event.pathname
                mirror(S3_COMMAND)
        else:
            logger.debug('event name:'+event.maskname+',file '+ event.pathname +' match exclude pattens.')

    def process_IN_DELETE(self, event):
        logger=logging.getLogger('PTmp')
        if not EXCLUDE_PAT.match(event.pathname):
            if os.path.isdir(event.pathname):
                logger.debug('event name:'+event.maskname+',action of dir ' + event.pathname + ' is delete')
            else:
                logger.debug('event name:'+event.maskname+',action of file ' + event.pathname + ' is delete')
                S3_COMMAND = S3_PATH + ' delete file ' + event.pathname
                mirror(S3_COMMAND)

    def process_IN_DELETE_SELF(self, event):
        logger=logging.getLogger('PTmp')
        if not EXCLUDE_PAT.match(event.pathname):
            if os.path.isdir(event.pathname):
                logger.debug('event name:'+event.maskname+',action of dir ' + event.pathname + ' is delete')
            else:
                logger.debug('event name:'+event.maskname+',action of file ' + event.pathname + ' is delete')
                S3_COMMAND = S3_PATH + ' delete file ' + event.pathname
                mirror(S3_COMMAND)

    def process_IN_CLOSE_WRITE(self, event):
        logger=logging.getLogger('PTmp')
        if not EXCLUDE_PAT.match(event.pathname):
            logger.debug('event name:'+event.maskname+',action of ' + event.pathname + ' is edit')
            S3_COMMAND = S3_PATH + ' edit file ' + event.pathname
            mirror(S3_COMMAND)
        else:
            logger.debug('event name:'+event.maskname+',file '+ event.pathname +' match exclude pattens.')

    def process_IN_CREATE(self, event):
        logger=logging.getLogger('PTmp')
        if not EXCLUDE_PAT.match(event.pathname):
            if os.path.isdir(event.pathname):
                logger.debug('event name:'+event.maskname+',dir ' + event.pathname + ' is created.')
            else:
                logger.debug('event name:'+event.maskname+',file ' + event.pathname + ' is created.')
        else:
            logger.debug('event name:'+event.maskname+',file '+ event.pathname +' match exclude pattens.')

def main():
    global EXCLUDE_PAT,S3_PATH
    S3_PATH='s3sync'
    EXCLUDE_PAT=re.compile('.*\/\..*\.sw|.*\/.*~')
    logger = logging.getLogger('s3sync')
    log_level = logging.DEBUG
    logging.basicConfig(level=log_level,)
    PIDFILE='./pidfile'
    FREQ=0
    wmg = pyinotify.WatchManager()
    mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_DELETE | \
           pyinotify.IN_MOVED_TO | pyinotify.IN_MOVED_FROM | \
           pyinotify.IN_DELETE_SELF | pyinotify.IN_CREATE  |  \
           pyinotify.IN_MODIFY
    ptm = PTmp()
    notifier = pyinotify.Notifier(wmg, ptm, read_freq=FREQ)
    try:
        wmg.add_watch(sys.argv[1], mask, rec=True, auto_add=True)
    except pyinotify.WatchManagerError as err:
        logger.warn(err)
        logger.warn(err.wmd)
    print('daemon...')
    notifier.loop(pid_file=PIDFILE)

if __name__ == "__main__":
    main()

IN_CLOSE_WRITE : a writable file was closed
只有fileclosed的时候会触发IN_CLOSE_WRITE, directory不会触发此事件.

参见: Events types

【热门文章】
【热门文章】