Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 185314fd456c7d06372888b7b2e81a6ef35850de @jbohman committed May 31, 2010
@@ -0,0 +1 @@
+Logsandra
No changes.
No changes.
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+import sys
+from optparse import OptionParser
+
+# Try to import pyinotify handler, else standard handler
+try:
+ from watchers.inotify import InotifyWatcher as Watcher
+except ImportError:
+ from watchers.standard import StandardWatcher as Watcher
+
+def my_callback(data):
+ print data
+
+# Run it
+if __name__ == '__main__':
+ usage = 'usage: %prog [options] path [path ...]'
+ parser = OptionParser(usage=usage)
+ parser.add_option('-r', '--rescan-freq', dest='rescan_freq', help='rescan frequency in seconds', metavar='SECONDS', default=20)
+ parser.add_option('-u', '--update-freq', dest='update_freq', help='update frequnecy in seconds', metavar='SECONDS', default=0)
+ parser.add_option('--recursive', action='store_true', dest='recursive')
+ (options, args) = parser.parse_args()
+
+ if not args:
+ print "Need atleast one path (file or directory) to monitor, see --help"
+ sys.exit(1)
+
+
+ settings = {'freq': options.update_freq, 'rescan': options.rescan_freq}
+
+ entities = []
+ for arg in args:
+ entities.append({'name': args, 'recursive': options.recursive})
+
+ w = Watcher(settings, entities, my_callback)
+ w.loop()
No changes.
Binary file not shown.
@@ -0,0 +1,25 @@
+import pyinotify
+
+class EventHandler(pyinotify.ProcessEvent):
+ def my_init(self, callback):
+ self.callback = callback
+
+ def process_IN_MODIFY(self, event):
+ self.callback(event)
+
+class InotifyWatcher(object):
+
+ def __init__(self, settings, entities, callback):
+ self.settings = settings
+ self.entities = entities
+ self.callback = callback
+ self.wm = pyinotify.WatchManager()
+
+ pyinotify.log.setLevel(50)
+
+ def loop(self):
+ notifier = pyinotify.Notifier(self.wm, EventHandler(callback=self.callback), self.settings['freq'])
+ for entity in self.entities:
+ self.wm.add_watch(entity['name'], pyinotify.IN_MODIFY, rec=entity['recursive'])
+
+ notifier.loop()
Binary file not shown.
@@ -0,0 +1,81 @@
+import os, os.path, time
+
+class StandardWatcher(object):
+
+ def __init__(self, settings, entities, callback):
+ self.settings = settings
+ self.entities = entities
+ self.callback = callback
+ self.files = {}
+
+ for filename in self._find_files_generator():
+ self.files[filename] = self._mtime(filename)
+
+ self._last_rescan_time = time.time()
+
+ def loop(self):
+ while True:
+ current_time = time.time()
+ if (current_time - self._last_rescan_time) > self.settings['rescan']:
+ self._last_rescan_time = self._rescan()
+
+ reference_time = time.time()
+ for filename, mtime in self.files.iteritems():
+ new_mtime = self._mtime(filename)
+ if new_mtime > mtime:
+ self.files[filename] = new_mtime
+ self.callback(filename)
+
+ if self.settings['freq'] > 0:
+ current_time = time.time()
+ sleep = self.settings['freq'] - (current_time - reference_time)
+ if sleep > 0:
+ time.sleep(sleep)
+
+ def _find_files_generator(self):
+ for entity in self.entities:
+ # Is directory
+ if os.path.isdir(entity['name']):
+ if entity['recursive']:
+ for path in os.walk(entity['name']):
+ if path[2]:
+ for filename in path[2]:
+ filename = os.path.join(os.path.abspath(path[0]), filename)
+ yield filename
+ else:
+ for filename in os.listdir(entity['name']):
+ filename = os.path.abspath(entity['name']) + '/' + filename
+ if os.path.isfile(filename):
+ yield filename
+ # Is file
+ else:
+ filename = os.path.join(os.path.abspath(entity['name']), entity['name'])
+ yield filename
+
+ def _rescan(self):
+ tempfiles = {}
+ for filename in self._find_files_generator():
+ if filename not in self.files:
+ self.files[filename] = self._mtime(filename)
+ tempfiles[filename] = 0
+
+ result = set(self.files).difference(set(tempfiles))
+ for element in result:
+ del self.files[element]
+
+ return time.time()
+
+
+ def _mtime(self, filename):
+ try:
+ return os.stat(filename).st_mtime
+ except os.error:
+ return False
+
+
+if __name__ == '__main__':
+ def callback(x):
+ print x
+
+ sw = StandardWatcher({'freq': 1, 'rescan': 10}, [{'name': '.', 'recursive': False}], callback)
+ sw.loop()
Binary file not shown.

0 comments on commit 185314f

Please sign in to comment.