[PATCH 1/2] Try to import modules the standard way first in collect

Vratislav Podzimek vpodzime at redhat.com
Wed Feb 20 15:41:04 UTC 2013


From: Martin Sivak <msivak at redhat.com>

This ensures that the correct package structure is present
in sys.path for modules and addons that make use of it.

The package-less addons will be imported file by file with
the package structure ensured by dummy empty modules.

This also changes module masks for addons to make the change
work properly.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 pyanaconda/addons.py    |  6 +++---
 pyanaconda/ui/common.py | 29 +++++++++++++++++++++++++++--
 2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/pyanaconda/addons.py b/pyanaconda/addons.py
index 77792be..7c517c6 100644
--- a/pyanaconda/addons.py
+++ b/pyanaconda/addons.py
@@ -50,15 +50,15 @@ def collect_addon_paths(toplevel_addon_paths, ui_subdir="gui"):
         for addon_id in directories:
             addon_ks_path = os.path.join(path, addon_id, "ks")
             if os.path.isdir(addon_ks_path):
-                module_paths["ks"].append(("pyanaconda.addon.%s.ks.%%s" % addon_id, addon_ks_path))
+                module_paths["ks"].append(("%s.ks.%%s" % addon_id, addon_ks_path))
 
             addon_spoke_path = os.path.join(path, addon_id, ui_subdir, "spokes")
             if os.path.isdir(addon_spoke_path):
-                module_paths["spokes"].append(("pyanaconda.addon.%s.spokes.%%s" % addon_id, addon_spoke_path))
+                module_paths["spokes"].append(("%s.%s.spokes.%%s" % (addon_id, ui_subdir), addon_spoke_path))
 
             addon_category_path = os.path.join(path, addon_id, ui_subdir, "categories")
             if os.path.isdir(addon_spoke_path):
-                module_paths["categories"].append(("pyanaconda.addon.%s.categories.%%s" % addon_id, addon_category_path))
+                module_paths["categories"].append(("%s.%s.categories.%%s" % (addon_id, ui_subdir), addon_category_path))
 
     return module_paths
 
diff --git a/pyanaconda/ui/common.py b/pyanaconda/ui/common.py
index c94861e..29c97fe 100644
--- a/pyanaconda/ui/common.py
+++ b/pyanaconda/ui/common.py
@@ -25,6 +25,7 @@ import imp
 import inspect
 import copy
 import sys
+import types
 
 class PathDict(dict):
     """Dictionary class supporting + operator"""
@@ -500,8 +501,32 @@ def collect(module_pattern, path, pred):
             # do not load module if any module with the same name
             # is already imported
             if not module:
-                module = imp.load_module(module_pattern % mod_name,
-                                         fo, module_path, module_flags)
+                # try importing the module the standard way first
+                # uses sys.path and the module's full name!
+                try:
+                    __import__(module_pattern % mod_name)
+                    module = sys.modules[module_pattern % mod_name]
+
+                # if it fails (package-less addon?) try importing single file
+                # and filling up the package structure voids
+                except ImportError:
+                    # prepare dummy modules to prevent RuntimeWarnings
+                    module_parts = (module_pattern % mod_name).split(".")
+
+                    # remove the last name as it will be inserted by the import
+                    module_parts.pop()
+
+                    # make sure all "parent" modules are in sys.modules
+                    for l in range(len(module_parts)):
+                        module_part_name = ".".join(module_parts[:l+1])
+                        if module_part_name not in sys.modules:
+                            module_part = types.ModuleType(module_part_name)
+                            module_part.__path__ = [path]
+                            sys.modules[module_part_name] = module_part
+
+                    # load the collected module
+                    module = imp.load_module(module_pattern % mod_name,
+                                             fo, module_path, module_flags)
 
 
             # get the filenames without the extensions so we can compare those
-- 
1.7.11.7



More information about the anaconda-patches mailing list