Skip to content

Instantly share code, notes, and snippets.

@hashimotor
Created September 29, 2012 04:52
Show Gist options
  • Save hashimotor/3803213 to your computer and use it in GitHub Desktop.
Save hashimotor/3803213 to your computer and use it in GitHub Desktop.
File watchdog
"""
File watchdog.
REQUIREMENT
Watchdog Python package <http://packages.python.org/watchdog/>
USAGE
python watchdog-file.py echo something changed. # Ctrl-C to stop.
python watchdog-file.py "make test && make install"
"""
import sys
import time
import string
import subprocess
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
COMMAND_TO_RUN = ""
class FileEventHandler(FileSystemEventHandler):
def on_any_event(self, event):
if COMMAND_TO_RUN == "":
command = string.join(sys.argv[1:])
else:
command = COMMAND_TO_RUN
if event.__class__.__name__ == "DirModifiedEvent":
pass
else:
subprocess.call(command, shell=True)
if __name__ == "__main__":
observer = Observer()
observer.schedule(FileEventHandler(), path='.', recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment