From 4bfdce39b80c8a7e51776f2783f4292cd70ceb0b Mon Sep 17 00:00:00 2001 From: Andrew Gaffney Date: Sun, 13 Sep 2009 13:44:43 -0500 Subject: [PATCH] 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 --- ChangeLog | 9 +++++ modules/catalyst/target/generic_stage.py | 33 +++++++----------- modules/catalyst/target/livecd_stage1.py | 5 ++- modules/catalyst/target/livecd_stage2.py | 6 ++-- modules/catalyst/target/netboot.py | 14 ++++---- modules/catalyst/util.py | 44 ++++++++++++++++++------ 6 files changed, 66 insertions(+), 45 deletions(-) diff --git a/ChangeLog b/ChangeLog index 14376f10..b9966fbe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,15 @@ # Distributed under the GPL v2 # $Id$ + 13 Sep 2009; Andrew Gaffney + 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 modules/catalyst/util.py: Add catalyst.util.remove_dir() helper function diff --git a/modules/catalyst/target/generic_stage.py b/modules/catalyst/target/generic_stage.py index e5be90ef..b9757e8c 100644 --- a/modules/catalyst/target/generic_stage.py +++ b/modules/catalyst/target/generic_stage.py @@ -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) diff --git a/modules/catalyst/target/livecd_stage1.py b/modules/catalyst/target/livecd_stage1.py index 3c9d8b17..eec477a9 100644 --- a/modules/catalyst/target/livecd_stage1.py +++ b/modules/catalyst/target/livecd_stage1.py @@ -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 diff --git a/modules/catalyst/target/livecd_stage2.py b/modules/catalyst/target/livecd_stage2.py index 7e0bbd82..2530d3a1 100644 --- a/modules/catalyst/target/livecd_stage2.py +++ b/modules/catalyst/target/livecd_stage2.py @@ -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: diff --git a/modules/catalyst/target/netboot.py b/modules/catalyst/target/netboot.py index 593e3e95..b604d704 100644 --- a/modules/catalyst/target/netboot.py +++ b/modules/catalyst/target/netboot.py @@ -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"): diff --git a/modules/catalyst/util.py b/modules/catalyst/util.py index 122212fd..0701dda5 100644 --- a/modules/catalyst/util.py +++ b/modules/catalyst/util.py @@ -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 -- 2.26.2