If you want to execute a task every time you save a file with an editor, you can use Guard for Ruby, Grunt if you are writing JS, but that kind of tool when writing Python code I also want to use something made by Python. Here, watchdog is used.
Introduced
pip install watchdog
Then you can use the watchmedo command of the command line utility. If you want to monitor file system events and execute specific commands like this
watchmedo shell-command --patterns="*.py" --recursive --command='echo "${watch_src_path} is saved"' .
As you can see in the watchdog Readme, I can't get filesystem events when using vim on MacOSX + terminal. It's a good idea to change the following vim settings so that you don't create a swap file. My .vimrc is as follows. Or if you monitor the . *. Swp file, it will work.
.vimrc
let OSTYPE = system('uname')
if OSTYPE == "Darwin\n"
set noswapfile
set nowritebackup
endif
For example, if you have a test task in your Makefile and you want to add a watch task that runs the test task when you save the file
Makefile
watch:
#Under the app directory*.Monitor py file events and run make test
@echo Watch file changes and run test
watchmedo shell-command --interval=5 --patterns="*.py" -R -W -D --command='make test' app
test:
# (Example)Run Django tests
python app/manage.py test;
Needs a double boot prevention option to prevent the test from running multiple times when multiple filesystem events are skipped.
Recommended Posts