joh/when-changed

-r argument unnecessary?

Closed this issue · 5 comments

While trying to figure out what -r does, I found that when-changed behaves recursively, even if you specify a directory without using -r.

As long as this is the case, can we get rid of -r and just make it the default?

WhenChanged.__init__ will automatically pass the recursive argument to watchdog.observers.Observer. However, -r will still matter in WhenChanged.is_interested, which is used by WhenChanged.on_change.

So either the setting is not exactly necessary, or I am misunderstanding its purpose.

joh commented

Yes, we watch recursively in watchdog.observers.Observer but ignore files in subdirectories in WhenChanged.is_interested() if recursive is False. See lines 90-98.

joh commented

It does exactly what it claims to do: with -r, the command will be executed also for changed files in subdirs (recursively).

Try it and see:

Without -r:

$ ./when-changed -v ./ echo %f changed &
When './' changes, run 'echo %f changed'
$ touch file
/home/joh/code/when-changed/file changed
$ touch dir/file
# nothing printed

With -r:

$ ./when-changed -v ./ echo %f changed &
When './' changes, run 'echo %f changed'
$ touch file
/home/joh/code/when-changed/file changed
$ touch dir/file
/home/joh/code/when-changed/dir/file changed

I see, so without -r there is only one level of recursion. I expected it to only watch the directory itself for changes if not using -r, but maybe it's more obvious to others...

I can't think of how to make it clearer; it seems obvious in retrospect... thanks for the answers, and for writing and maintaining this very handy tool.