Create catalyst.util.empty_tree() helper function and use it all over
authorAndrew Gaffney <agaffney@gentoo.org>
Sat, 12 Sep 2009 16:08:52 +0000 (11:08 -0500)
committerAndrew Gaffney <agaffney@gentoo.org>
Sat, 12 Sep 2009 16:08:52 +0000 (11:08 -0500)
ChangeLog
modules/catalyst/target/generic_stage.py
modules/catalyst/target/livecd_stage1.py
modules/catalyst/target/netboot.py
modules/catalyst/target/snapshot.py
modules/catalyst/util.py

index b9e2b60d7a3a94bcd9dfd442d83be13d84c632ce..9bf50227ea9b287c31ef6ed53cdc6ea7ff867e74 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,13 @@
 # Distributed under the GPL v2
 # $Id$
 
+  12 Sep 2009; Andrew Gaffney <agaffney@gentoo.org>
+  modules/catalyst/target/generic_stage.py,
+  modules/catalyst/target/livecd_stage1.py,
+  modules/catalyst/target/netboot.py, modules/catalyst/target/snapshot.py,
+  modules/catalyst/util.py:
+  Create catalyst.util.empty_tree() helper function and use it all over
+
   12 Sep 2009; Andrew Gaffney <agaffney@gentoo.org> TODO,
   modules/catalyst/target/generic.py:
   Add ability to invalidate all further autoresume points
index 1ce41a2c02305ee8e6f6b1f0d53b25cffc4a7ba1..0f4a9fe8bb24173e22cbbc493216549835aecdf8 100644 (file)
@@ -4,8 +4,7 @@ This class does all of the chroot setup, copying of files, etc. It is
 the driver class for pretty much everything that Catalyst does.
 """
 
-import os, string, types, shutil
-from stat import *
+import os, types
 import catalyst
 from catalyst.output import *
 from catalyst.spawn import cmd
@@ -224,7 +223,7 @@ class generic_stage_target(generic_target):
                if "install_mask" in self.settings:
                        if type(self.settings["install_mask"])!=types.StringType:
                                self.settings["install_mask"]=\
-                                       string.join(self.settings["install_mask"])
+                                       " ".join(self.settings["install_mask"])
 
        def set_target_profile(self):
                self.settings["target_profile"]=self.settings["profile"]
@@ -243,7 +242,7 @@ class generic_stage_target(generic_target):
                if "pkgcache_path" in self.settings:
                        if type(self.settings["pkgcache_path"])!=types.StringType:
                                self.settings["pkgcache_path"]=\
-                                       catalyst.util.normpath(string.join(self.settings["pkgcache_path"]))
+                                       catalyst.util.normpath(" ".join(self.settings["pkgcache_path"]))
                else:
                        self.settings["pkgcache_path"]=\
                                catalyst.util.normpath(self.settings["storedir"]+"/packages/"+\
@@ -253,7 +252,7 @@ class generic_stage_target(generic_target):
                if "kerncache_path" in self.settings:
                        if type(self.settings["kerncache_path"])!=types.StringType:
                                self.settings["kerncache_path"]=\
-                                       catalyst.util.normpath(string.join(self.settings["kerncache_path"]))
+                                       catalyst.util.normpath(" ".join(self.settings["kerncache_path"]))
                else:
                        self.settings["kerncache_path"]=catalyst.util.normpath(self.settings["storedir"]+\
                                "/kerncache/"+self.settings["target_subpath"]+"/")
@@ -907,7 +906,7 @@ class generic_stage_target(generic_target):
                        if myusevars:
                                myf.write("# These are the USE flags that were used in addition to what is provided by the\n# profile used for building.\n")
                                myusevars = sorted(set(myusevars))
-                               myf.write('USE="'+string.join(myusevars)+'"\n')
+                               myf.write('USE="' + " ".join(myusevars)+'"\n')
                                if '-*' in myusevars:
                                        msg("\nWarning!!!  ")
                                        msg("\tThe use of -* in " + "use will cause portage to ignore")
@@ -984,15 +983,7 @@ class generic_stage_target(generic_target):
                                                msg(x + " not a directory or does not exist, skipping 'empty' operation.")
                                                continue
                                        msg("Emptying directory" + x)
-                                       """
-                                       stat the dir, delete the dir, recreate the dir and set
-                                       the proper perms and ownership
-                                       """
-                                       mystat=os.stat(myemp)
-                                       shutil.rmtree(myemp)
-                                       os.makedirs(myemp,0755)
-                                       os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
-                                       os.chmod(myemp,mystat[ST_MODE])
+                                       catalyst.util.empty_dir(myemp)
                        self.set_autoresume("empty")
 
        def remove(self):
@@ -1033,7 +1024,7 @@ class generic_stage_target(generic_target):
                        """ Capture target in a tarball """
                        mypath=self.settings["target_path"].split("/")
                        """ Remove filename from path """
-                       mypath=string.join(mypath[:-1],"/")
+                       mypath = " ".join(mypath[:-1],"/")
 
                        """ Now make sure path exists """
                        if not os.path.exists(mypath):
@@ -1078,8 +1069,8 @@ class generic_stage_target(generic_target):
                                #os.environ[varname]=self.settings[x]
                                self.env[varname]=self.settings[x]
                        elif type(self.settings[x])==types.ListType:
-                               #os.environ[varname]=string.join(self.settings[x])
-                               self.env[varname]=string.join(self.settings[x])
+                               #os.environ[varname] = " ".join(self.settings[x])
+                               self.env[varname] = " ".join(self.settings[x])
                        elif type(self.settings[x])==types.BooleanType:
                                if self.settings[x]:
                                        self.env[varname]="true"
@@ -1136,7 +1127,7 @@ class generic_stage_target(generic_target):
                                        things like "<" to remain intact
                                        """
                                        myunmerge[x]="'"+myunmerge[x]+"'"
