From d923d05a33431e9c4c9d3d03bdb4bbdfd4b90447 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 14 Oct 2015 17:52:19 -0400 Subject: [PATCH] Reworking the way all() works in plugin_loader * Don't throw away the full path of the module code being loaded, as this can cause conflicts when files of the same name are being instantiated * Generalize the module loading code Fixes #12738 --- lib/ansible/plugins/__init__.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/ansible/plugins/__init__.py b/lib/ansible/plugins/__init__.py index 9b0471a216..fd1c110fea 100644 --- a/lib/ansible/plugins/__init__.py +++ b/lib/ansible/plugins/__init__.py @@ -302,6 +302,11 @@ class PluginLoader: __contains__ = has_plugin + def _load_module_source(self, name, path): + with open(path, 'r') as module_file: + module = imp.load_source(name, path, module_file) + return module + def get(self, name, *args, **kwargs): ''' instantiates a plugin of the given name using arguments ''' @@ -312,7 +317,7 @@ class PluginLoader: return None if path not in self._module_cache: - self._module_cache[path] = imp.load_source('.'.join([self.package, name]), path) + self._module_cache[path] = self._load_module_source('.'.join([self.package, name]), path) if kwargs.get('class_only', False): obj = getattr(self._module_cache[path], self.class_name) @@ -330,12 +335,12 @@ class PluginLoader: matches = glob.glob(os.path.join(i, "*.py")) matches.sort() for path in matches: - name, ext = os.path.splitext(os.path.basename(path)) - if name.startswith("_"): + name, _ = os.path.splitext(path) + if '__init__' in name: continue if path not in self._module_cache: - self._module_cache[path] = imp.load_source('.'.join([self.package, name]), path) + self._module_cache[path] = self._load_module_source(name, path) if kwargs.get('class_only', False): obj = getattr(self._module_cache[path], self.class_name)