加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
notify.py 2.49 KB
一键复制 编辑 原始数据 按行查看 历史
walkskyer 提交于 2020-08-20 11:03 . 完善功能
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
from multiprocessing import Process
import time
import util.sign as sign
import util.op as op
import traceback
from pyinotify import WatchManager, Notifier, \
ProcessEvent, IN_DELETE, IN_CREATE, IN_MODIFY, IN_CLOSE_WRITE
notify_ext = tuple(op.cfg.get('file', 'ext').split(','))
"""
监听程序,本程序只能在linux环境下测试
"""
class EventHandler(ProcessEvent):
"""事件处理"""
def process_IN_DELETE(self, event):
file_path = os.path.abspath(os.path.join(event.path, event.name))
print "Delete file: %s " % file_path
def process_IN_CLOSE_WRITE(self,event):
file_path = os.path.abspath(os.path.join(event.path, event.name))
p = Process(target=check_file, args=(file_path,))
p.start()
p.join()
def check_file(file_path):
"""
检查文件合法性
:param file_path:
:return:
"""
print "Close file: %s " % file_path
ext = file_path[file_path.rfind('.')+1:]
if (ext not in notify_ext) or (not os.path.isfile(file_path)):
print 'file type (%s) is not in %s or file is not exist:%s' % (ext, notify_ext, file_path)
return True
result = sign.sign_check(file_path)
if not result:
print 'sign file error:%s' % file_path
#op.add_file(file_path)
#op.add_file_sql(file_path)
#op.move_file(file_path)
return result
print 'sign file ok:%s' % file_path
return result
def FSMonitor(path='.'):
if len(path) <= 0:
print 'There is no folder to monitor,Bye!'
return
if isinstance(path,unicode):
path = path.encode('utf-8')
wm = WatchManager()
# IN_DELETE | IN_CREATE | IN_MODIFY | IN_CLOSE_WRITE
mask = IN_CLOSE_WRITE
notifier = Notifier(wm, EventHandler())
paths = path.split(';')
for path in paths:
if not path:
continue
wm.add_watch(path, mask, auto_add=True, rec=True)
print 'now starting monitor: %s' % path
while True:
try:
notifier.process_events()
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt, e:
print e
notifier.stop()
break
if __name__ == "__main__":
cfg = op.cfg
# 守护总是从异常恢复
while True:
try:
FSMonitor(cfg.get('file', 'notify_dir'))
except Exception, e:
traceback.print_exc(file=open(op.log_path + '/trace.log','w+'))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化