Modify code to use catalyst.util.remove_path() helper everywhere
authorAndrew Gaffney <agaffney@gentoo.org>
Sun, 13 Sep 2009 18:44:43 +0000 (13:44 -0500)
committerAndrew Gaffney <agaffney@gentoo.org>
Sun, 13 Sep 2009 18:44:43 +0000 (13:44 -0500)
Include globbing support in remove_path()
Create catalyst.util.create_symlink() helper and modify code to use it

ChangeLog
modules/catalyst/target/generic_stage.py
modules/catalyst/target/livecd_stage1.py
modules/catalyst/target/livecd_stage2.py
modules/catalyst/target/netboot.py
modules/catalyst/util.py

index 14376f10ac5773bd91b94e122f2c8bf58a2e52a4..b9966fbe16f1df17c6144a644f857fe672e34de0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,15 @@
 # Distributed under the GPL v2
 # $Id$
 
+  13 Sep 2009; Andrew Gaffney <agaffney@gentoo.org>
+  modules/catalyst/target/generic_stage.py,
+  modules/catalyst/target/livecd_stage1.py,
+  modules/catalyst/target/livecd_stage2.py,
+  modules/catalyst/target/netboot.py, modules/catalyst/util.py:
+  Modify code to use catalyst.util.remove_path() helper everywhere
+  Include globbing support in remove_path()
+  Create catalyst.util.create_symlink() helper and modify code to use it
+
   13 Sep 2009; Andrew Gaffney <agaffney@gentoo.org>
   modules/catalyst/util.py:
   Add catalyst.util.remove_dir() helper function
index e5be90efa902199f3f7275941c21e6895443515e..b9757e8c7335efcd244c9fa3fda91852af4497a0 100644 (file)
@@ -664,8 +664,7 @@ class generic_stage_target(generic_target):
                                self.snapshot_lock_object.write_lock()
                        if os.path.exists(destdir):
                                msg(cleanup_msg)
-                               cleanup_cmd="rm -rf "+destdir
-                               cmd(cleanup_cmd,cleanup_errmsg,env=self.env)
+                               catalyst.util.remove_path(destdir)
                        if not os.path.exists(destdir):
                                os.makedirs(destdir,0755)
 
@@ -692,12 +691,8 @@ class generic_stage_target(generic_target):
                        # TODO: zmedico and I discussed making this a directory and pushing
                        # in a parent file, as well as other user-specified configuration.
                        msg("Configuring profile link...")
-                       cmd("rm -f "+self.settings["chroot_path"]+"/etc/make.profile",\
-                                       "Error zapping profile link",env=self.env)
-                       cmd("ln -sf ../usr/portage/profiles/"+\
-                               self.settings["target_profile"]+" "+\
-                               self.settings["chroot_path"]+"/etc/make.profile",\
-                               "Error creating profile link",env=self.env)
+                       catalyst.util.create_symlink("../usr/portage/profiles/" + self.settings["target_profile"], \
+                               self.settings["chroot_path"] + "/etc/make.profile", True)
                        self.set_autoresume("config_profile_link")
 
        def setup_confdir(self):
@@ -706,8 +701,7 @@ class generic_stage_target(generic_target):
                else:
                        if "portage_confdir" in self.settings:
                                msg("Configuring /etc/portage...")
-                               cmd("rm -rf "+self.settings["chroot_path"]+"/etc/portage",\
-                                       "Error zapping /etc/portage",env=self.env)
+                               catalyst.util.remove_path(self.settings["chroot_path"] + "/etc/portage")
                                cmd("cp -R "+self.settings["portage_confdir"]+"/ "+\
                                        self.settings["chroot_path"]+"/etc/portage",\
                                        "Error copying /etc/portage",env=self.env)
@@ -871,9 +865,7 @@ class generic_stage_target(generic_target):
                                        "Could not copy /etc/hosts",env=self.env)
 
                        """ Modify and write out make.conf (for the chroot) """
-                       cmd("rm -f "+self.settings["chroot_path"]+"/etc/make.conf",\
-                               "Could not remove "+self.settings["chroot_path"]+\
-                               "/etc/make.conf",env=self.env)
+                       catalyst.util.remove_path(self.settings["chroot_path"] + "/etc/make.conf")
                        myf=open(self.settings["chroot_path"]+"/etc/make.conf","w")
                        myf.write("# These settings were set by the catalyst build script that automatically\n# built this stage.\n")
                        myf.write("# Please consult /usr/share/portage/config/make.conf.example for a more\n# detailed example.\n")
