From 5bd81b5f6e1da5140288dde0c9a18feb83cd45a8 Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Thu, 21 Mar 2013 17:55:05 -0700 Subject: [PATCH] plugins: Ensure load_controllers is called in paster shell/run as well. load_controllers is the safe place to do imports of things that rely on `g` et al being in the correct state. It was not being called in paster shell or run which meant that any hooks defined in a module that relies on `g` at module scope (e.g. things importing model or controller code) would not be connected in those contexts. --- r2/r2/commands.py | 12 ++++++++++-- r2/r2/config/middleware.py | 28 ++++++++++++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/r2/r2/commands.py b/r2/r2/commands.py index 7b596ca58..6ba7e5aac 100644 --- a/r2/r2/commands.py +++ b/r2/r2/commands.py @@ -29,6 +29,8 @@ from r2.config.environment import load_environment from paste.script.pluginlib import find_egg_info_dir from pylons.wsgiapp import PylonsApp +from r2.config.middleware import RedditApp + #from pylons.commands import ShellCommand, ControllerCommand, \ # RestControllerCommand @@ -64,7 +66,8 @@ class RunCommand(command.Command): here_dir = os.getcwd() - if self.args[0].lower() == 'standalone': + is_standalone = self.args[0].lower() == 'standalone' + if is_standalone: load_environment(setup_globals=False) else: config_name = 'config:%s' % self.args[0] @@ -81,7 +84,12 @@ class RunCommand(command.Command): sys.path.insert(0, here_dir) # Load the wsgi app first so that everything is initialized right - wsgiapp = RegistryManager(PylonsApp()) + if not is_standalone: + wsgiapp = RegistryManager(RedditApp()) + else: + # in standalone mode we don't have an ini so we can't use + # RedditApp since it imports all the fancy controllers. + wsgiapp = RegistryManager(PylonsApp()) test_app = paste.fixture.TestApp(wsgiapp) # Query the test app to setup the environment diff --git a/r2/r2/config/middleware.py b/r2/r2/config/middleware.py index c5b7f8733..96f16338a 100644 --- a/r2/r2/config/middleware.py +++ b/r2/r2/config/middleware.py @@ -21,6 +21,7 @@ ############################################################################### """Pylons middleware initialization""" +import importlib import re import urllib import tempfile @@ -363,22 +364,29 @@ class RedditApp(PylonsApp): self._loading_lock = Lock() self._controllers = None - def load_controllers(self): - with self._loading_lock: - if not self._controllers: - controllers = __import__(self.package_name + '.controllers').controllers - controllers.load_controllers() - config['r2.plugins'].load_controllers() - self._controllers = controllers + def setup_app_env(self, environ, start_response): + PylonsApp.setup_app_env(self, environ, start_response) + self.load_controllers() - return self._controllers + def load_controllers(self): + if self._controllers: + return + + with self._loading_lock: + if self._controllers: + return + + controllers = importlib.import_module(self.package_name + + '.controllers') + controllers.load_controllers() + config['r2.plugins'].load_controllers() + self._controllers = controllers def find_controller(self, controller_name): if controller_name in self.controller_classes: return self.controller_classes[controller_name] - controllers = self.load_controllers() - controller_cls = controllers.get_controller(controller_name) + controller_cls = self._controllers.get_controller(controller_name) self.controller_classes[controller_name] = controller_cls return controller_cls