Skip to content

Instantly share code, notes, and snippets.

@robgolding
Created July 18, 2012 15:57
Show Gist options
  • Save robgolding/3137116 to your computer and use it in GitHub Desktop.
Save robgolding/3137116 to your computer and use it in GitHub Desktop.
A simple watchdog script in Python
#!/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