egencache: prune empty cache directories
diff --git a/bin/egencache b/bin/egencache
index 2f53b40..a75a341 100755
--- a/bin/egencache
+++ b/bin/egencache
@@ -426,6 +426,9 @@
 					"committing target: %s\n" % (ce,),
 					level=logging.ERROR, noiselevel=-1)
 
+		if hasattr(trg_cache, '_prune_empty_dirs'):
+			trg_cache._prune_empty_dirs()
+
 class GenUseLocalDesc(object):
 	def __init__(self, portdb, output=None,
 			preserve_comments=False):
diff --git a/pym/portage/cache/fs_template.py b/pym/portage/cache/fs_template.py
index a82e862..8f0636e 100644
--- a/pym/portage/cache/fs_template.py
+++ b/pym/portage/cache/fs_template.py
@@ -1,14 +1,14 @@
-# Copyright: 2005 Gentoo Foundation
+# Copyright 2005-2012 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
 # Author(s): Brian Harring (ferringb@gentoo.org)
-# License: GPL2
 
+import os as _os
 import sys
 from portage.cache import template
 from portage import os
 
 from portage.proxy.lazyimport import lazyimport
 lazyimport(globals(),
-	'portage.data:portage_gid',
 	'portage.exception:PortageException',
 	'portage.util:apply_permissions',
 )
@@ -22,10 +22,6 @@
 	attempt to ensure files have the specified owners/perms"""
 
 	def __init__(self, *args, **config):
-		"""throws InitializationError if needs args aren't specified
-		gid and perms aren't listed do to an oddity python currying mechanism
-		gid=portage_gid
-		perms=0665"""
 
 		for x, y in (("gid", -1), ("perms", -1)):
 			if x in config:
@@ -78,7 +74,17 @@
 					if self._perms != -1:
 						os.umask(um)
 
-	
+	def _prune_empty_dirs(self):
+		all_dirs = []
+		for parent, dirs, files in os.walk(self.location):
+			for x in dirs:
+				all_dirs.append(_os.path.join(parent, x))
+		while all_dirs:
+			try:
+				_os.rmdir(all_dirs.pop())
+			except OSError:
+				pass
+
 def gen_label(base, label):
 	"""if supplied label is a path, generate a unique label based upon label, and supplied base path"""
 	if label.find(os.path.sep) == -1: