From 7a4e280313f57f714c499aed2566c93744d60851 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Thu, 28 Jan 2010 00:49:55 +0100 Subject: Delete empty overlay directories --- CHANGES | 4 ++++ layman/action.py | 6 +++++- layman/db.py | 17 +++++++++++------ layman/utils.py | 18 +++++++++++++++++- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/CHANGES b/CHANGES index 1e2168e..c3da745 100644 --- a/CHANGES +++ b/CHANGES @@ -9,6 +9,10 @@ Version TODO - Fix handling of non-existing overlays + - Now delete empty overlay directories .. + - on deletion of a non-existing overlay and + - after failed addition of an overlay. + Version 1.3.0 - Released 2010/01/19 =================================== diff --git a/layman/action.py b/layman/action.py index 0cd55f6..0ea4269 100644 --- a/layman/action.py +++ b/layman/action.py @@ -27,7 +27,7 @@ __version__ = "$Id: action.py 312 2007-04-09 19:45:49Z wrobel $" import os, sys from layman.db import DB, RemoteDB, UnknownOverlayException - +from layman.utils import path, delete_empty_directory from layman.debug import OUT #=============================================================================== @@ -266,6 +266,10 @@ class Delete: overlay = self.db.select(i) except UnknownOverlayException, error: OUT.warn(str(error), 2) + + mdir = path([self.db.config['storage'], i]) + delete_empty_directory(mdir) + result = 1 else: OUT.debug('Selected overlay', 7) diff --git a/layman/db.py b/layman/db.py index 252e81a..e0763b3 100644 --- a/layman/db.py +++ b/layman/db.py @@ -26,7 +26,7 @@ __version__ = "$Id: db.py 309 2007-04-09 16:23:38Z wrobel $" import os, codecs, os.path, urllib2, re, hashlib -from layman.utils import path +from layman.utils import path, delete_empty_directory from layman.overlay import Overlays, UnknownOverlayException from layman.debug import OUT @@ -116,11 +116,16 @@ class DB(Overlays): make_conf = MakeConf(self.config, self.overlays) make_conf.add(overlay) else: - raise Exception('Adding the overlay failed! Possible remains of' - ' the operation have NOT been removed and may b' - 'e left at ' + path([self.config['storage'], - overlay.name]) + '. Please re' - 'move them manually if required.') + mdir = path([self.config['storage'], overlay.name]) + delete_empty_directory(mdir) + if os.path.exists(mdir): + raise Exception('Adding overlay "%s" failed!' + ' Possible remains of the operation have NOT' + ' been removed and may be left at "%s".' + ' Please remove them manually if required.' \ + % (overlay.name, mdir)) + else: + raise Exception('Adding overlay "%s" failed!' % overlay.name) else: raise Exception('Overlay "' + overlay.name + '" already in the loca' 'l list!') diff --git a/layman/utils.py b/layman/utils.py index 0a824f1..62014d9 100644 --- a/layman/utils.py +++ b/layman/utils.py @@ -29,7 +29,8 @@ __version__ = '$Id: utils.py 236 2006-09-05 20:39:37Z wrobel $' # #------------------------------------------------------------------------------- -import types, re +import types, re, os +from layman.debug import OUT #=============================================================================== # @@ -102,6 +103,21 @@ def path(path_elements): return pathname +def delete_empty_directory(mdir): + if os.path.exists(mdir) and not os.listdir(mdir): + # Check for sufficient privileges + if os.access(mdir, os.W_OK): + OUT.info('Deleting _empty_ directory "%s"' % mdir, 2) + try: + os.rmdir(mdir) + except OSError, error: + OUT.warn(str(error)) + else: + OUT.warn('Insufficient permissions to delete _empty_ folder "%s".' % mdir) + import getpass + if getpass.getuser() != 'root': + OUT.warn('Hint: You are not root.') + #=============================================================================== # # Testing -- cgit v1.2.3-1-g7c22