def Index(self): """ Get the queries from the config file """ try: module = imp.load_source(safe_module_name('Ldap', self.name), self.name) except: # pylint: disable=W0702 err = sys.exc_info()[1] self.logger.error("Ldap: Failed to import %s: %s" % (self.name, err)) return if not hasattr(module, "__queries__"): self.logger.error("Ldap: %s has no __queries__ list" % self.name) return self.queries = list() for query in module.__queries__: try: self.queries.append(getattr(module, query)) except AttributeError: self.logger.warning( "Ldap: %s exports %s, but has no such attribute" % (self.name, query)) if self.core.metadata_cache_mode in ['cautious', 'aggressive']: self.core.metadata_cache.expire()
def HandleEvent(self, event=None): """ HandleEvent is called whenever the FAM registers an event. :param event: The event object :type event: Bcfg2.Server.FileMonitor.Event :returns: None """ if event and event.code2str() not in ["exists", "changed", "created"]: return try: module = imp.load_source(safe_module_name("TemplateHelper", self._module_name), self.name) except: # pylint: disable=W0702 # this needs to be a blanket except because the # imp.load_source() call can raise literally any error, # since it imports the module and just passes through any # exceptions raised. err = sys.exc_info()[1] self.logger.error("TemplateHelper: Failed to import %s: %s" % (self.name, err)) return if not hasattr(module, "__export__"): self.logger.error("TemplateHelper: %s has no __export__ list" % self.name) return newattrs = [] for sym in module.__export__ + getattr(module, "__default__", []): if sym in newattrs: # already added to attribute list continue if sym not in self._attrs and hasattr(self, sym): self.logger.warning( "TemplateHelper: %s: %s is a reserved keyword, " "skipping export" % (self.name, sym) ) continue try: setattr(self, sym, getattr(module, sym)) newattrs.append(sym) except AttributeError: self.logger.warning("TemplateHelper: %s exports %s, but has no such attribute" % (self.name, sym)) # remove old exports for sym in set(self._attrs) - set(newattrs): delattr(self, sym) self._attrs = newattrs self.defaults = [] for sym in getattr(module, "__default__", []): if sym in self.reserved_defaults: self.logger.warning( "TemplateHelper: %s: %s is a reserved keyword, not adding " "as default" % (self.name, sym) ) self.defaults.append(sym)
def check_helper(self, helper): """ Check a single helper module. :param helper: The filename of the helper module :type helper: string """ module_name = MODULE_RE.search(helper).group(1) try: module = imp.load_source( safe_module_name('TemplateHelper', module_name), helper) except: # pylint: disable=W0702 err = sys.exc_info()[1] self.LintError("templatehelper-import-error", "Failed to import %s: %s" % (helper, err)) return if not hasattr(module, "__export__"): self.LintError("templatehelper-no-export", "%s has no __export__ list" % helper) return elif not isinstance(module.__export__, list): self.LintError("templatehelper-nonlist-export", "__export__ is not a list in %s" % helper) return for sym in module.__export__: if not hasattr(module, sym): self.LintError( "templatehelper-nonexistent-export", "%s: exported symbol %s does not exist" % (helper, sym)) elif sym in self.reserved_keywords: self.LintError( "templatehelper-reserved-export", "%s: exported symbol %s is reserved" % (helper, sym)) elif sym.startswith("_"): self.LintError( "templatehelper-underscore-export", "%s: exported symbol %s starts with underscore" % (helper, sym)) if sym in getattr(module, "__default__", []): self.LintError( "templatehelper-export-and-default", "%s: %s is listed in both __default__ and " "__export__" % (helper, sym)) for sym in getattr(module, "__default__", []): if sym in self.reserved_defaults: self.LintError( "templatehelper-reserved-default", "%s: default symbol %s is reserved" % (helper, sym))
def check_helper(self, helper): """ Check a single helper module. :param helper: The filename of the helper module :type helper: string """ module_name = MODULE_RE.search(helper).group(1) try: module = imp.load_source( safe_module_name('TemplateHelper', module_name), helper) except: # pylint: disable=W0702 err = sys.exc_info()[1] self.LintError("templatehelper-import-error", "Failed to import %s: %s" % (helper, err)) return if not hasattr(module, "__export__"): self.LintError("templatehelper-no-export", "%s has no __export__ list" % helper) return elif not isinstance(module.__export__, list): self.LintError("templatehelper-nonlist-export", "__export__ is not a list in %s" % helper) return for sym in module.__export__: if not hasattr(module, sym): self.LintError("templatehelper-nonexistent-export", "%s: exported symbol %s does not exist" % (helper, sym)) elif sym in self.reserved_keywords: self.LintError("templatehelper-reserved-export", "%s: exported symbol %s is reserved" % (helper, sym)) elif sym.startswith("_"): self.LintError("templatehelper-underscore-export", "%s: exported symbol %s starts with underscore" % (helper, sym)) if sym in getattr(module, "__default__", []): self.LintError("templatehelper-export-and-default", "%s: %s is listed in both __default__ and " "__export__" % (helper, sym)) for sym in getattr(module, "__default__", []): if sym in self.reserved_defaults: self.LintError("templatehelper-reserved-default", "%s: default symbol %s is reserved" % (helper, sym))
def test_HandleEvent(self, mock_load_source): hm = self.get_obj() mock_load_source.side_effect = ImportError attrs = dir(hm) hm.HandleEvent() mock_load_source.assert_called_with( safe_module_name('TemplateHelper', hm._module_name), hm.name) self.assertEqual(attrs, dir(hm)) self.assertEqual(hm._attrs, []) mock_load_source.reset() mock_load_source.side_effect = None # a regular Mock (not a MagicMock) won't automatically create # __export__, so this triggers a failure condition in HandleEvent mock_load_source.return_value = Mock() attrs = dir(hm) hm.HandleEvent() mock_load_source.assert_called_with( safe_module_name('TemplateHelper', hm._module_name), hm.name) self.assertEqual(attrs, dir(hm)) self.assertEqual(hm._attrs, []) # test reserved attributes module = Mock() module.__export__ = ["_attrs", "HandleEvent", "__init__"] mock_load_source.reset() mock_load_source.return_value = module attrs = dir(hm) hm.HandleEvent() mock_load_source.assert_called_with( safe_module_name('TemplateHelper', hm._module_name), hm.name) self.assertEqual(attrs, dir(hm)) self.assertEqual(hm._attrs, []) # test adding attributes module = Mock() module.__export__ = ["foo", "bar", "baz", "HandleEvent"] mock_load_source.reset() mock_load_source.return_value = module hm.HandleEvent() mock_load_source.assert_called_with( safe_module_name('TemplateHelper', hm._module_name), hm.name) self.assertTrue(hasattr(hm, "foo")) self.assertTrue(hasattr(hm, "bar")) self.assertTrue(hasattr(hm, "baz")) self.assertEqual(hm._attrs, ["foo", "bar", "baz"]) # test removing attributes module = Mock() module.__export__ = ["foo", "bar", "quux", "HandleEvent"] mock_load_source.reset() mock_load_source.return_value = module hm.HandleEvent() mock_load_source.assert_called_with( safe_module_name('TemplateHelper', hm._module_name), hm.name) self.assertTrue(hasattr(hm, "foo")) self.assertTrue(hasattr(hm, "bar")) self.assertTrue(hasattr(hm, "quux")) self.assertFalse(hasattr(hm, "baz")) self.assertEqual(hm._attrs, ["foo", "bar", "quux"])
def HandleEvent(self, event=None): """ HandleEvent is called whenever the FAM registers an event. :param event: The event object :type event: Bcfg2.Server.FileMonitor.Event :returns: None """ if event and event.code2str() not in ['exists', 'changed', 'created']: return # expire the metadata cache, because the module might have changed if self.core.metadata_cache_mode in ['cautious', 'aggressive']: self.core.metadata_cache.expire() try: module = imp.load_source( safe_module_name('TemplateHelper', self._module_name), self.name) except: # pylint: disable=W0702 # this needs to be a blanket except because the # imp.load_source() call can raise literally any error, # since it imports the module and just passes through any # exceptions raised. err = sys.exc_info()[1] self.logger.error("TemplateHelper: Failed to import %s: %s" % (self.name, err)) return if not hasattr(module, "__export__"): self.logger.error("TemplateHelper: %s has no __export__ list" % self.name) return newattrs = [] for sym in module.__export__ + getattr(module, "__default__", []): if sym in newattrs: # already added to attribute list continue if sym not in self._attrs and hasattr(self, sym): self.logger.warning( "TemplateHelper: %s: %s is a reserved keyword, " "skipping export" % (self.name, sym)) continue try: setattr(self, sym, getattr(module, sym)) newattrs.append(sym) except AttributeError: self.logger.warning( "TemplateHelper: %s exports %s, but has no such attribute" % (self.name, sym)) # remove old exports for sym in set(self._attrs) - set(newattrs): delattr(self, sym) self._attrs = newattrs self.defaults = [] for sym in getattr(module, "__default__", []): if sym in self.reserved_defaults: self.logger.warning( "TemplateHelper: %s: %s is a reserved keyword, not adding " "as default" % (self.name, sym)) self.defaults.append(sym)