exception handling integration
authorDaniel Robbins <drobbins@gentoo.org>
Tue, 28 Oct 2003 14:00:56 +0000 (14:00 +0000)
committerDaniel Robbins <drobbins@gentoo.org>
Tue, 28 Oct 2003 14:00:56 +0000 (14:00 +0000)
git-svn-id: svn+ssh://svn.gentoo.org/var/svnroot/catalyst/trunk@29 d1e1f19c-881f-0410-ab34-b69fee027534

ChangeLog
catalyst
modules/targets.py

index 8f9dbeb88859f3fdcbeac5bea1b65ab6c649f84d..eb9ba5836858d74d2e4eba1b0e78042cf6dd0640 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,7 +1,10 @@
 # ChangeLog for gentoo/src/catalyst 
 # Copyright 2002-2003 Gentoo Technologies, Inc.; Distributed under the GPL v2
-# $Header: /var/cvsroot/gentoo/src/catalyst/ChangeLog,v 1.6 2003/10/28 04:11:42 drobbins Exp $
+# $Header: /var/cvsroot/gentoo/src/catalyst/ChangeLog,v 1.7 2003/10/28 14:00:56 drobbins Exp $
 
+  28 Oct 2003; Daniel Robbins <drobbins@gentoo.org>: Exception handling fully-
+  integrated into current prototype code.
+  
   27 Oct 2003; Daniel Robbins <drobbins@gentoo.org>: beginning of exception
   handling integration, got some of the target code nicely fleshed out.
   
