Update module loading for the new python structure, rename snapshot_target to snapshot
authorBrian Dolbec <dolsen@gentoo.org>
Sun, 20 Jan 2013 21:50:23 +0000 (13:50 -0800)
committerBrian Dolbec <dolsen@gentoo.org>
Thu, 28 Feb 2013 01:38:13 +0000 (17:38 -0800)
14 files changed:
catalyst/defaults.py
catalyst/main.py
catalyst/targets/embedded_target.py
catalyst/targets/grp_target.py
catalyst/targets/livecd_stage1_target.py
catalyst/targets/livecd_stage2_target.py
catalyst/targets/netboot2_target.py
catalyst/targets/netboot_target.py
catalyst/targets/snapshot.py [moved from catalyst/targets/snapshot_target.py with 95% similarity]
catalyst/targets/stage1_target.py
catalyst/targets/stage2_target.py
catalyst/targets/stage3_target.py
catalyst/targets/stage4_target.py
catalyst/targets/tinderbox_target.py

index 2a0a3ffb04ebf07a774b62256534cfb9663e6ee3..3b30817ab353a8ce17bf271beb693a920f540700 100644 (file)
@@ -1,15 +1,5 @@
 
 
-# these should never be touched
-required_build_targets = ["generic_stage_target"]
-
-# new build types should be added here
-valid_build_targets = ["stage1_target", "stage2_target", "stage3_target",
-       "stage4_target", "grp_target", "livecd_stage1_target", "livecd_stage2_target",
-       "embedded_target", "tinderbox_target", "snapshot_target", "netboot_target",
-       "netboot2_target"
-       ]
-
 required_config_file_values = ["storedir", "sharedir", "distdir", "portdir"]
 
 valid_config_file_values = required_config_file_values[:]
index 936a9960589650de33ecbc8b0d587e324f988301..ff820c819597c94f932fdd497ca6067efb521786 100644 (file)
@@ -9,22 +9,17 @@
 
 import os
 import sys
-import imp
 import getopt
 import os.path
 
 __selfpath__ = os.path.abspath(os.path.dirname(__file__))
 
-sys.path.append(__selfpath__ + "/modules")
 
 import catalyst.config
 import catalyst.util
 from catalyst.lock import LockInUse
 from catalyst.support import CatalystError, find_binary
-from catalyst.defaults import (required_build_targets, valid_build_targets,
-       hash_definitions, confdefaults, option_messages
-       )
-
+from catalyst.defaults import hash_definitions, confdefaults, option_messages
 from hash_utils import HashMap
 from defaults import  contents_definitions
 from contents import ContentsMap
@@ -141,56 +136,35 @@ def parse_config(myconfig):
                print "Envscript support enabled."
 
 
-def import_modules():
-       # import catalyst's own modules
-       # (i.e. stage and the arch modules)
-       targetmap={}
-
+def import_module(target):
+       """
+       import catalyst's own modules
+       (i.e. targets and the arch modules)
+       """
        try:
-               module_dir = __selfpath__ + "/targets/"
-               for x in required_build_targets:
-                       try:
-                               fh=open(module_dir + x + ".py")
-                               module=imp.load_module(x, fh,"targets/" + x + ".py",
-                                       (".py", "r", imp.PY_SOURCE))
-                               fh.close()
-
-                       except IOError:
-                               raise CatalystError("Can't find " + x + ".py plugin in " +
-                                       module_dir, print_traceback=True)
-               for x in valid_build_targets:
-                       try:
-                               fh=open(module_dir + x + ".py")
-                               module=imp.load_module(x, fh, "targets/" + x + ".py",
-                                       (".py", "r", imp.PY_SOURCE))
-                               module.register(targetmap)
-                               fh.close()
-
-                       except IOError:
-                               raise CatalystError("Can't find " + x + ".py plugin in " +
-                                       module_dir, print_traceback=True)
-
+               mod_name = "catalyst.targets." + target
+               module = __import__(mod_name, [],[], ["not empty"])
        except ImportError as e:
