python实现inotify功能模块举例 pyinotify

#! /usr/bin/env python 
# -*- coding: utf-8 -*-


import os
import sys
import re
import pdb
import time
import datetime
import atexit
import urllib2
import urllib
import signal
from string import ljust, center
from optparse import OptionParser
from pyinotify import WatchManager, Notifier, ProcessEvent, EventsCodes


FLAG_COLLECTIONS = EventsCodes.FLAG_COLLECTIONS


#IN_DELETE, IN_CREATE, IN_CLOSE_WRITE, IN_MOVED_FROM, IN_MOVED_TO
g_strMonitorLog = ""


def appendLog(strIn, logName="./monitor.run.log"):
    global g_strMonitorLog
    if g_strMonitorLog != "":
        logName = g_strMonitorLog
    fp = file(logName, 'at')
    fp.write(strIn + '\n')
    fp.close()




def hand_hub(n=0, e=0):
    print 'catch a hub SIGINT'
 
         
class proc_evt(ProcessEvent):
    def process_IN_CREATE(self, event):
        print ('[%s] IN_CREATE   %s/%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), event.path, event.name))
        appendLog('[%s] IN_CREATE   %s/%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), event.path, event.name))
 
    def process_IN_DELETE(self, event):
        print ('[%s] DELETE      %s/%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), event.path, event.name))
        appendLog('[%s] DELETE      %s/%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), event.path, event.name))
        
    def process_IN_CLOSE_WRITE(self, event):
        print ('[%s] CLOSE_WRITE %s/%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), event.path, event.name))
        appendLog('[%s] CLOSE_WRITE %s/%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), event.path, event.name))
          
    def process_IN_MOVED_FROM(self, event):
        print ('[%s] MOVE_FROM   %s/%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), event.path, event.name))
        appendLog('[%s] MOVE_FROM   %s/%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), event.path, event.name))
     
    def process_IN_MOVED_TO(self, event):
        print ('[%s] MOVE_TO     %s/%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), event.path, event.name))
        appendLog('[%s] MOVE_TO     %s/%s' % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), event.path, event.name))
 
class notify(object):
     
    def __init__(self, local_rootpath):
        self.wm = WatchManager()
        mask = FLAG_COLLECTIONS['OP_FLAGS']['IN_DELETE'] | FLAG_COLLECTIONS['OP_FLAGS']['IN_CREATE'] | FLAG_COLLECTIONS['OP_FLAGS']['IN_CLOSE_WRITE'] | FLAG_COLLECTIONS['OP_FLAGS']['IN_MOVED_FROM'] | FLAG_COLLECTIONS['OP_FLAGS']['IN_MOVED_TO']
         
        self.notifier = Notifier(self.wm, proc_evt())
        self.wm.add_watch(local_rootpath, mask, rec = True)
         
         
    def run(self):
        signal.signal(signal.SIGHUP, hand_hub)
         
        while (True):
                self.notifier.process_events()
                if self.notifier.check_events():
                    self.notifier.read_events() 
            #except:
            #    self.notifier.stop()
            #    break
             
parser = OptionParser()
parser.add_option("-p", "--path",  dest="path",  help="[in] monitor path")
parser.add_option("-l", "--log",  type="string", default="./monitor.run.log",  dest="log",  help="[out] log path")


if __name__ == '__main__':
    options, args = parser.parse_args()


    assert(options.path != None)
    if options.path != "":
        g_strMonitorLog = options.log
    nty = notify(options.path)
    nty.run()

 
 
  
  
  
  

你可能感兴趣的:(python实现inotify功能模块举例 pyinotify)