index 698d3cb06d604bc6a02884bb771923de4adcd99b..f313c22c736ec2bb85927a13045fa08dc93becaa 100755 (executable)
--- a/catalyst
+++ b/catalyst
@@ -95,8 +95,7 @@ targetmap={   "x86" : ["x86"],
                
 mymachine=os.uname()[4]
 if not machinemap.has_key(mymachine):
-       print "Unknown machine type:",mymachine
-       sys.exit(1)
+       die("Unknown machine type: "+mymachine)
 hostarch=machinemap[mymachine]
 print "Host architecture:",hostarch
 print "Supported architectures for targets:",string.join(targetmap[hostarch])
@@ -162,10 +161,7 @@ mytarget=targetmap[myspec["target"]](myspec)
 print
 spec_dump(myspec)
 
-#set up build directories, etc.
-retval=mytarget.run()
-if not retval:
-       die("Target failed.")
+mytarget.run()
 
 #to test this program, type:
 
index 300dce4318a38f8b992f6a1d3ec9cf9cfd87a0f1..97451d6e5a0d79986af2890ce1b5fd455c903656 100644 (file)
@@ -21,12 +21,13 @@ class generic_stage_target(generic_target):
                self.settings["chroot_path"]=st+"/tmp/"+self.settings["target_subpath"]
                self.settings["pkgcache_path"]=st+"/packages/"+self.settings["target_subpath"]
 
-       def mount_safety_check(self,mypath):
+       def mount_safety_check(self):
+               mypath=self.settings["chroot_path"]
                #check and verify that none of our paths in mypath are mounted. We don't want to clean up with things still
                #mounted, and this allows us to check. returns 1 on ok, 0 on "something is still mounted" case.
                paths=["/usr/portage/packages","/usr/portage/distfiles", "/var/tmp/distfiles", "/proc", "/root/.ccache", "/dev"]
                if not os.path.exists(mypath):
-                       return 
+                       return 
                mypstat=os.stat(mypath)[stat.ST_DEV]
                for x in paths:
                        if not os.path.exists(mypath+x):
@@ -34,42 +35,45 @@ class generic_stage_target(generic_target):
                        teststat=os.stat(mypath+x)[stat.ST_DEV]
                        if teststat!=mypstat:
                                #something is still mounted
-                               return 0
+                               raise CatalystError, x+" is still mounted; aborting."
                return 1
                
        def dir_setup(self):
-               if not self.mount_safety_check(self.settings["chroot_path"]):
-                       print "Error: bind mounts still mounted in "+self.settings["chroot_path"]+"."
-                       print "Will not clean up directory until this is fixed."
-                       return 0
+               self.mount_safety_check()
                retval=os.system("rm -rf "+self.settings["chroot_path"])
                if retval != 0:
-                       print "Could not remove existing directory: "+self.settings["chroot_path"]
-                       return 0
+                       raise CatalystError,"Could not remove existing directory: "+self.settings["chroot_path"]
                os.makedirs(self.settings["chroot_path"])
                if not os.path.exists(self.settings["pkgcache_path"]):
                        os.makedirs(self.settings["pkgcache_path"])
-               return 1
                
        def unpack_and_bind(self):
                retval=os.system("tar xjpf "+self.settings["source_path"]+" -C "+self.settings["chroot_path"])
                if retval!=0:
-                       die("Unpack error")
+                       raise CatalystError,"Error unpacking tarball"
                for x in [[self.settings["portdir"],"/usr/portage"],[self.settings["distdir"],"/usr/portage/distfiles"],
                                ["/proc","/proc"],["/dev","/dev"]]:
                        retval=os.system("mount --bind "+x[0]+" "+x[1])
                        if not retval:
-                               die("Bind mount error")
-       
-       def run(self):
-               print "I'm running!"
-               retval=self.dir_setup()
-               if not retval:
-                       return 0
-               retval=self.unpack_and_bind()
-               if not retval:
-                       return 0
+                               self.unbind()
+                               raise CatalystError,"Couldn't bind mount "+x[0]
+
+       def unbind(self):
+               pass    
 
+       def setup(self):
+               #setup will leave everything in unbound state if there is a failure
+               self.dir_setup()
+               self.unpack_and_bind()
+
+       def run(self):
+               self.setup()
+               try:
+                       pass
+               finally:
+                       #always unbind
+                       self.unbind()
+       
 class snapshot_target(generic_target):
        def __init__(self):
                self.settings["target_subpath"]="portage-"+self.settings["version_stamp"]
@@ -77,29 +81,35 @@ class snapshot_target(generic_target):
                self.settings["snapshot_path"]=st+"/snapshots/"+self.settings["target_subpath"]+".tar.bz2"
                self.settings["tmp_path"]=st+"/tmp/"+self.settings["target_subpath"]
 
-       def do_snapshot(portdir,snap_temp_dir,snapdir,snapversion):
+       def setup(self):
+               #nothing to do here
+               pass
+
+       def run(self):
                print "Creating Portage tree snapshot "+snapversion+" from "+portdir+"..."
-               mytmp=snap_temp_dir+"/snap-"+snapversion
+               mytmp=self.settings["tmp_path"]+"/"+self.settings["target_subpath"]
                if os.path.exists(mytmp):
                        retval=os.system("rm -rf "+mytmp)
                        if retval != 0:
-                               die("Could not remove existing directory: "+mytmp)
+                               raise CatalystError, "Could not remove existing directory: "+mytmp
                os.makedirs(mytmp)
-               retval=os.system("rsync -a --exclude /packages/ --exclude /distfiles/ --exclude CVS/ "+portdir+"/ "+mytmp+"/portage/")
+               retval=os.system("rsync -a --exclude /packages/ --exclude /distfiles/ --exclude CVS/ "+self.settings["portdir"]+"/ "+mytmp+"/portage/")
                if retval != 0:
-                       die("snapshot failure.")
+                       raise CatalystError,"Snapshot failure"
                print "Compressing Portage snapshot tarball..."
-               retval=os.system("( cd "+mytmp+"; tar cjf "+snapdir+"/portage-"+snapversion+".tar.bz2 portage )")
+               retval=os.system("( cd "+mytmp+"; tar cjf "+self.settings["snapshot_path"]+"/portage-"+self.settings["snapversion"]+".tar.bz2 portage )")
                if retval != 0:
-                       die("snapshot tarball creation failure.")
+                       raise CatalystError,"Snapshot creation failure"
+               self.cleanup()
+
+       def cleanup(self):
+               mytmp=self.settings["tmp_path"]+"/"+self.settings["target_subpath"]
                print "Cleaning up temporary snapshot directory..."
                #Be a good citizen and clean up after ourselves
                retval=os.system("rm -rf "+mytmp)
                if retval != 0:
-                       die("Unable to clean up directory: "+mytmp)
-       def run(self):
-               self.do_snapshot(self.settings["portdir"],self.settings["tmp_path"],self.settings["snapshot_path"],self.settings["snapversion"])
-
+                       raise CatalystError,"Snapshot cleanup failure"
+                       
 class stage1_target(generic_stage_target):
        def __init__(self,spec):
                generic_stage_target.__init__(self,spec)