summaryrefslogtreecommitdiff
path: root/cloudinit/importer.py
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2012-06-20 23:40:00 -0700
committerJoshua Harlow <harlowja@yahoo-inc.com>2012-06-20 23:40:00 -0700
commit95e0fa29af3656c1011c41ab0f35dc4e9317269c (patch)
treedd36e2a58478639733d114f452bf36f0af780803 /cloudinit/importer.py
parentd0ff82b71e315d49f49cbbd8e6a7740036973a4a (diff)
downloadvyos-cloud-init-95e0fa29af3656c1011c41ab0f35dc4e9317269c.tar.gz
vyos-cloud-init-95e0fa29af3656c1011c41ab0f35dc4e9317269c.zip
1. Add an importer function that will search for a given module in a set of search module 'prefixes'
that also has a potential set of required attributes. 2. Use this new importer to find the distro class, the userdata handler modules, the config modules and the datasource modules, if none can be found error out accordingly.
Diffstat (limited to 'cloudinit/importer.py')
-rw-r--r--cloudinit/importer.py44
1 files changed, 35 insertions, 9 deletions
diff --git a/cloudinit/importer.py b/cloudinit/importer.py
index a36b87bc..71cf2726 100644
--- a/cloudinit/importer.py
+++ b/cloudinit/importer.py
@@ -23,17 +23,43 @@
import sys
from cloudinit import log as logging
-from cloudinit import util
LOG = logging.getLogger(__name__)
-# Simple wrapper that allows us to add more logging in...
def import_module(module_name):
- try:
- LOG.debug("Attempting to import module %s", module_name)
- __import__(module_name)
- return sys.modules[module_name]
- except:
- util.logexc(LOG, 'Failed at importing %s', module_name)
- raise
+ __import__(module_name)
+ return sys.modules[module_name]
+
+
+def find_module(base_name, search_paths, required_attrs=None):
+ found_places = []
+ if not required_attrs:
+ required_attrs = []
+ real_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)
+ real_paths.append(full_path)
+ LOG.debug("Looking for modules %s that have attributes %s",
+ real_paths, required_attrs)
+ for full_path in real_paths:
+ mod = None
+ try:
+ mod = import_module(full_path)
+ except ImportError:
+ pass
+ if not mod:
+ continue
+ found_attrs = 0
+ for attr in required_attrs:
+ if hasattr(mod, attr):
+ found_attrs += 1
+ if found_attrs == len(required_attrs):
+ found_places.append(full_path)
+ LOG.debug("Found %s with attributes %s in %s", base_name,
+ required_attrs, found_places)
+ return found_places