diff options
author | Brett Holman <brett.holman@canonical.com> | 2023-03-31 15:24:09 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-31 15:24:09 -0600 |
commit | 612b4de892d19333c33276d541fed99fd16d3998 (patch) | |
tree | c90b72f14ea98f7a4d99563e3b3f9745a0bbe343 /cloudinit/importer.py | |
parent | 2a61a589fc0145314a61a31df4fd71c4639c0154 (diff) | |
download | cloud-init-git-612b4de892d19333c33276d541fed99fd16d3998.tar.gz |
Standardize kernel commandline user interface (#2093)
- deprecate ci.ds= and ci.datasource= in favor of ds=
- enable semi-colon-delimited datasource everywhere
- add support for case-insensitive datasource match
- add integration tests
Diffstat (limited to 'cloudinit/importer.py')
-rw-r--r-- | cloudinit/importer.py | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/cloudinit/importer.py b/cloudinit/importer.py index ce25fe9a..2ddd7a47 100644 --- a/cloudinit/importer.py +++ b/cloudinit/importer.py @@ -12,6 +12,8 @@ import importlib from types import ModuleType from typing import Optional, Sequence +from cloudinit import util + def import_module(module_name: str) -> ModuleType: return importlib.import_module(module_name) @@ -30,6 +32,26 @@ def _count_attrs( return found_attrs +def match_case_insensitive_module_name(mod_name: str) -> Optional[str]: + """Check the importable datasource modules for a case-insensitive match.""" + + # nocloud-net is the only datasource that requires matching on a name that + # does not match its python module - canonicalize it here + if "nocloud-net" == mod_name.lower(): + mod_name = mod_name[:-4] + if not mod_name.startswith("DataSource"): + ds_name = f"DataSource{mod_name}" + modules = {} + spec = importlib.util.find_spec("cloudinit.sources") + if spec and spec.submodule_search_locations: + for dir in spec.submodule_search_locations: + modules.update(util.get_modules_from_dir(dir)) + for module in modules.values(): + if module.lower() == ds_name.lower(): + return module + return ds_name + + def find_module( base_name: str, search_paths: Sequence[str], @@ -38,23 +60,16 @@ def find_module( """Finds specified modules""" if not required_attrs: required_attrs = [] - # NOTE(harlowja): translate the search paths to include the base name. lookup_paths = [] + found_paths = [] + for path in search_paths: - real_path = [] - if path: - real_path.extend(path.split(".")) - real_path.append(base_name) - full_path = ".".join(real_path) + # Add base name to search paths. Filter out empty paths. + full_path = ".".join(filter(None, [path, base_name])) lookup_paths.append(full_path) - found_paths = [] - for full_path in lookup_paths: if not importlib.util.find_spec(full_path): continue # Check that required_attrs are all present within the module. if _count_attrs(full_path, required_attrs) == len(required_attrs): found_paths.append(full_path) return (found_paths, lookup_paths) - - -# vi: ts=4 expandtab |