代码拉取完成,页面将自动刷新
同步操作将从 e23-cn/e23-file-check 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#!/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+'))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。