Advertisement
Guest User

Untitled

a guest
Oct 21st, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import os
  4. import pyinotify
  5. import logging
  6. import time
  7.  
  8. logging.basicConfig(level=logging.DEBUG)
  9. log = logging.getLogger(__name__)
  10.  
  11. class EventHandler(pyinotify.ProcessEvent):
  12.     def __init__(self, wm, targets):
  13.         self.targets = targets
  14.         self.wm = wm
  15.         self.results = None
  16.  
  17.     def process_IN_CREATE(self, event):
  18.         log.debug("recieved inotify {} event for {}".format(event.maskname, event.pathname))
  19.         if event.pathname in self.targets:
  20.             if event.maskname == self.targets[event.pathname]:
  21.                 self.event_triggered(event)
  22.  
  23.     def process_IN_DELETE(self, event):
  24.         print "Removing:", event.pathname
  25.  
  26.     def event_triggered(self, event):
  27.         if not self.results:
  28.             self.results = []
  29.  
  30.         self.results.append(event)
  31.  
  32.         # all done?
  33.         if len(self.results) == len(self.targets):
  34.             self.teardown()
  35.  
  36.     def teardown(self):
  37.         log.debug("tearing down inotify listener")
  38.         try:
  39.             self.wm.close()
  40.         except OSError:
  41.             pass
  42.  
  43. def exists(name, timeout_secs=10, **kwargs):
  44.     path = name
  45.     ret = {'name': path, 'changes': {}, 'result': True, 'comment': ''}
  46.     return ret
  47.  
  48.     if os.path.exists(path):
  49.         msg = "path {} already exists, taking no action".format(path)
  50.         log.debug(msg)
  51.         ret['comment'] = msg
  52.         return ret
  53.  
  54.     path_to_watch = os.path.dirname(path)
  55.  
  56.     mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE
  57.  
  58.     wm = pyinotify.WatchManager()
  59.     wm.add_watch(path_to_watch, mask)
  60.  
  61.     targets = {path: "IN_CREATE", "/tmp/baz": "IN_CREATE"}
  62.     handler = EventHandler(wm, targets)
  63.     notifier = pyinotify.Notifier(wm, handler, timeout=1000 * timeout_secs)
  64.  
  65.     log.debug("about to wait on inotify for {} seconds".format(timeout_secs))
  66.  
  67.     # block for timeout_secs while waiting for inotify events
  68.     notifier.process_events()
  69.     while notifier.check_events():  #loop in case more events appear while we are processing
  70.         notifier.read_events()
  71.         notifier.process_events()
  72.  
  73.     # check results
  74.     if not handler.results:
  75.         log.debug("timed out waiting for inotify event on {}".format(path))
  76.         ret['result'] = False
  77.         ret['comment'] = 'timed out waiting for file {}'.format(path)
  78.         return ret
  79.  
  80.     ret['changes'] = {'created': path}
  81.  
  82.     return ret
  83.  
  84. if __name__ == "__main__":
  85.     print exists("/tmp/foo")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement