0

I am using this command to watch a directory:

inotifywait -r -e modify,create,delete --exclude ".*(\.(git|idea)|(.*(___jb_tmp___|___jb_old___)))" my-dir

However, it does not work like I intend. What I want is that it does only NOT fire if every file that was changed is excluded. If there is at least one file that is NOT excluded and changes, then I want inotifywait to "fire".

My overall plan is to push code to a remove server whenever a python file or something else that is relevant is changed. I do not want to push code when only meta files from my IDE (PyCharm) or git files are changed.

Is there a way to do this?

Right now inotifywait does not fire when I change a python file in PyCharm since there are some files changed that end with _jb_tmp_. However, if I change a python file by hand, it works (e.g. in nano or vim).

8
  • Have you tried experimenting with simpler  --exclude values? Commented Nov 28, 2018 at 23:45
  • It is very hard to find simpler values that take care of all the files that PyCharm writes... Do you have a simpler idea? Commented Nov 29, 2018 at 0:15
  • It is really strange. When I use --exclude "(___jb_tmp___|___jb_old___)$" it fires when I save a file in PyCharm (but the event is called '.git/ CREATE index.lock'). When I use --exclude "(index\.lock|___jb_tmp___|___jb_old___)$" and save a file, nothing happens. It looks like PyCharm is not really writing python files? But this makes no sense Commented Nov 29, 2018 at 0:24
  • I tried the following command: inotifywait -r -m -e modify,create,delete my-dir (note the -m flag to monitor and see all events) and found that there is no event for the .py files when I save them in pycharm. The only events: my-dir/patch_experts/ CREATE densenet.py___jb_tmp___ my-dir/patch_experts/ MODIFY densenet.py___jb_tmp___ my-dir/patch_experts/ DELETE densenet.py___jb_old___ my-dir/.idea/ CREATE workspace.xml___jb_tmp___ my-dir/.idea/ MODIFY workspace.xml___jb_tmp___ my-dir/.idea/ DELETE workspace.xml___jb_old___ my-dir/.git/ CREATE index.lock my-dir/.git/ DELETE index.lock Commented Nov 29, 2018 at 0:50
  • When I use sublime instead of PyCharm it works (I see 'MODIFY main.py'). So the reason for the problem is PyCharm, but I have no idea why this happens Commented Nov 29, 2018 at 0:52

1 Answer 1

1

As stated in the comments to my question, I found that the problem was not caused by inotifywait but by PyCharm. It turned out that PyCharm has an option called "safe write" that tells the IDE to write changes to temporary files first and if this succeeds, it deletes the original file and renames the tempory file to the original file name.

This is why inotifywait only sees events like these:

$ inotifywait -r -m -e modify,create,delete my-dir

my-dir/ CREATE main.py___jb_tmp___ 
my-dir/ MODIFY main.py___jb_tmp___ 
my-dir/ DELETE main.py___jb_old___ 
my-dir/.idea/ CREATE workspace.xml___jb_tmp___ 
my-dir/.idea/ MODIFY workspace.xml___jb_tmp___ 
my-dir/.idea/ DELETE workspace.xml___jb_old___ 
my-dir/.git/ CREATE index.lock 
my-dir/.git/ DELETE index.lock

So I disabled safe write in PyCharm and now use this command:

inotifywait -r -e modify,create,delete --exclude ".*(\.git|\.idea)" my-dir

This is doing exactly what I want. The events I see from inotifywait are like this:

my-dir/ MODIFY main.py

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .