From 637651656894d946a1cb27606f20cb98fa64d332 Mon Sep 17 00:00:00 2001 From: Arjun Singh Date: Wed, 25 Jul 2012 13:07:24 -0400 Subject: [PATCH 1/4] Adding script that restarts django when content in the data directories changes. --- run_watch_data.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 run_watch_data.py diff --git a/run_watch_data.py b/run_watch_data.py new file mode 100755 index 0000000000..df7b3ccdec --- /dev/null +++ b/run_watch_data.py @@ -0,0 +1,51 @@ +#! ../python/bin/python + +# 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 +# watch fewer or more extensions, you can change EXTENSIONS. + +WATCH_DIRS = ["../data"] +EXTENSIONS = ["xml", "js", "css", "coffee", "scss", "html"] + +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: + if event.src_path.endswith(extension): + print "%s changed: restarting server." % event.src_path + self.process.terminate() + os.system("ps aux | grep 'django' | grep -v grep | awk '{print $2}' | xargs kill") + time.sleep(0.25) + self.process = Popen(['rake', 'lms']) + +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() From c9a36a7085645a5d018ac59ceaaafd0171bcb420 Mon Sep 17 00:00:00 2001 From: Arjun Singh Date: Wed, 25 Jul 2012 13:25:03 -0400 Subject: [PATCH 2/4] Adding an option to watch all extensions; useful for a VIM user where the filesystem events aren't triggered when saving a file without disabling backups. --- run_watch_data.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/run_watch_data.py b/run_watch_data.py index df7b3ccdec..eea721e4be 100755 --- a/run_watch_data.py +++ b/run_watch_data.py @@ -14,10 +14,11 @@ 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 -# watch fewer or more extensions, you can change EXTENSIONS. +# watch fewer or more extensions, you can change EXTENSIONS. To watch all +# extensions, add "*" to EXTENSIONS. WATCH_DIRS = ["../data"] -EXTENSIONS = ["xml", "js", "css", "coffee", "scss", "html"] +EXTENSIONS = ["*", "xml", "js", "css", "coffee", "scss", "html"] WATCH_DIRS = [os.path.abspath(os.path.normpath(dir)) for dir in WATCH_DIRS] @@ -30,12 +31,13 @@ class DjangoEventHandler(FileSystemEventHandler): def on_any_event(self, event): for extension in EXTENSIONS: - if event.src_path.endswith(extension): + if event.src_path.endswith(extension) or extension == "*": print "%s changed: restarting server." % event.src_path self.process.terminate() os.system("ps aux | grep 'django' | grep -v grep | awk '{print $2}' | xargs kill") time.sleep(0.25) self.process = Popen(['rake', 'lms']) + break if __name__ == "__main__": event_handler = DjangoEventHandler(Popen(['rake', 'lms'])) From e6ee3d50bab58dc13333180c4253ce8fbef83b13 Mon Sep 17 00:00:00 2001 From: Arjun Singh Date: Wed, 25 Jul 2012 13:39:30 -0400 Subject: [PATCH 3/4] Simplified server restart to just touching lms/__init__.py rather than killing and restarting the server. --- run_watch_data.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/run_watch_data.py b/run_watch_data.py index eea721e4be..895522a19c 100755 --- a/run_watch_data.py +++ b/run_watch_data.py @@ -33,10 +33,7 @@ class DjangoEventHandler(FileSystemEventHandler): for extension in EXTENSIONS: if event.src_path.endswith(extension) or extension == "*": print "%s changed: restarting server." % event.src_path - self.process.terminate() - os.system("ps aux | grep 'django' | grep -v grep | awk '{print $2}' | xargs kill") - time.sleep(0.25) - self.process = Popen(['rake', 'lms']) + os.system("touch lms/__init__.py") break if __name__ == "__main__": From 60365a6446f996c5e9d1aa4c50c5d4b57a6eab1f Mon Sep 17 00:00:00 2001 From: Arjun Singh Date: Wed, 25 Jul 2012 13:40:47 -0400 Subject: [PATCH 4/4] Using /usr/bin/env rather than defaulting to ../python/bin/python --- run_watch_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run_watch_data.py b/run_watch_data.py index 895522a19c..f5605a5c6a 100755 --- a/run_watch_data.py +++ b/run_watch_data.py @@ -1,4 +1,4 @@ -#! ../python/bin/python +#! /usr/bin/env python # This script requires that you have watchdog installed. You can install # watchdog via 'pip install watchdog'