Created
July 18, 2012 15:57
-
-
Save robgolding/3137116 to your computer and use it in GitHub Desktop.
A simple watchdog script in Python
This file contains 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 | |
import os | |
import sys | |
import re | |
import time | |
from watchdog.observers import Observer | |
from watchdog.events import PatternMatchingEventHandler | |
ignore = ( | |
re.compile('.*\.css'), | |
) | |
class DogEventHandler(PatternMatchingEventHandler): | |
def __init__(self, command, *args, **kwargs): | |
super(DogEventHandler, self).__init__(*args, **kwargs) | |
self.command = command | |
def on_modified(self, event): | |
os.system(self.command) | |
def main(args): | |
command = ' '.join(args[1:]) | |
event_handler = DogEventHandler(command, ignore_patterns=['*.css']) | |
observer = Observer() | |
observer.schedule(event_handler, path=args[0], recursive=True) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() | |
if __name__ == "__main__": | |
if len(sys.argv) < 3: | |
print 'Usage: %s <file> <command>' % sys.argv[0] | |
else: | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment