- import pyinotify
- import MySQLdb
- import time
- import sys
- DB_USER = 'root'
- DB_PASS = ''
- DB_HOST = 'db1.testing.internal'
- DB_DB = 'nodehost_scanner'
- try:
- conn = MySQLdb.connect(passwd=DB_PASS,db=DB_DB,host=DB_HOST,user=DB_USER)
- except:
- print "Could not connect to the database :("
- sys.exit(5)
- wm = pyinotify.WatchManager()
- class EventHandler(pyinotify.ProcessEvent):
- def process_IN_CREATE(self, event): # File has been created
- try:
- cursor = conn.cursor()
- data = cursor.execute("SELECT * FROM queue WHERE status != 'P' AND file = '%s'" % (event.pathname)) # Get stuff that isin't in processing
- cursor.close()
- if data == 0: # This isn't in the queue already
- cursor = conn.cursor()
- 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!
- cursor.close()
- except Exception, e:
- print "Could not add %s into the queue" % (event.pathname)
- print e
- def process_IN_MODIFY(self, event): # File has been modified
- try:
- cursor = conn.cursor()
- data = cursor.execute("SELECT * FROM queue WHERE status != 'P' AND file = '%s'" % (event.pathname)) # Get stuff that isin't in processing
- cursor.close()
- if data == 0: # This isn't in the queue already
- cursor = conn.cursor()
- 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!
- cursor.close()
- except Exception, e:
- print "Could not add %s into the queue" % (even.pathname)
- print e
- def process_IN_DELETE(self, event): # If this is a delete then remove it from the queue
- try:
- cursor = conn.cursor()
- data = cursor.execute("SELECT * FROM queue WHERE status != 'P' AND file = '%s'" % (event.pathname)) # Get stuff that isin't in processing
- rows = cursor.fetchall()
- cursor.close()
- if data != 0: # This is in the queue
- for row in rows:
- cursor = conn.cursor()
- cursor.execute("DELETE FROM `queue` WHERE id = '%s'" % (row[0])) # Remove it!
- cursor.close()
- except Exception, e:
- print "Could not remove %s from the queue" % (event.pathname)
- print e
- def process_IN_MOVED_FROM(self, event): # File has been moved out of this dir
- try:
- cursor = conn.cursor()
- data = cursor.execute("SELECT * FROM queue WHERE status != 'P' AND file = '%s'" % (event.pathname)) # Get stuff that isin't in processing
- rows = cursor.fetchall()
- cursor.close()
- if data != 0: # This is in the queue
- for row in rows:
- cursor = conn.cursor()
- cursor.execute("DELETE FROM `queue` WHERE id = '%s'" % (row[0])) # Remove it!
- cursor.close()
- except Exception, e:
- print "Could not remove %s from the queue" % (event.pathname)
- print e
- def process_IN_MOVED_TO(self, event): # File has been moved into this dir
- try:
- cursor = conn.cursor()
- data = cursor.execute("SELECT * FROM queue WHERE status != 'P' AND file = '%s'" % (event.pathname)) # Get stuff that isin't in processing
- cursor.close()
- if data == 0: # This isn't in the queue already
- cursor = conn.cursor()
- 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!
- cursor.close()
- except Exception, e:
- print "Could not remove %s from the queue" % (event.pathname)
- print e
- try:
- notifier = pyinotify.ThreadedNotifier(wm, EventHandler())
- notifier.start()
- 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)
- notifier.loop()
- except KeyboardInterrupt:
- wm.rm_watch(wdd.values())
- notifier.stop()