def callObject(self, request, ob): """See `zope.publisher.interfaces.IPublication`. Our implementation make sure that no result is returned on redirect. It also sets the launchpad.userid and launchpad.pageid WSGI environment variables. """ request._publicationticks_start = tickcount.tickcount() if request.response.getStatus() in [301, 302, 303, 307]: return '' request.setInWSGIEnvironment( 'launchpad.userid', request.principal.id) # The view may be security proxied view = removeSecurityProxy(ob) # It's possible that the view is a bound method. view = getattr(view, 'im_self', view) context = removeSecurityProxy(getattr(view, 'context', None)) pageid = self.constructPageID(view, context) request.setInWSGIEnvironment('launchpad.pageid', pageid) # And spit the pageid out to our tracelog. tracelog(request, 'p', pageid) # For status URLs, where we really don't want to have any DB access # at all, ensure that all flag lookups will stop early. if pageid in ( 'RootObject:OpStats', 'RootObject:+opstats', 'RootObject:+haproxy'): request.features = NullFeatureController() features.install_feature_controller(request.features) # Calculate the hard timeout: needed because featureflags can be used # to control the hard timeout, and they trigger DB access, but our # DB tracers are not safe for reentrant use, so we must do this # outside of the SQL stack. We must also do it after traversal so that # the view is known and can be used in scope resolution. As we # actually stash the pageid after afterTraversal, we need to do this # even later. da.set_permit_timeout_from_features(True) da._get_request_timeout() if isinstance(removeSecurityProxy(ob), METHOD_WRAPPER_TYPE): # this is a direct call on a C-defined method such as __repr__ or # dict.__setitem__. Apparently publishing this is possible and # acceptable, at least in the case of # lp.services.webapp.servers.PrivateXMLRPCPublication. # mapply cannot handle these methods because it cannot introspect # them. We'll just call them directly. return ob(*request.getPositionalArguments()) return mapply(ob, request.getPositionalArguments(), request)
def test_threadGetFlag(self): self.populateStore() # the start-of-request handler will do something like this: controller, call_log = self.makeControllerInScopes(["default", "beta_user"]) install_feature_controller(controller) try: # then application code can simply ask without needing a context # object self.assertEqual(u"4.0", getFeatureFlag("ui.icing")) finally: install_feature_controller(None)
def task_init(dbuser): """Prepare to run a task. :param dbuser: The database user to use for running the task. """ ensure_zcml() transaction.abort() store = IStore(Job) getUtility(IZStorm).remove(store) store.close() dbconfig.override(dbuser=dbuser, isolation_level='read_committed') install_feature_controller(make_script_feature_controller('celery'))
def test_threadGetFlag(self): self.populateStore() # the start-of-request handler will do something like this: controller, call_log = self.makeControllerInScopes( ['default', 'beta_user']) install_feature_controller(controller) try: # then application code can simply ask without needing a context # object self.assertEqual(u'4.0', getFeatureFlag('ui.icing')) finally: install_feature_controller(None)
def callObject(self, request, ob): """See `zope.publisher.interfaces.IPublication`. Our implementation make sure that no result is returned on redirect. It also sets the launchpad.userid and launchpad.pageid WSGI environment variables. """ request._publicationticks_start = tickcount.tickcount() if request.response.getStatus() in [301, 302, 303, 307]: return '' request.setInWSGIEnvironment('launchpad.userid', request.principal.id) # The view may be security proxied view = removeSecurityProxy(ob) # It's possible that the view is a bound method. view = getattr(view, 'im_self', view) context = removeSecurityProxy(getattr(view, 'context', None)) pageid = self.constructPageID(view, context) request.setInWSGIEnvironment('launchpad.pageid', pageid) # And spit the pageid out to our tracelog. tracelog(request, 'p', pageid) # For status URLs, where we really don't want to have any DB access # at all, ensure that all flag lookups will stop early. if pageid in ('RootObject:OpStats', 'RootObject:+opstats', 'RootObject:+haproxy'): request.features = NullFeatureController() features.install_feature_controller(request.features) # Calculate the hard timeout: needed because featureflags can be used # to control the hard timeout, and they trigger DB access, but our # DB tracers are not safe for reentrant use, so we must do this # outside of the SQL stack. We must also do it after traversal so that # the view is known and can be used in scope resolution. As we # actually stash the pageid after afterTraversal, we need to do this # even later. da.set_permit_timeout_from_features(True) da._get_request_timeout() if isinstance(removeSecurityProxy(ob), METHOD_WRAPPER_TYPE): # this is a direct call on a C-defined method such as __repr__ or # dict.__setitem__. Apparently publishing this is possible and # acceptable, at least in the case of # lp.services.webapp.servers.PrivateXMLRPCPublication. # mapply cannot handle these methods because it cannot introspect # them. We'll just call them directly. return ob(*request.getPositionalArguments()) return mapply(ob, request.getPositionalArguments(), request)
def _setUp(self): """Set the feature flags that this fixture is responsible for.""" rule_source = self.makeRuleSource(self.makeNewRules()) original_controller = get_relevant_feature_controller() def scope_lookup(scope_name): request = get_current_browser_request() return ScopesFromRequest(request).lookup(scope_name) if self.override_scope_lookup: scope_lookup = self.override_scope_lookup install_feature_controller(FeatureController(scope_lookup, rule_source)) self.addCleanup(install_feature_controller, original_controller)
def _install_and_reschedule(controller, script_name): install_feature_controller(controller) refresh = getFeatureFlag('twisted.flags.refresh') or 60.0 try: refresh = float(refresh) except ValueError: log.msg("Invalid value {0!r} for twisted.flags.refresh".format( refresh)) refresh = 60.0 global _last_refresh if refresh != _last_refresh: if _last_refresh is not None: log.msg("twisted.flags.refresh changed to {0}".format(refresh)) _last_refresh = refresh reactor.callLater(refresh, update, script_name)
def setUp(self): """Set the feature flags that this fixture is responsible for.""" super(FeatureFixture, self).setUp() rule_source = StormFeatureRuleSource() self.addCleanup( rule_source.setAllRules, rule_source.getAllRulesAsTuples()) rule_source.setAllRules(self.makeNewRules()) original_controller = get_relevant_feature_controller() def scope_lookup(scope_name): request = get_current_browser_request() return ScopesFromRequest(request).lookup(scope_name) if self.override_scope_lookup: scope_lookup = self.override_scope_lookup install_feature_controller( FeatureController(scope_lookup, rule_source)) self.addCleanup(install_feature_controller, original_controller)
def __init__(self, context, request): super(SystemErrorView, self).__init__(context, request) self.request.response.removeAllNotifications() if self.response_code is not None: self.request.response.setStatus(self.response_code) if getattr(self.request, 'oopsid') is not None: self.request.response.addHeader('X-Lazr-OopsId', self.request.oopsid) # Need to neuter the feature flags on error output. The base template # checks for a feature flag, but they depend on db access which might # not have been setup yet. request.features = NullFeatureController() features.install_feature_controller(request.features) self.computeDebugOutput() if config.canonical.show_tracebacks: self.show_tracebacks = True if lp.layers.DebugLayer.providedBy(self.request): self.debugging = True self.specialuser = getUtility(ILaunchBag).developer
def __init__(self, context, request): super(SystemErrorView, self).__init__(context, request) self.request.response.removeAllNotifications() if self.response_code is not None: self.request.response.setStatus(self.response_code) if getattr(self.request, 'oopsid') is not None: self.request.response.addHeader( 'X-Lazr-OopsId', self.request.oopsid) # Need to neuter the feature flags on error output. The base template # checks for a feature flag, but they depend on db access which might # not have been setup yet. request.features = NullFeatureController() features.install_feature_controller(request.features) self.computeDebugOutput() if config.canonical.show_tracebacks: self.show_tracebacks = True if lp.layers.DebugLayer.providedBy(self.request): self.debugging = True self.specialuser = getUtility(ILaunchBag).developer
def run(self, use_web_security=False, isolation=None): """Actually run the script, executing zcml and initZopeless.""" if isolation is None: isolation = 'read_committed' self._init_zca(use_web_security=use_web_security) self._init_db(isolation=isolation) # XXX wgrant 2011-09-24 bug=29744: initZopeless used to do this. # Should be called directly by scripts that actually need it. set_immediate_mail_delivery(True) date_started = datetime.datetime.now(UTC) profiler = None if self.options.profile: profiler = Profile() original_feature_controller = get_relevant_feature_controller() install_feature_controller(make_script_feature_controller(self.name)) try: if profiler: profiler.runcall(self.main) else: self.main() except LaunchpadScriptFailure as e: self.logger.error(str(e)) sys.exit(e.exit_status) except SilentLaunchpadScriptFailure as e: sys.exit(e.exit_status) else: date_completed = datetime.datetime.now(UTC) self.record_activity(date_started, date_completed) finally: install_feature_controller(original_feature_controller) if profiler: profiler.dump_stats(self.options.profile)
def installFeatureController(self, feature_controller): old_features = features.get_relevant_feature_controller() features.install_feature_controller(feature_controller) self.addCleanup(features.install_feature_controller, old_features)
def installFeatureController(self, feature_controller): old_features = features.get_relevant_feature_controller() features.install_feature_controller(feature_controller) self.addCleanup( features.install_feature_controller, old_features)
def end_request(event): """Done with this FeatureController.""" install_feature_controller(None) event.request.features = None
def start_request(event): """Register FeatureController.""" event.request.features = FeatureController( ScopesFromRequest(event.request).lookup, StormFeatureRuleSource()) install_feature_controller(event.request.features)
def tearDown(self): install_feature_controller(self.original_controller) super(TestScriptFeatureController, self).tearDown()
def test_threadGetFlagNoContext(self): # If there is no context, please don't crash. workaround for the root # cause in bug 631884. install_feature_controller(None) self.assertEqual(None, getFeatureFlag('ui.icing'))
def test_script_restores_feature_controller(self): previous_controller = NullFeatureController() install_feature_controller(previous_controller) FakeScript(name="mongo").run() self.assertEqual( previous_controller, get_relevant_feature_controller())
def test_threadGetFlagNoContext(self): # If there is no context, please don't crash. workaround for the root # cause in bug 631884. install_feature_controller(None) self.assertEqual(None, getFeatureFlag("ui.icing"))