iHusainM
iHusainM

Reputation: 71

PyInotify: How to put nested watch or watch deeper directories using pyinotify

I am creating a watch on

/temp 

directory. I want to watch this directory for any new directories like

/temp/dir1, /temp/dir2, temp/dir3.

I want to watch the new directories created looking for a file like "submit" and perform necessary actions.

Right now I am creating a watch on /temp directory, on IN_CREATE event I check if it is a directory, if it is I put a new watch on the new directory found and call another event handler.

Code:

import asyncore
import pyinotify
wm = pyinotify.WatchManager()  # Watch Manager
mask = pyinotify.IN_CREATE  # watched events


class EventHandler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
    print "dir: ", event.dir
    print "Creating now: ", event.pathname
    print "Event Path: ", event.path
    print "Event name: ", event.name
    new_notifier = pyinotify.AsyncNotifier(wm, EventHandlerForNewDir())
    if event.dir:
        print "Setting up second watch"
        wdd2 = wm.add_watch(event.pathname, mask, rec=True)
        new_notifier.loop()

class EventHandlerForNewDir(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
    print "dir: ", event.dir
    print "Creating now: ", event.pathname
    print "Event Path: ", event.path
    print "Event name: ", event.name
    if (event.name == 'submit' and not (event.dir)):
        print "You are Awesome"

notifier = pyinotify.AsyncNotifier(wm, EventHandler())
wdd = wm.add_watch('/temp', mask, rec=True)
notifier.loop()

If the approach of creating watch inside a watch is correct, then how do I stop my internal watch once I find the file?

Upvotes: 5

Views: 719

Answers (0)

Related Questions