Skip to content

Instantly share code, notes, and snippets.

@FlaviuSim
Created May 4, 2011 03:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlaviuSim/954700 to your computer and use it in GitHub Desktop.
Save FlaviuSim/954700 to your computer and use it in GitHub Desktop.
Python watch for modified files and run tests
#!/usr/bin/env python
"""
-Thanks to Shawn Milochik for this - saved for future reference
Run unit test suite every time a Python file
is modified.
Requires pyinotify:
https://github.com/seb-m/pyinotify
"""
import os
from time import sleep
import pyinotify
from django.core.management import call_command
watch_manager = pyinotify.WatchManager()
class EventHandler(pyinotify.ProcessEvent):
#we use IN_CLOSE_WRITE instead of IN_MODIFY
#because it happens only once per file save
def process_IN_CLOSE_WRITE(self, event):
#ignore non-Python files
if event.pathname.split(os.extsep)[-1] != 'py':
return False
try:
call_command('test',
'myapp1', 'myapp2',
)
except SystemExit as ex:
"""
needed in case the the test runner
calls sys.exit(), which it does if
there are any test failures
"""
pass
#let's not run tests like crazy --
#I like to hit 'save' pretty frequently.
sleep(30)
handler = EventHandler()
notifier = pyinotify.Notifier(watch_manager, handler)
watch_dir = watch_manager.add_watch('.', pyinotify.IN_CLOSE_WRITE, rec = True)
notifier.loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment