Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 3.78 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import pyinotify
  2. import MySQLdb
  3. import time
  4. import sys
  5. DB_USER = 'root'
  6. DB_PASS = ''
  7. DB_HOST = 'db1.testing.internal'
  8. DB_DB = 'nodehost_scanner'
  9.  
  10. try:
  11.         conn = MySQLdb.connect(passwd=DB_PASS,db=DB_DB,host=DB_HOST,user=DB_USER)
  12. except:
  13.         print "Could not connect to the database :("
  14.         sys.exit(5)
  15.  
  16. wm = pyinotify.WatchManager()
  17.  
  18. class EventHandler(pyinotify.ProcessEvent):
  19.         def process_IN_CREATE(self, event): # File has been created
  20.                 try:
  21.                         cursor = conn.cursor()
  22.                         data = cursor.execute("SELECT * FROM queue WHERE status != 'P' AND file = '%s'" % (event.pathname)) # Get stuff that isin't in processing
  23.                         cursor.close()
  24.  
  25.                         if data == 0: # This isn't in the queue already
  26.                                 cursor = conn.cursor()
  27.                                 cursor.execute("INSERT INTO `queue` (id, file, status, insert_time, update_time) VALUES (NULL, '%s', 'N', '%d', NULL)" % (event.pathname, int(time.time()))) # Add it!
  28.                                 cursor.close()
  29.                 except Exception, e:
  30.                         print "Could not add %s into the queue" % (event.pathname)
  31.                         print e
  32.        
  33.         def process_IN_MODIFY(self, event): # File has been modified
  34.                 try:
  35.                         cursor = conn.cursor()
  36.                         data = cursor.execute("SELECT * FROM queue WHERE status != 'P' AND file = '%s'" % (event.pathname)) # Get stuff that isin't in processing
  37.                         cursor.close()
  38.  
  39.                         if data == 0: # This isn't in the queue already
  40.                                 cursor = conn.cursor()
  41.                                 cursor.execute("INSERT INTO `queue` (id, file, status, insert_time, update_time) VALUES (NULL, '%s', 'N', '%d', NULL)" % (event.pathname, int(time.time()))) # Add it!
  42.                                 cursor.close()
  43.                 except Exception, e:
  44.                         print "Could not add %s into the queue" % (even.pathname)
  45.                         print e
  46.        
  47.         def process_IN_DELETE(self, event): # If this is a delete then remove it from the queue
  48.                 try:
  49.                         cursor = conn.cursor()
  50.                         data = cursor.execute("SELECT * FROM queue WHERE status != 'P' AND file = '%s'" % (event.pathname)) # Get stuff that isin't in processing
  51.                         rows = cursor.fetchall()
  52.                         cursor.close()
  53.  
  54.                         if data != 0: # This is in the queue
  55.                                 for row in rows:
  56.                                         cursor = conn.cursor()
  57.                                         cursor.execute("DELETE FROM `queue` WHERE id = '%s'" % (row[0])) # Remove it!
  58.                                         cursor.close()
  59.                 except Exception, e:
  60.                         print "Could not remove %s from the queue" % (event.pathname)
  61.                         print e
  62.  
  63.         def process_IN_MOVED_FROM(self, event): # File has been moved out of this dir
  64.                 try:
  65.                         cursor = conn.cursor()
  66.                         data = cursor.execute("SELECT * FROM queue WHERE status != 'P' AND file = '%s'" % (event.pathname)) # Get stuff that isin't in processing
  67.                         rows = cursor.fetchall()
  68.                         cursor.close()
  69.        
  70.                         if data != 0: # This is in the queue
  71.                                 for row in rows:
  72.                                         cursor = conn.cursor()
  73.                                         cursor.execute("DELETE FROM `queue` WHERE id = '%s'" % (row[0])) # Remove it!
  74.                                         cursor.close()
  75.                 except Exception, e:
  76.                         print "Could not remove %s from the queue" % (event.pathname)
  77.                         print e
  78.        
  79.         def process_IN_MOVED_TO(self, event): # File has been moved into this dir
  80.                 try:
  81.                         cursor = conn.cursor()
  82.                         data = cursor.execute("SELECT * FROM queue WHERE status != 'P' AND file = '%s'" % (event.pathname)) # Get stuff that isin't in processing
  83.                         cursor.close()
  84.        
  85.                         if data == 0: # This isn't in the queue already
  86.                                 cursor = conn.cursor()
  87.                                 cursor.execute("INSERT INTO `queue` (id, file, status, insert_time, update_time) VALUES (NULL, '%s', 'N', '%d', NULL)" % (event.pathname, int(time.time()))) # Add it!
  88.                                 cursor.close()
  89.                 except Exception, e:
  90.                         print "Could not remove %s from the queue" % (event.pathname)
  91.                         print e
  92.  
  93. try:
  94.         notifier = pyinotify.ThreadedNotifier(wm, EventHandler())
  95.         notifier.start()
  96.  
  97.         wdd = wm.add_watch('/var/www/', pyinotify.IN_CREATE | pyinotify.IN_MODIFY | pyinotify.IN_DELETE | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO, rec=True)
  98.         notifier.loop()
  99. except KeyboardInterrupt:
  100.         wm.rm_watch(wdd.values())
  101.         notifier.stop()