run_watch_data.py 1.63 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

20
WATCH_DIRS = ["../data", "common/lib/xmodule/xmodule/js", "common/lib/xmodule/xmodule/css"]
21
EXTENSIONS = ["*", "xml", "js", "css", "coffee", "scss", "html"]
22 23 24

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

25

26 27 28
class DjangoEventHandler(FileSystemEventHandler):

    def __init__(self, process):
29
        super(DjangoEventHandler, self).__init__()
30 31 32 33 34

        self.process = process

    def on_any_event(self, event):
        for extension in EXTENSIONS:
35
            if event.src_path.endswith(extension) or extension == "*":
36
                print "%s changed: restarting server." % event.src_path
37
                os.system("touch lms/__init__.py")
38
                break
39 40

if __name__ == "__main__":
41
    event_handler = DjangoEventHandler(Popen(['paver', 'lms']))
42 43 44 45 46 47 48 49 50 51
    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()