-               print "!!! catalyst: Python modules not found in "+\
-                       module_dir + "; exiting."
-               print e
+               print "!!! catalyst: Python module import error: %s " % target + \
+                       "in catalyst/targets/ ... exiting."
+               print "ERROR was: ", e
                sys.exit(1)
+       return module
 
-       return targetmap
 
-def build_target(addlargs, targetmap):
+def build_target(addlargs):
        try:
-               if addlargs["target"] not in targetmap:
-                       raise CatalystError(
-                               "Target \"%s\" not available." % addlargs["target"],
-                               print_traceback=True)
-
-               mytarget=targetmap[addlargs["target"]](conf_values, addlargs)
-
-               mytarget.run()
+               module = import_module(addlargs["target"])
+               target = getattr(module, addlargs["target"])(conf_values, addlargs)
+       except AttributeError:
+               raise CatalystError(
+                       "Target \"%s\" not available." % addlargs["target"],
+                       print_traceback=True)
 
+       try:
+               target.run()
        except:
-               print "Python traceback output follows:"
+               print "Target run() exception:  Python traceback output follows:"
                catalyst.util.print_traceback()
                print
                print "!!! catalyst: Error encountered during run of target " + \
@@ -198,7 +172,6 @@ def build_target(addlargs, targetmap):
                sys.exit(1)
 
 def main():
-       targetmap={}
 
        version()
        if os.getuid() != 0:
@@ -348,9 +321,6 @@ def main():
                        print "Catalyst aborting...."
                        sys.exit(2)
 
-       # import the rest of the catalyst modules
-       targetmap=import_modules()
-
        addlargs={}
 
        if myspecfile:
@@ -371,7 +341,7 @@ def main():
 
        # everything is setup, so the build is a go
        try:
-               build_target(addlargs, targetmap)
+               build_target(addlargs)
 
        except CatalystError:
                print
index 528d5457e7931d81381ed84cb46cc6f7d7dbe46d..aee0f00feebdbf9b41ac345d55e0848d2b517bee 100644 (file)
@@ -45,7 +45,3 @@ class embedded_target(StageBase):
        def set_root_path(self):
                self.settings["root_path"]=normpath("/tmp/mergeroot")
                print "embedded root path is "+self.settings["root_path"]
-
-def register(foo):
-       foo.update({"embedded":embedded_target})
-       return foo
index 02279c3f9b1d7d3d63f01fa70abdf373a680c78d..b0eff7bf5254b5965c277aa986be38f94baef198 100644 (file)
@@ -119,7 +119,3 @@ class grp_target(StageBase):
                                        "config_profile_link","setup_confdir","portage_overlay","bind","chroot_setup",\
                                        "setup_environment","run_local","unbind",\
                                        "generate_digests","clear_autoresume"]
-
-def register(foo):
-       foo.update({"grp":grp_target})
-       return foo
index 30ba6054f1d74269d0238a0f6e20d81c6d39e8e7..19a03e9c878c77f21f4cdbcb07c452781508c408 100644 (file)
@@ -74,7 +74,3 @@ class livecd_stage1_target(StageBase):
                                self.settings["pkgcache_path"]=normpath(string.join(self.settings["pkgcache_path"]))
                else:
                        StageBase.set_pkgcache_path(self)
-
-def register(foo):
-       foo.update({"livecd-stage1":livecd_stage1_target})
-       return foo
index c63c94cebd5430d3e51894acc7c2f8501e4a0c15..106b43f22807725bd8c0949fbb027ec85db93606 100644 (file)
@@ -151,7 +151,3 @@ class livecd_stage2_target(StageBase):
                                "unbind","remove","empty","target_setup",\
                                "setup_overlay","create_iso"]
                self.settings["action_sequence"].append("clear_autoresume")
-
-def register(foo):
-       foo.update({"livecd-stage2":livecd_stage2_target})
-       return foo
index ff7c0a9779c46829206927eaf9b50fbd2fa5c3bc..aa35c8ceabf52d6307a9e98f79930d1d51d354e7 100644 (file)
@@ -170,7 +170,3 @@ class netboot2_target(StageBase):
                                        "setup_environment","build_packages","root_overlay",\
                                        "copy_files_to_image","setup_overlay","build_kernel","move_kernels",\
                                        "remove","empty","unbind","clean","clear_autoresume"]
