From 2e6e66e50abc615ea96edd7aa93ef6368a18e689 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Mon, 19 Mar 2012 12:53:10 -0400 Subject: Some more work on `pecan serve --reload`. Now the app and config directories are monitored (instead of cwd()). --- pecan/commands/serve.py | 52 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-) (limited to 'pecan/commands') diff --git a/pecan/commands/serve.py b/pecan/commands/serve.py index 3bd55f4..13b45a1 100644 --- a/pecan/commands/serve.py +++ b/pecan/commands/serve.py @@ -38,9 +38,12 @@ class ServeCommand(BaseCommand): stdout=sys.stdout, stderr=sys.stderr ) - def watch_and_spawn(self): + def watch_and_spawn(self, conf): from watchdog.observers import Observer - from watchdog.events import FileSystemEventHandler + from watchdog.events import ( + FileSystemEventHandler, FileSystemMovedEvent, FileModifiedEvent, + DirModifiedEvent + ) print 'Monitoring for changes...' self.create_subprocess() @@ -48,24 +51,31 @@ class ServeCommand(BaseCommand): parent = self class AggressiveEventHandler(FileSystemEventHandler): - def should_reload(self, path): - extension = os.path.splitext(path)[1] - if extension in ( - '.py', '.pyc', '.html', '.mak', - '.mako', '.xml' + def should_reload(self, event): + for t in ( + FileSystemMovedEvent, FileModifiedEvent, DirModifiedEvent ): - return True + if isinstance(event, t): + return True return False def on_modified(self, event): - if self.should_reload(getattr(event, 'src_path', '')): + if self.should_reload(event): parent.server_process.kill() parent.create_subprocess() + # Determine a list of file paths to monitor + paths = self.paths_to_monitor(conf) + event_handler = AggressiveEventHandler() - observer = Observer() - observer.schedule(event_handler, path=os.getcwd(), recursive=True) - observer.start() + for path, recurse in paths: + observer = Observer() + observer.schedule( + event_handler, + path=path, + recursive=recurse + ) + observer.start() try: while True: @@ -73,6 +83,22 @@ class ServeCommand(BaseCommand): except KeyboardInterrupt: pass + def paths_to_monitor(self, conf): + paths = [] + + for package_name in getattr(conf.app, 'modules', []): + module = __import__(package_name, fromlist=['app']) + if hasattr(module, 'app') and hasattr(module.app, 'setup_app'): + paths.append(( + os.path.dirname(module.__file__), + True + )) + break + + paths.append((os.path.dirname(conf.__file__), False)) + return paths + + def _serve(self, app, conf): from wsgiref.simple_server import make_server @@ -100,7 +126,7 @@ class ServeCommand(BaseCommand): if self.args.reload: try: - self.watch_and_spawn() + self.watch_and_spawn(conf) except ImportError: print('The `--reload` option requires `watchdog` to be ' 'installed.') -- cgit v1.2.1