@@ -942,8 +934,7 @@ class generic_stage_target(generic_target):
                else:
                        for x in self.settings["cleanables"]:
                                msg("Cleaning chroot: " + x +"...")
-                               cmd("rm -rf "+self.settings["destpath"]+x,"Couldn't clean "+\
-                                       x,env=self.env)
+                               catalyst.util.remove_path(self.settings["destpath"] + x)
 
                """ Put /etc/hosts back into place """
                if os.path.exists(self.settings["chroot_path"]+"/etc/hosts.catalyst"):
@@ -953,8 +944,7 @@ class generic_stage_target(generic_target):
 
                """ Remove our overlay """
                if os.path.exists(self.settings["chroot_path"]+"/usr/local/portage"):
-                       cmd("rm -rf "+self.settings["chroot_path"]+"/usr/local/portage",\
-                               "Could not remove /usr/local/portage",env=self.env)
+                       catalyst.util.remove_path(self.settings["chroot_path"] + "/usr/local/portage")
                        cmd("sed -i '/^PORTDIR_OVERLAY/d' "+self.settings["chroot_path"]+\
                                "/etc/make.conf",\
                                "Could not remove PORTDIR_OVERLAY from make.conf",env=self.env)
@@ -996,7 +986,10 @@ class generic_stage_target(generic_target):
                                        operations, so we get easy glob handling.
                                        """
                                        msg("livecd: removing " + x)
-                                       os.system("rm -rf "+self.settings["chroot_path"]+x)
+                                       try:
+                                               catalyst.util.remove_path(self.settings["chroot_path"] + x)
+                                       except:
+                                               pass
                                try:
                                        self.run_controller_action("clean")
                                        self.set_autoresume("remove")
@@ -1282,8 +1275,8 @@ class generic_stage_target(generic_target):
                                                                if os.path.exists(self.settings["chroot_path"]+\
                                                                        "/tmp/initramfs_overlay/"):
                                                                        msg("Cleaning up temporary overlay dir")
-                                                                       cmd("rm -R "+self.settings["chroot_path"]+\
-                                                                               "/tmp/initramfs_overlay/",env=self.env)
+                                                                       catalyst.util.remove_path(self.settings["chroot_path"] + \
+                                                                               "/tmp/initramfs_overlay/")
 
                                                        self.set_autoresume("build_kernel_" + kname)
 
index 3c9d8b1719919d74ed56491986ae463370b366f8..eec477a9a46fd21072e91d4ab49fdd08c9075427 100644 (file)
@@ -29,13 +29,12 @@ class livecd_stage1_target(generic_stage_target):
                else:
                        # first clean up any existing target stuff
                        if os.path.exists(self.settings["target_path"]):
-                               cmd("rm -rf "+self.settings["target_path"],\
-                                       "Could not remove existing directory: "+self.settings["target_path"],env=self.env)
-                               self.set_autoresume("setup_target_path")
+                               catalyst.util.remove_path(self.settings["target_path"])
 
                        if not os.path.exists(self.settings["target_path"]):
                                os.makedirs(self.settings["target_path"])
 
+                       self.set_autoresume("setup_target_path")
 
        def set_target_path(self):
                pass
index 7e0bbd828b99b6adfc7f6acbbf9124f4b794cd39..2530d3a1aef08deddcd72386b942b56deaf09894 100644 (file)
@@ -48,12 +48,12 @@ class livecd_stage2_target(generic_stage_target):
                else:
                        # first clean up any existing target stuff
                        if os.path.isdir(self.settings["target_path"]):
-                               cmd("rm -rf "+self.settings["target_path"],
-                               "Could not remove existing directory: "+self.settings["target_path"],env=self.env)
-                               self.set_autoresume("setup_target_path")
+                               catalyst.util.remove_path(self.settings["target_path"])
                        if not os.path.exists(self.settings["target_path"]):
                                os.makedirs(self.settings["target_path"])
 
+                       self.set_autoresume("setup_target_path")
+
        def run_local(self):
                # what modules do we want to blacklist?
                if "livecd/modblacklist" in self.settings:
index 593e3e95e38a89eb7b34de0555ed65e313ae356b..b604d704e61c4b1d374c9ad2cf57fe7dbb1d7a91 100644 (file)
@@ -50,12 +50,12 @@ class netboot_target(generic_stage_target):
                else:
                        # first clean up any existing target stuff
                        if os.path.isfile(self.settings["target_path"]):
-                               cmd("rm -f "+self.settings["target_path"], \
-                                       "Could not remove existing file: "+self.settings["target_path"],env=self.env)
-                               self.set_autoresume("setup_target_path")
+                               catalyst.util.remove_path(self.settings["target_path"])
 
-               if not os.path.exists(self.settings["storedir"]+"/builds/"):
-                       os.makedirs(self.settings["storedir"]+"/builds/")
+                       if not os.path.exists(self.settings["storedir"]+"/builds/"):
+                               os.makedirs(self.settings["storedir"]+"/builds/")
+
+                       self.set_autoresume("setup_target_path")
 
        def copy_files_to_image(self):
                # copies specific files from the buildroot to merge_path
@@ -120,10 +120,8 @@ class netboot_target(generic_stage_target):
                else:
                        if "rm" in self.settings:
                                for x in self.settings["rm"]:
-                                       # we're going to shell out for all these cleaning operations,
-                                       # so we get easy glob handling
                                        msg("netboot: removing " + x)
-                                       os.system("rm -rf " + self.settings["chroot_path"] + self.settings["merge_path"] + x)
+                                       catalyst.util.remove_path(self.settings["chroot_path"] + self.settings["merge_path"] + x)
 
        def empty(self):
                if self.check_autoresume("empty"):
index 122212fd8008f7e413277c4df24a3271a5045a5a..0701dda57353475192c1bcec2697081e1f4e82e6 100644 (file)
@@ -2,7 +2,7 @@
 Collection of utility functions for catalyst
 """
 
