]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Fix importing of quantum resources subpackage
authorZane Bitter <zbitter@redhat.com>
Wed, 5 Dec 2012 10:15:42 +0000 (11:15 +0100)
committerZane Bitter <zbitter@redhat.com>
Wed, 5 Dec 2012 10:15:42 +0000 (11:15 +0100)
Apparently pkgutil.walk_packages(path) will return packages outside the
given path if they have the same names as the ones inside - so having a
quantum subpackage results in importing the global quantum package from
Quantum itself instead. Fix this by passing a package prefix to give a
fully qualified name.

Also make sure that submodules are added as an attribute to their immediate
parent package, not to some grandparent package with '.'s in the attribute
name.

bug 1085725

Change-Id: Ie571100898a33ed4fe15d1878c7b16db4caf4fa5
Signed-off-by: Zane Bitter <zbitter@redhat.com>
heat/common/plugin_loader.py
heat/tests/test_plugin_loader.py

index 04f8f24b7fc4b66f4977b41b5935679ca2497cbd..c68b310da517330727f698e5e8d55a13d159f2f8 100644 (file)
@@ -59,27 +59,34 @@ def _import_module(importer, module_name, package):
     Import a module dynamically into the specified package, given its name and
     PEP302 Importer object (which knows the path to look in).
     '''
-    fullname = _module_name(package.__name__, module_name)
 
     # Duplicate copies of modules are bad, so check if this has already been
     # imported statically
-    if fullname in sys.modules:
-        return sys.modules[fullname]
+    if module_name in sys.modules:
+        return sys.modules[module_name]
 
-    loader = importer.find_module(fullname)
+    loader = importer.find_module(module_name)
     if loader is None:
         return None
 
-    module = loader.load_module(fullname)
+    module = loader.load_module(module_name)
+
     # Make this accessible through the parent package for static imports
-    setattr(package, module_name, module)
+    local_name = module_name.partition(package.__name__ + '.')[2]
+    module_components = local_name.split('.')
+    parent = reduce(getattr, module_components[:-1], package)
+    setattr(parent, module_components[-1], module)
+
     return module
 
 
 def load_modules(package, ignore_error=False):
     '''Dynamically load all modules from a given package.'''
     path = package.__path__
-    for importer, module_name, is_package in pkgutil.walk_packages(path):
+    pkg_prefix = package.__name__ + '.'
+
+    for importer, module_name, is_package in pkgutil.walk_packages(path,
+                                                                   pkg_prefix):
         try:
             module = _import_module(importer, module_name, package)
         except ImportError as ex:
index d458d324935e0e68c2c20b2c68b18e8384d8b4d8..ca4feced1adff90c0ccf2508baad9b4e4eafc964 100644 (file)
@@ -56,11 +56,12 @@ class PluginLoaderTest(unittest.TestCase):
 
     def test_import_module_existing(self):
         import heat.engine.service
+        existing = heat.engine.service
         importer = pkgutil.ImpImporter(heat.engine.__path__[0])
         loaded = plugin_loader._import_module(importer,
-                                              'service',
+                                              'heat.engine.service',
                                               heat.engine)
-        self.assertTrue(loaded is heat.engine.service)
+        self.assertTrue(loaded is existing)
 
     def test_import_module_garbage(self):
         importer = pkgutil.ImpImporter(heat.engine.__path__[0])