run_watch_data.py 1.59 KB
Newer Older
1
#! /usr/bin/env python
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

# This script requires that you have watchdog installed. You can install
# watchdog via 'pip install watchdog'

import sys
import time
import logging
import os
from subprocess import Popen
from signal import SIGTERM
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler, FileSystemEventHandler

# To watch more (or more specific) directories, change WATCH_DIRS to include the
# directories you want to watch. Note that this is recursive. If you want to
17 18
# watch fewer or more extensions, you can change EXTENSIONS. To watch all
# extensions, add "*" to EXTENSIONS.
19

Arjun Singh committed
20
WATCH_DIRS = ["../data", "common/lib/xmodule/xmodule/js"]
21
EXTENSIONS = ["*", "xml", "js", "css", "coffee", "scss", "html"]
22 23 24 25 26 27 28 29 30 31 32 33

WATCH_DIRS = [os.path.abspath(os.path.normpath(dir)) for dir in WATCH_DIRS]

class DjangoEventHandler(FileSystemEventHandler):

    def __init__(self, process):
        FileSystemEventHandler.__init__(self)

        self.process = process

    def on_any_event(self, event):
        for extension in EXTENSIONS:
34
            if event.src_path.endswith(extension) or extension == "*":
35
                print "%s changed: restarting server." % event.src_path
36
                os.system("touch lms/__init__.py")
37
                break
38 39 40 41 42 43 44 45 46 47 48 49 50

if __name__ == "__main__":
    event_handler = DjangoEventHandler(Popen(['rake', 'lms']))
    observer = Observer()
    for dir in WATCH_DIRS:
        observer.schedule(event_handler, dir, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()