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

发布于 2022-08-28 23:22:41 字数 7123 浏览 11 评论 0

我用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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2022-09-04 23:22:41

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

参见: Events types

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文