-
-def register(foo):
-       foo.update({"netboot2":netboot2_target})
-       return foo
index eae35a9683d9ed61878bb3289f08f2533a475de7..923525754b8062082a315c22c97d2d67d124749e 100644 (file)
@@ -132,7 +132,3 @@ class netboot_target(StageBase):
                                                "setup_environment","build_packages","build_busybox",\
                                                "build_kernel","copy_files_to_image",\
                                                "clean","create_netboot_files","unbind","clear_autoresume"]
-
-def register(foo):
-       foo.update({"netboot":netboot_target})
-       return foo
similarity index 95%
rename from catalyst/targets/snapshot_target.py
rename to catalyst/targets/snapshot.py
index 49d8e6d68d35eff5cfc5738c58ea4e10b34b6e90..aa0a251b8d1151037ee883664b33c28d7f204723 100644 (file)
@@ -11,7 +11,7 @@ from catalyst.support import normpath, cmd
 from catalyst.base.targetbase import TargetBase
 from catalyst.base.genbase import GenBase
 
-class snapshot_target(TargetBase, GenBase):
+class snapshot(TargetBase, GenBase):
        """
        Builder class for snapshots.
        """
@@ -91,7 +91,3 @@ class snapshot_target(TargetBase, GenBase):
                        os.makedirs(myemp,0755)
                        os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
                        os.chmod(myemp,mystat[ST_MODE])
-
-def register(foo):
-       foo.update({"snapshot":snapshot_target})
-       return foo
index d70c5ec15986c00a5fd00828b546193a2635c9ad..6e5224705eb14f5a82b18ca0cea9e76bf4b1ee77 100644 (file)
@@ -95,7 +95,3 @@ class stage1_target(StageBase):
                # alter the mount mappings to bind mount proc onto it
                self.mounts.append("/tmp/stage1root/proc")
                self.mountmap["/tmp/stage1root/proc"]="/proc"
-
-def register(foo):
-       foo.update({"stage1":stage1_target})
-       return foo
index 490bca714549ba979d4445975bd7b1f029748fc3..4ecb46551d8724acc8633a583a198fbf344c430c 100644 (file)
@@ -63,7 +63,3 @@ class stage2_target(StageBase):
                                print "\tUsing an portage overlay for earlier stages could cause build issues."
                                print "\tIf you break it, you buy it. Don't complain to us about it."
                                print "\tDont say we did not warn you\n"
-
-def register(foo):
-       foo.update({"stage2":stage2_target})
-       return foo
index be73a6c30cd7adbc60fc1415f7821511dd421c6d..ae70df1be791f994037dff388a3b9396679bab18 100644 (file)
@@ -26,7 +26,3 @@ class stage3_target(StageBase):
 
        def set_cleanables(self):
                StageBase.set_cleanables(self)
-
-def register(foo):
-       foo.update({"stage3":stage3_target})
-       return foo
index bbdb048cbda67d1cefb72c2b1f9c6c339deb3333..d314801863636d4f759ad5981b1a138fdb7047cd 100644 (file)
@@ -37,8 +37,3 @@ class stage4_target(StageBase):
                if "fetch" not in self.settings['options']:
                        self.settings["action_sequence"].append("capture")
                self.settings["action_sequence"].append("clear_autoresume")
-
-def register(foo):
-       foo.update({"stage4":stage4_target})
-       return foo
-
index c415d74473e1ee34572c28450f037ea8a16f0d39..96d065292d76106998060f039c72d4cf1ee96e37 100644 (file)
@@ -44,7 +44,3 @@ class tinderbox_target(StageBase):
                              "config_profile_link","setup_confdir","bind","chroot_setup",\
                              "setup_environment","run_local","preclean","unbind","clean",\
                              "clear_autoresume"]
-
-def register(foo):
-       foo.update({"tinderbox":tinderbox_target})
-       return foo