commit:     7f4fd57ff9b591acee49c675dcdc77973896cbce
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Thu May 19 19:23:40 2016 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Fri May 20 03:36:16 2016 +0000
URL:        https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=7f4fd57f

clear_path: new helper for nuking dirs

Add a simple helper to replace all calls to `rm -rf`.

Fix a thinko in the previous commit where the os.unlink call was
given the wrong variable.

 catalyst/base/stagebase.py   | 18 +++++++-----------
 catalyst/fileops.py          |  7 ++++++-
 catalyst/targets/netboot2.py | 12 +++++-------
 3 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py
index 6aa854b..9b74685 100644
--- a/catalyst/base/stagebase.py
+++ b/catalyst/base/stagebase.py
@@ -18,7 +18,7 @@ from catalyst.base.targetbase import TargetBase
 from catalyst.base.clearbase import ClearBase
 from catalyst.base.genbase import GenBase
 from catalyst.lock import LockDir, LockInUse
-from catalyst.fileops import ensure_dirs, pjoin, clear_dir
+from catalyst.fileops import ensure_dirs, pjoin, clear_dir, clear_path
 from catalyst.base.resume import AutoResume
 
 if sys.version_info[0] >= 3:
@@ -870,9 +870,8 @@ class StageBase(TargetBase, ClearBase, GenBase):
                        # TODO: zmedico and I discussed making this a directory 
and pushing
                        # in a parent file, as well as other user-specified 
configuration.
                        log.info('Configuring profile link...')
-                       cmd("rm -f " + self.settings["chroot_path"] +
-                               self.settings["port_conf"] + "/make.profile",
-                               "Error zapping profile link",env=self.env)
+                       clear_path(self.settings['chroot_path'] +
+                               self.settings['port_conf'] + '/make.profile')
                        ensure_dirs(self.settings['chroot_path'] + 
self.settings['port_conf'])
                        cmd("ln -sf ../.." + self.settings["portdir"] + 
"/profiles/" +
                                self.settings["target_profile"] + " " +
@@ -1050,8 +1049,7 @@ class StageBase(TargetBase, ClearBase, GenBase):
                        # Modify and write out make.conf (for the chroot)
                        makepath = normpath(self.settings["chroot_path"] +
                                self.settings["make_conf"])
-                       cmd("rm -f " + makepath,\
-                               "Could not remove " + makepath, env=self.env)
+                       clear_path(makepath)
                        myf=open(makepath, "w")
                        myf.write("# These settings were set by the catalyst 
build script "
                                        "that automatically\n# built this 
stage.\n")
@@ -1164,8 +1162,7 @@ class StageBase(TargetBase, ClearBase, GenBase):
                else:
                        for x in self.settings["cleanables"]:
                                log.notice('Cleaning chroot: %s', x)
-                               cmd("rm -rf 
"+self.settings["destpath"]+x,"Couldn't clean "+\
-                                       x,env=self.env)
+                               clear_path(self.settings["destpath"] + x)
 
                # Put /etc/hosts back into place
                if 
os.path.exists(self.settings["chroot_path"]+"/etc/hosts.catalyst"):
@@ -1175,8 +1172,7 @@ class StageBase(TargetBase, ClearBase, GenBase):
 
                # Remove our overlay
                if os.path.exists(self.settings["chroot_path"] + 
self.settings["local_overlay"]):
-                       cmd("rm -rf " + self.settings["chroot_path"] + 
self.settings["local_overlay"],
-                               "Could not remove " + 
self.settings["local_overlay"], env=self.env)
+                       clear_path(self.settings["chroot_path"] + 
self.settings["local_overlay"])
 
                        make_conf = self.settings['chroot_path'] + 
self.settings['make_conf']
                        try:
@@ -1234,7 +1230,7 @@ class StageBase(TargetBase, ClearBase, GenBase):
                                        # We're going to shell out for all 
these cleaning
                                        # operations, so we get easy glob 
handling.
                                        log.notice('livecd: removing %s', x)
-                                       os.system("rm -rf 
"+self.settings["chroot_path"]+x)
+                                       clear_path(self.settings["chroot_path"] 
+ x)
                                try:
                                        if 
os.path.exists(self.settings["controller_file"]):
                                                
cmd(self.settings["controller_file"]+\

diff --git a/catalyst/fileops.py b/catalyst/fileops.py
index 5fbca26..4b9e200 100644
--- a/catalyst/fileops.py
+++ b/catalyst/fileops.py
@@ -87,7 +87,7 @@ def clear_dir(target, mode=0o755, chg_flags=False, 
remove=False,
                        return False
        elif os.path.exists(target):
                if clear_nondir:
-                       os.unlink(clear_nondir)
+                       os.unlink(target)
                else:
                        log.info('clear_dir failed: %s: is not a directory', 
target)
                        return False
@@ -101,3 +101,8 @@ def clear_dir(target, mode=0o755, chg_flags=False, 
remove=False,
 
        log.debug('DONE, returning True')
        return True
+
+
+def clear_path(target):
+       """Nuke |target| regardless of it being a dir or file."""
+       clear_dir(target, remove=True)

diff --git a/catalyst/targets/netboot2.py b/catalyst/targets/netboot2.py
index 769b945..986ce01 100644
--- a/catalyst/targets/netboot2.py
+++ b/catalyst/targets/netboot2.py
@@ -9,7 +9,7 @@ from stat import ST_UID, ST_GID, ST_MODE
 
 from catalyst import log
 from catalyst.support import (CatalystError, normpath, cmd, list_bashify)
-from catalyst.fileops import ensure_dirs
+from catalyst.fileops import ensure_dirs, clear_path
 
 from catalyst.base.stagebase import StageBase
 
@@ -56,10 +56,8 @@ class netboot2(StageBase):
                        log.notice('Resume point detected, skipping target path 
setup operation...')
                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.resume.enable("setup_target_path")
+                       clear_path(self.settings['target_path'])
+                       self.resume.enable("setup_target_path")
                ensure_dirs(self.settings["storedir"]+"/builds/")
 
        def copy_files_to_image(self):
@@ -135,8 +133,8 @@ class netboot2(StageBase):
                                        # we're going to shell out for all 
these cleaning operations,
                                        # so we get easy glob handling
                                        log.notice('netboot2: removing %s', x)
-                                       os.system("rm -rf " + 
self.settings["chroot_path"] +
-                                               self.settings["merge_path"] + x)
+                                       clear_path(self.settings['chroot_path'] 
+
+                                               self.settings['merge_path'] + x)
 
        def empty(self):
                if "autoresume" in self.settings["options"] \

Reply via email to