From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-777493-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	by finch.gentoo.org (Postfix) with ESMTP id 130BC138ADA
	for <garchives@archives.gentoo.org>; Thu, 26 Feb 2015 20:13:10 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 2C6FCE08DA;
	Thu, 26 Feb 2015 20:13:01 +0000 (UTC)
Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183])
	(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 9BE00E08DA
	for <gentoo-commits@lists.gentoo.org>; Thu, 26 Feb 2015 20:13:00 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 8AAF2340C41
	for <gentoo-commits@lists.gentoo.org>; Thu, 26 Feb 2015 20:12:59 +0000 (UTC)
Received: from localhost.localdomain (localhost [127.0.0.1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 66096129DE
	for <gentoo-commits@lists.gentoo.org>; Thu, 26 Feb 2015 20:12:43 +0000 (UTC)
From: "Brian Dolbec" <dolsen@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Brian Dolbec" <dolsen@gentoo.org>
Message-ID: <1420091887.80493d8d32cb21d622bb2f514da68ac3106b7c35.dolsen@gentoo>
Subject: [gentoo-commits] proj/catalyst:master commit in: catalyst/base/
X-VCS-Repository: proj/catalyst
X-VCS-Files: catalyst/base/clearbase.py
X-VCS-Directories: catalyst/base/
X-VCS-Committer: dolsen
X-VCS-Committer-Name: Brian Dolbec
X-VCS-Revision: 80493d8d32cb21d622bb2f514da68ac3106b7c35
X-VCS-Branch: master
Date: Thu, 26 Feb 2015 20:12:43 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Archives-Salt: 3cd2bac8-1e3a-4c3b-a75c-f23706d2956a
X-Archives-Hash: 5c7ef2c71735fba0502e1b60272a149b
Message-ID: <20150226201243.0u0r34aCnciIJBgg55WjYTK2xZgNEoEnLNbYztiesrM@z>

commit:     80493d8d32cb21d622bb2f514da68ac3106b7c35
Author:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
AuthorDate: Sat Jun  1 06:24:46 2013 +0000
Commit:     Brian Dolbec <dolsen <AT> gentoo <DOT> org>
CommitDate: Thu Jan  1 05:58:07 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/catalyst.git;a=commit;h=80493d8d

Refactor ClearBase code to remove code duplication.

---
 catalyst/base/clearbase.py | 73 ++++++++++++++++++----------------------------
 1 file changed, 29 insertions(+), 44 deletions(-)

diff --git a/catalyst/base/clearbase.py b/catalyst/base/clearbase.py
index 585d4f2..8462a3c 100644
--- a/catalyst/base/clearbase.py
+++ b/catalyst/base/clearbase.py
@@ -41,60 +41,20 @@ class ClearBase(object):
 
 
 	def clear_chroot(self):
-		myemp=self.settings["chroot_path"]
-		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
-			"""
-			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)
-			ensure_dirs(myemp, mode=0755)
-			os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
-			os.chmod(myemp,mystat[ST_MODE])
+		print 'Clearing the chroot path ...'
+		self.clear_dir(self.settings["chroot_path"], 0755, True)
 
 
 	def clear_packages(self):
 		if "pkgcache" in self.settings["options"]:
 			print "purging the pkgcache ..."
-
-			myemp=self.settings["pkgcache_path"]
-			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
-				"""
-				mystat=os.stat(myemp)
-				#cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env)
-				shutil.rmtree(myemp)
-				ensure_dirs(myemp, mode=0755)
-				os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
-				os.chmod(myemp,mystat[ST_MODE])
+			self.clear_dir(self.settings["pkgcache_path"])
 
 
 	def clear_kerncache(self):
 		if "kerncache" in self.settings["options"]:
 			print "purging the kerncache ..."
-
-			myemp=self.settings["kerncache_path"]
-			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
-				"""
-				mystat=os.stat(myemp)
-				#cmd("rm -rf "+myemp, "Could not remove existing file: "+myemp,env=self.env)
-				shutil.rmtree(myemp)
-				ensure_dirs(myemp, mode=0755)
-				os.chown(myemp,mystat[ST_UID],mystat[ST_GID])
-				os.chmod(myemp,mystat[ST_MODE])
+			self.clear_dir(self.settings["kerncache_path"])
 
 
 	def purge(self):
@@ -113,3 +73,28 @@ class ClearBase(object):
 			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