Created
May 4, 2011 03:18
-
-
Save FlaviuSim/954700 to your computer and use it in GitHub Desktop.
Python watch for modified files and run tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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