def test_legacy_plugin_dir_to_plugin_type(dirname, expected_result): if isinstance(expected_result, string_types): assert AnsibleCollectionRef.legacy_plugin_dir_to_plugin_type( dirname) == expected_result else: with pytest.raises(expected_result): AnsibleCollectionRef.legacy_plugin_dir_to_plugin_type(dirname)
def _find_fq_plugin(self, fq_name, extension): """Search builtin paths to find a plugin. No external paths are searched, meaning plugins inside roles inside collections will be ignored. """ plugin_type = AnsibleCollectionRef.legacy_plugin_dir_to_plugin_type( self.subdir) acr = AnsibleCollectionRef.from_fqcr(fq_name, plugin_type) n_resource = to_native(acr.resource, errors='strict') # we want this before the extension is added full_name = '{0}.{1}'.format(acr.n_python_package_name, n_resource) if extension: n_resource += extension pkg = sys.modules.get(acr.n_python_package_name) if not pkg: # FIXME: there must be cheaper/safer way to do this pkg = import_module(acr.n_python_package_name) # if the package is one of our flatmaps, we need to consult its loader to find the path, since the file could be # anywhere in the tree if hasattr(pkg, '__loader__') and isinstance(pkg.__loader__, AnsibleFlatMapLoader): try: file_path = pkg.__loader__.find_file(n_resource) return full_name, to_text(file_path) except IOError: # this loader already takes care of extensionless files, so if we didn't find it, just bail return None, None pkg_path = os.path.dirname(pkg.__file__) n_resource_path = os.path.join(pkg_path, n_resource) # FIXME: and is file or file link or ... if os.path.exists(n_resource_path): return full_name, to_text(n_resource_path) # look for any matching extension in the package location (sans filter) ext_blacklist = ['.pyc', '.pyo'] found_files = [ f for f in glob.iglob(os.path.join(pkg_path, n_resource) + '.*') if os.path.isfile(f) and os.path.splitext(f)[1] not in ext_blacklist ] if not found_files: return None, None if len(found_files) > 1: # TODO: warn? pass return full_name, to_text(found_files[0])