-import sys, traceback, os, re, time, shutil
+import sys, traceback, os, re, time, shutil, glob
 import catalyst
 from catalyst.error import *
 from catalyst.output import *
@@ -195,25 +195,47 @@ def addl_arg_parse(myspec,addlargs,requiredspec,validspec):
                if not x in myspec:
                        raise CatalystError, "Required argument \""+x+"\" not specified."
 
-def remove_dir(path):
-       if os.uname()[0] == "FreeBSD":
-               cmd("chflags -R noschg " + path, \
-                       "Could not remove immutable flag for file " \
-                       + path)
-       try:
-               shutil.rmtree(path)
-       except:
-               raise CatalystError("Could not remove directory '%s'" % (path,))
+def remove_path(path, glob=True):
+       paths = None
+       if glob:
+               paths = glob.glob(path)
+       else:
+               paths = [path]
+       for x in paths:
+               if os.uname()[0] == "FreeBSD":
+                       cmd("chflags -R noschg " + x, \
+                               "Could not remove immutable flag for path " \
+                               + x)
+               if os.path.is_dir(x):
+                       try:
+                               shutil.rmtree(x)
+                       except:
+                               raise CatalystError("Could not remove directory '%s'" % (x,))
+               else:
+                       try:
+                               os.remove(x)
+                       except:
+                               raise CatalystError("Could not remove file '%s'" % (x,))
 
 def empty_dir(path):
        try:
                mystat = os.stat(path)
-               remove_dir(path)
+               remove_path(path, False)
                os.makedirs(path, 0755)
                os.chown(path, mystat[stat.ST_UID], mystat[stat.ST_GID])
                os.chmod(path, mystat[stat.ST_MODE])
        except:
                raise CatalystError("Could not empty directory '%s'" % (path,))
 
+def create_symlink(src, dest, remove_existing=False):
+       if os.path.exists(dest):
+               if remove_existing:
+                       remove_path(dest)
+               else:
+                       raise CatalystError("Could not create symlink at '%s' due to existing file" % (dest,))
+       try:
+               os.symlink(src, dest)
+       except:
+               raise CatalystError("Could not create symlink '%s' to '%s'" % (dest, src))
 
 # vim: ts=4 sw=4 sta noet sts=4 ai