From 36b2aa66627a4cc147f982d03688ae9df14bbe08 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 24 Jul 2015 00:57:55 +0200 Subject: TemplateHelper: Expire metadata cache on FileMonitor event --- src/lib/Bcfg2/Server/Plugins/TemplateHelper.py | 12 ++++++++++-- .../Testlib/TestServer/TestPlugins/TestTemplateHelper.py | 7 +++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/lib/Bcfg2/Server/Plugins/TemplateHelper.py b/src/lib/Bcfg2/Server/Plugins/TemplateHelper.py index cec2de297..09f3130ca 100644 --- a/src/lib/Bcfg2/Server/Plugins/TemplateHelper.py +++ b/src/lib/Bcfg2/Server/Plugins/TemplateHelper.py @@ -22,9 +22,10 @@ def safe_module_name(module): class HelperModule(Debuggable): """ Representation of a TemplateHelper module """ - def __init__(self, name): + def __init__(self, name, core): Debuggable.__init__(self) self.name = name + self.core = core #: The name of the module as used by get_additional_data(). #: the name of the file with .py stripped off. @@ -51,6 +52,10 @@ class HelperModule(Debuggable): 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(self._module_name), self.name) @@ -107,7 +112,6 @@ class TemplateHelper(Plugin, Connector, DirectoryBacked, TemplateDataProvider): __author__ = 'chris.a.st.pierre@gmail.com' ignore = re.compile(r'^(\.#.*|.*~|\..*\.(sw[px])|.*\.py[co])$') patterns = MODULE_RE - __child__ = HelperModule def __init__(self, core): Plugin.__init__(self, core) @@ -115,6 +119,10 @@ class TemplateHelper(Plugin, Connector, DirectoryBacked, TemplateDataProvider): DirectoryBacked.__init__(self, self.data) TemplateDataProvider.__init__(self) + # The HelperModule needs access to the core, so we have to construct + # it manually and add the custom argument. + self.__child__ = lambda fname: HelperModule(fname, core) + def get_additional_data(self, _): return dict([(h._module_name, h) # pylint: disable=W0212 for h in self.entries.values()]) diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestTemplateHelper.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestTemplateHelper.py index 128d6cae5..6f5ee18ba 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestTemplateHelper.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestTemplateHelper.py @@ -22,10 +22,13 @@ class TestHelperModule(Bcfg2TestCase): test_obj = HelperModule path = os.path.join(datastore, "test.py") - def get_obj(self, path=None): + def get_obj(self, path=None, core=None): if path is None: path = self.path - return self.test_obj(path) + if core is None: + core = Mock() + core.metadata_cache_mode = 'none' + return self.test_obj(path, core) def test__init(self): hm = self.get_obj() -- cgit v1.2.3-1-g7c22