-                               myunmerge=string.join(myunmerge)
+                               myunmerge = " ".join(myunmerge)
 
                                """ Before cleaning, unmerge stuff """
                                try:
@@ -1248,7 +1239,7 @@ class generic_stage_target(generic_target):
                                                                        "/kernelopts"]
 
                                                                if type(myopts) != types.StringType:
-                                                                       myopts = string.join(myopts)
+                                                                       myopts = " ".join(myopts)
                                                                        self.env[kname+"_kernelopts"]=myopts
 
                                                                else:
@@ -1336,19 +1327,7 @@ class generic_stage_target(generic_target):
                myemp=self.settings["chroot_path"]
                if os.path.isdir(myemp):
                        msg("Emptying directory " + myemp)
-                       """
-                       stat the dir, delete the dir, recreate the dir and set
-                       the proper perms and ownership
-                       """
-                       mystat=os.stat(myemp)
-                       #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env)
-                       """ There's no easy way to change flags recursively in python """
-                       if os.uname()[0] == "FreeBSD":
-                               os.system("chflags -R noschg "+myemp)
-                       shutil.rmtree(myemp)
-                       os.makedirs(myemp,0755)
-                       os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
-                       os.chmod(myemp,mystat[ST_MODE])
+                       catalyst.util.empty_dir(myemp)
 
        def clear_packages(self):
                if "PKGCACHE" in self.settings:
@@ -1357,16 +1336,7 @@ class generic_stage_target(generic_target):
                        myemp=self.settings["pkgcache_path"]
                        if os.path.isdir(myemp):
                                msg("Emptying directory " + myemp)
-                               """
-                               stat the dir, delete the dir, recreate the dir and set
-                               the proper perms and ownership
-                               """
-                               mystat=os.stat(myemp)
-                               #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env)
-                               shutil.rmtree(myemp)
-                               os.makedirs(myemp,0755)
-                               os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
-                               os.chmod(myemp,mystat[ST_MODE])
+                               catalyst.util.empty_dir(myemp)
 
        def clear_kerncache(self):
                if "KERNCACHE" in self.settings:
@@ -1375,16 +1345,7 @@ class generic_stage_target(generic_target):
                        myemp=self.settings["kerncache_path"]
                        if os.path.isdir(myemp):
                                msg("Emptying directory " + myemp)
-                               """
-                               stat the dir, delete the dir, recreate the dir and set
-                               the proper perms and ownership
-                               """
-                               mystat=os.stat(myemp)
-                               #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env)
-                               shutil.rmtree(myemp)
-                               os.makedirs(myemp,0755)
-                               os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
-                               os.chmod(myemp,mystat[ST_MODE])
+                               catalyst.util.empty_dir(myemp)
 
        def clear_autoresume(self):
                """ Clean resume points since they are no longer needed """
@@ -1393,20 +1354,7 @@ class generic_stage_target(generic_target):
                        myemp=self.settings["autoresume_path"]
                        if os.path.isdir(myemp):
                                msg("Emptying directory " + myemp)
-                               """
-                               stat the dir, delete the dir, recreate the dir and set
-                               the proper perms and ownership
-                               """
-                               mystat=os.stat(myemp)
-                               if os.uname()[0] == "FreeBSD":
-                                       cmd("chflags -R noschg "+myemp,\
-                                               "Could not remove immutable flag for file "\
-                                               +myemp)
-                               #cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env-self.env)
-                               shutil.rmtree(myemp)
-                               os.makedirs(myemp,0755)
-                               os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
-                               os.chmod(myemp,mystat[ST_MODE])
+                               catalyst.util.empty_dir(myemp)
 
        def purge(self):
                catalyst.util.countdown(10, "Purging Caches ...")
