目录变动监视 ::-- ZoomQuiet [DateTime(2007-07-23T14:42:41Z)] TableOfContents

Include(CPUGnav)

1. 推荐.监控目录变化的脚本真不错

{{{[email protected]" <[email protected]> hide details 6:03 pm (3½ hours ago)

}}}

   1 #!/usr/local/bin/python2.4
   2 # ex: ts=4
   3 #
   4 # $Id: test.py,v 1.7 2003/01/29 21:39:08 gurubert Exp $
   5 #
   6 
   7 import os, sys
   8 
   9 if os.name != 'posix':
  10        print 'I am sorry, but this script will run only on POSIX
  11 platforms.'
  12        sys.exit(1)
  13 
  14 import _fam, time, signal, getopt, errno, select
  15 
  16 # hash of filename => TestRequest
  17 requests = {}
  18 home="/home/www/html"
  19 showreqid = None
  20 suspend = None
  21 cont = None
  22 intr = None
  23 usr1 = None
  24 usr2 = None
  25 
  26 class TestRequest:
  27        def __init__(self, userData, isDir):
  28                self.fr = None
  29                self.userData = userData
  30                self.isDir = isDir
  31 
  32 def handle_stop(signum, frame):
  33        global suspend
  34 
  35        print 'Suspended!'
  36        suspend = 1
  37 
  38 def handle_cont(signum, frame):
  39        global cont
  40 
  41        print 'Resumed!'
  42        signal.signal(signal.SIGCONT, handle_cont)
  43        cont = 1
  44 
  45 def handle_int(signum, frame):
  46        global intr
  47 
  48        print 'Interupted!'
  49        signal.signal(signal.SIGINT, handle_int)
  50        intr = 1
  51 
  52 def handle_usr1(signum, frame):
  53        global usr1
  54 
  55        print 'Got USR1!'
  56        signal.signal(signal.SIGUSR1, handle_usr1)
  57        usr1 = 1
  58 
  59 def handle_usr2(signum, frame):
  60        global usr2
  61 
  62        print 'Got USR2!'
  63        signal.signal(signal.SIGUSR2, handle_usr2)
  64        usr2 = 1
  65 
  66 def sendRequests():
  67        for path in requests.keys():
  68                if requests[path].isDir:
  69                        requests[path].fr = fc.monitorDirectory(path,
  70 requests[path].userData)
  71                else:
  72                        requests[path].fr = fc.monitorFile(path,
  73 requests[path].userData)
  74 
  75 def processDirEvents(fe):
  76        code=fe.code2str()
  77        if code!="created" and code!="changed" and code!="deleted" and
  78 code!="moved":
  79                return
  80        if fe.filename[0]=="/":
  81                filename=fe.filename
  82        else:
  83                filename="%s/%s" % (fe.userData,fe.filename)
  84        print filename,code
  85        if code=="deleted":
  86                if requests.has_key(filename):
  87                        del(requests[filename])
  88                        print "dir %s deleted" % filename
  89        if code=="created":
  90                if os.path.isdir(filename):
  91                        if not requests.has_key(filename):
  92                                requests[filename] = TestRequest('%s'
  93 % filename, 1)
  94                                requests[filename].fr =
  95 fc.monitorDirectory(filename, requests[filename].userData)
  96                                print "dir %s added into monitor" %
  97 filename
  98 fc = _fam.open()
  99 
 100 for d in os.walk(home):
 101                requests[d[0]] = TestRequest('%s' % d[0], 1)
 102 
 103 signal.signal(signal.SIGTSTP, handle_stop)
 104 signal.signal(signal.SIGCONT, handle_cont)
 105 signal.signal(signal.SIGINT, handle_int)
 106 signal.signal(signal.SIGUSR1, handle_usr1)
 107 signal.signal(signal.SIGUSR2, handle_usr2)
 108 
 109 sendRequests()
 110 lastevents=0
 111 while 1:
 112        if suspend:
 113                for request in requests.values():
 114                        request.fr.suspendMonitor()
 115                        print 'Suspended monitoring of request %i' %
 116 request.fr.requestID()
 117                suspend = None
 118                signal.signal(signal.SIGTSTP, handle_stop)
 119        if cont:
 120                for request in requests.values():
 121                        request.fr.resumeMonitor()
 122                        print 'Resumed monitoring of request %i' %
 123 request.fr.requestID()
 124                cont = None
 125        if intr:
 126                # The dealloc methods of all objects should handle
 127                # FAMCancelMonitor and FAMClose
 128                sys.exit(0)
 129        if usr1:
 130                # Cancel all requests, close the connection, and
 131 reopen it.
 132                # This makes sure long-lived clients can connect,
 133 monitor, and
 134                # disconnect repeatedly without leaking.
 135                usr1 = None
 136                sleeptime = 1
 137                for request in requests.values():
 138                        print 'Cancelling monitoring of request %i' %
 139 request.fr.requestID()
 140                        request.fr.cancelMonitor()
 141                fc.close()
 142                print 'Closed connection, sleeping %d...' % sleeptime
 143                time.sleep(sleeptime)
 144                fc = _fam.open()
 145                sendRequests()
 146        if usr2:
 147                # Clean things up like a well-behaved client and exit.
 148                for request in requests.values():
 149                        print 'Cancelling monitoring of request %i' %
 150 request.fr.requestID()
 151                        request.fr.cancelMonitor()
 152                fc.close()
 153                print 'Closed connection'
 154                sys.exit(0)
 155        thisevents=0
 156        try:
 157                ri, ro, re = select.select([fc], [], [],5)
 158        except select.error, er:
 159                errnumber, strerr = er
 160                if errnumber == errno.EINTR:
 161                        continue
 162                else:
 163                        print strerr
 164                        sys.exit(1)
 165        while fc.pending():
 166                fe = fc.nextEvent()
 167                processDirEvents(fe)
 168                thisevents=thisevents+1
 169        if thisevents==0:
 170                print "lastevents %s" % lastevents
 171                if lastevents>0:
 172                        os.system("/home/www/www.sync.sh")
 173                lastevents=0
 174        else:
 175                lastevents=thisevents+lastevents

1.1. 建议

{{{HuangJiahua <[email protected]> hide details 7:47 pm (2 hours ago)

}}}

Linux 下还是用 python-pyinotify 更好些,
旧版本 linux 可以用 python-gamin

1.2. 反馈

PageComment2