commit:     2f8312854d9fac7476f8f972660824941dc4c852
Author:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
AuthorDate: Mon Dec 30 21:19:28 2013 +0000
Commit:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
CommitDate: Thu Feb 26 20:08:06 2015 +0000
URL:        
http://sources.gentoo.org/gitweb/?p=proj/catalyst.git;a=commit;h=2f831285

Move some duplicate code to fileops, extend it's capability to not re-make the 
directory.

---
 catalyst/base/clearbase.py | 71 +++++++++++++++-------------------------------
 catalyst/base/resume.py    | 26 ++++-------------
 catalyst/fileops.py        | 43 +++++++++++++++++++++++++++-
 3 files changed, 70 insertions(+), 70 deletions(-)

diff --git a/catalyst/base/clearbase.py b/catalyst/base/clearbase.py
index 0ebe299..e38b1a8 100644
--- a/catalyst/base/clearbase.py
+++ b/catalyst/base/clearbase.py
@@ -5,7 +5,7 @@ from stat import ST_UID, ST_GID, ST_MODE
 
 
 from catalyst.support import cmd, countdown
-from catalyst.fileops import ensure_dirs
+from catalyst.fileops import ensure_dirs, clear_dir
 
 class ClearBase(object):
        """
@@ -16,68 +16,43 @@ class ClearBase(object):
                self.resume = None
 
 
-       def clear_autoresume(self):
+       def clear_autoresume(self, remove=False):
                """ Clean resume points since they are no longer needed """
                if "autoresume" in self.settings["options"]:
                        print "Removing AutoResume Points: ..."
                        self.resume.clear_all()
 
 
-       def clear_chroot(self):
+       def clear_chroot(self, remove=False):
                print 'Clearing the chroot path ...'
-               self.clear_dir(self.settings["chroot_path"], 0755, True)
+               clear_dir(self.settings["chroot_path"], 0755, True, remove)
 
 
-       def clear_packages(self):
+       def clear_packages(self, remove=False):
                if "pkgcache" in self.settings["options"]:
                        print "purging the pkgcache ..."
-                       self.clear_dir(self.settings["pkgcache_path"])
+                       clear_dir(self.settings["pkgcache_path"], remove=remove)
 
 
-       def clear_kerncache(self):
+       def clear_kerncache(self, remove=False):
                if "kerncache" in self.settings["options"]:
                        print "purging the kerncache ..."
-                       self.clear_dir(self.settings["kerncache_path"])
+                       clear_dir(self.settings["kerncache_path"], 
remove=remove)
 
 
-       def purge(self):
+       def purge(self, remove=False):
                countdown(10,"Purging Caches ...")
-               if any(k in self.settings["options"] for k in 
("purge","purgeonly","purgetmponly")):
-                       print "clearing autoresume ..."
-                       self.clear_autoresume()
-
-                       print "clearing chroot ..."
-                       self.clear_chroot()
-
-                       if "PURGETMPONLY" not in self.settings:
-                               print "clearing package cache ..."
-                               self.clear_packages()
-
-                       print "clearing kerncache ..."
-                       self.clear_kerncache()
-
-
-       def clear_dir(self, myemp, mode=0755, chg_flags=False):
-               '''Universal directory clearing function
-               '''
-               if not myemp:
-                       return False
-               if os.path.isdir(myemp):
-                       print "Emptying directory" , myemp
-                       """
-                       stat the dir, delete the dir, recreate the dir and set
-                       the proper perms and ownership
-                       """
-                       try:
-                               mystat=os.stat(myemp)
-                               """ There's no easy way to change flags 
recursively in python """
-                               if chg_flags and os.uname()[0] == "FreeBSD":
-                                       os.system("chflags -R noschg " + myemp)
-                               shutil.rmtree(myemp)
-                               ensure_dirs(myemp, mode=mode)
-                               os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
-                               os.chmod(myemp,mystat[ST_MODE])
-                       except Exception as e:
-                               print CatalystError("clear_dir(); Exeption: %s" 
% str(e))
-                               return False
-                       return True
+               if any(k in self.settings["options"] for k in ("purge",
+                               "purgeonly", "purgetmponly")):
+                       print "purge(); clearing autoresume ..."
+                       self.clear_autoresume(remove)
+
+                       print "purge(); clearing chroot ..."
+                       self.clear_chroot(remove)
+
+                       if "purgetmponly" not in self.settings["options"]:
+                               print "purge(); clearing package cache ..."
+                               self.clear_packages(remove)
+
+                       print "purge(); clearing kerncache ..."
+                       self.clear_kerncache(remove)

diff --git a/catalyst/base/resume.py b/catalyst/base/resume.py
index e42c7dc..cf495fc 100644
--- a/catalyst/base/resume.py
+++ b/catalyst/base/resume.py
@@ -16,7 +16,7 @@ import shutil
 from stat import ST_UID, ST_GID, ST_MODE
 import traceback
 
-from catalyst.fileops import ensure_dirs, pjoin, listdir_files
+from catalyst.fileops import ensure_dirs, pjoin, listdir_files, clear_dir
 from catalyst.support import touch
 
 
@@ -139,28 +139,12 @@ class AutoResume(object):
                return list(self._points)
 
 
-       def clear_all(self):
+       def clear_all(self, remove=False):
                '''Clear all active resume points
 
                @return boolean
                '''
-               try:
-                       print "Emptying directory---", self.basedir
-                       """
-                       stat the dir, delete the dir, recreate the dir and set
-                       the proper perms and ownership
-                       """
-                       mystat=os.stat(self.basedir)
-                       if os.uname()[0] == "FreeBSD":
-                               cmd("chflags -R noschg " + self.basedir,\
-                                       "Could not remove immutable flag for 
file "\
-                                       + self.basedir)
-                       shutil.rmtree(self.basedir)
-                       ensure_dirs(self.basedir, 0755)
-                       os.chown(self.basedir,mystat[ST_UID],mystat[ST_GID])
-                       os.chmod(self.basedir,mystat[ST_MODE])
+               if clear_dir(self.basedir, mode=0755, chg_flags=True, 
remove=remove):
                        self._points = {}
-               except Exception as e:
-                       print AutoResumeError(str(e))
-                       return False
-               return True
+                       return True
+               return False

diff --git a/catalyst/fileops.py b/catalyst/fileops.py
index e3a4ead..245c83e 100644
--- a/catalyst/fileops.py
+++ b/catalyst/fileops.py
@@ -10,7 +10,13 @@ ensuring directories exist,... imports snakeoils osutils
 functions for use throughout catalyst.
 '''
 
-from snakeoil.osutils import (ensure_dirs as snakeoil_ensure_dirs, normpath,
+import os
+import shutil
+from stat import ST_UID, ST_GID, ST_MODE
+
+# NOTE: pjoin and listdir_files are imported here for export
+# to other catalyst modules
+from snakeoil.osutils import (ensure_dirs as snakeoil_ensure_dirs,
        pjoin, listdir_files)
 from catalyst.support import CatalystError
 
@@ -42,3 +48,38 @@ def ensure_dirs(path, gid=-1, uid=-1, mode=0777, 
minimal=True,
                        raise CatalystError(
                                "Failed to create directory: %s" % path, 
print_traceback=True)
        return succeeded
+
+
+def clear_dir(target, mode=0755, chg_flags=False, remove=False):
+       '''Universal directory clearing function
+       '''
+       #print "fileops.clear_dir()"
+       if not target:
+               #print "fileops.clear_dir(), no target... returning"
+               return False
+       if os.path.isdir(target):
+               print "Emptying directory" , target
+               """
+               stat the dir, delete the dir, recreate the dir and set
+               the proper perms and ownership
+               """
+               try:
+                       #print "fileops.clear_dir(), os.stat()"
+                       mystat=os.stat(target)
+                       """ There's no easy way to change flags recursively in 
python """
+                       if chg_flags and os.uname()[0] == "FreeBSD":
+                               os.system("chflags -R noschg " + target)
+                       #print "fileops.clear_dir(), shutil.rmtree()"
+                       shutil.rmtree(target)
+                       if not remove:
+                               #print "fileops.clear_dir(), ensure_dirs()"
+                               ensure_dirs(target, mode=mode)
+                               os.chown(target, mystat[ST_UID], mystat[ST_GID])
+                               os.chmod(target, mystat[ST_MODE])
+               except Exception as e:
+                       print CatalystError("clear_dir(); Exeption: %s" % 
str(e))
+                       return False
+       else:
+               print "fileops.clear_dir(), %s is not a directory" % (target)
+       #print "fileops.clear_dir(), DONE, returning True"
+       return True

Reply via email to