index bb6404082392421501fadd229b2c2b882db2b414..7c066d9228eeeb5532a3260640b539010500f3be 100644 (file)
@@ -60,7 +60,7 @@ class livecd_stage1_target(generic_stage_target):
        def set_pkgcache_path(self):
                if "pkgcache_path" in self.settings:
                        if type(self.settings["pkgcache_path"]) != types.StringType:
-                               self.settings["pkgcache_path"]=catalyst.util.normpath(string.join(self.settings["pkgcache_path"]))
+                               self.settings["pkgcache_path"] = catalyst.util.normpath(" ".join(self.settings["pkgcache_path"]))
                else:
                        generic_stage_target.set_pkgcache_path(self)
 
index 5838a620056fa5cba816da676f92a5b0d42b1f35..81af14f68f46771f250ff82e0ab41e5ef907c2e4 100644 (file)
@@ -138,13 +138,7 @@ class netboot_target(generic_stage_target):
                                                msg(x + " not a directory or does not exist, skipping 'empty' operation.")
                                                continue
                                        msg("Emptying directory " + x)
-                                       # stat the dir, delete the dir, recreate the dir and set
-                                       # the proper perms and ownership
-                                       mystat=os.stat(myemp)
-                                       shutil.rmtree(myemp)
-                                       os.makedirs(myemp,0755)
-                                       os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
-                                       os.chmod(myemp,mystat[ST_MODE])
+                                       catalyst.util.empty_dir(x)
                self.set_autoresume("empty")
 
        def set_action_sequence(self):
index d10a5a3d4c8b89546b502e57a58ce5f2d86c0d13..9f3de982a071f2a9b64221faf8bce641aaa40b45 100644 (file)
@@ -3,7 +3,7 @@
 Builder class for snapshots.
 """
 
-import os, shutil, stat
+import os
 import catalyst
 from catalyst.target.generic import generic_target
 from catalyst.spawn import cmd
@@ -28,9 +28,6 @@ class snapshot_target(catalyst.target.generic.generic_target):
                if not os.path.exists(x):
                        os.makedirs(x)
 
-       def mount_safety_check(self):
-               pass
-
        def run(self):
                if "PURGEONLY" in self.settings:
                        self.purge()
@@ -60,28 +57,15 @@ class snapshot_target(catalyst.target.generic.generic_target):
                self.cleanup()
                msg("snapshot: complete!")
 
-       def kill_chroot_pids(self):
-               pass
-
        def cleanup(self):
+               # What is the point of this?
                msg("Cleaning up...")
 
        def purge(self):
                myemp=self.settings["tmp_path"]
                if os.path.isdir(myemp):
                        msg("Emptying directory " + myemp)
-                       """
-                       stat the dir, delete the dir, recreate the dir and set
-                       the proper perms and ownership
-                       """
-                       mystat=os.stat(myemp)
-                       """ There's no easy way to change flags recursively in python """
-                       if os.uname()[0] == "FreeBSD":
-                               os.system("chflags -R noschg "+myemp)
-                       shutil.rmtree(myemp)
-                       os.makedirs(myemp,0755)
-                       os.chown(myemp,mystat[stat.ST_UID],mystat[stat.ST_GID])
-                       os.chmod(myemp,mystat[stat.ST_MODE])
+                       catalyst.util.empty_dir(myemp)
 
 __target_map = {"snapshot":snapshot_target}
 
index 078cbfb72783d116b3651031c4ba1ee34592089d..1d3e38e3ea624a0f49379ab9046421872bb5ed3c 100644 (file)
@@ -2,7 +2,7 @@
 Collection of utility functions for catalyst
 """
 
-import sys, traceback, os, os.path, re, time
+import sys, traceback, os, os.path, re, time, shutil
 import catalyst
 from catalyst.error import *
 from catalyst.output import *
@@ -195,4 +195,16 @@ def addl_arg_parse(myspec,addlargs,requiredspec,validspec):
                if not x in myspec:
                        raise CatalystError, "Required argument \""+x+"\" not specified."
 
+def empty_dir(path):
+       mystat = os.stat(path)
+       if os.uname()[0] == "FreeBSD":
+               cmd("chflags -R noschg " + path, \
+                       "Could not remove immutable flag for file " \
+                       + path)
+       shutil.rmtree(path)
+       os.makedirs(path, 0755)
+       os.chown(path, mystat[stat.ST_UID], mystat[stat.ST_GID])
+       os.chmod(path, mystat[stat.ST_MODE])
+
+
 # vim: ts=4 sw=4 sta noet sts=4 ai