after remove and rename, remove newly emptied directories

This commit is contained in:
rolux 2012-10-09 14:37:56 +02:00
parent 41f40ed153
commit 4fad079b84

View file

@ -298,7 +298,25 @@ def organize():
def execute_organize(): def execute_organize():
def get_empty_directories():
empty_directories = []
for absolute_path, dirnames, filenames in os.walk(volume_path, followlinks=True):
if not dirnames and not filenames:
empty_directories.append(absolute_path)
return empty_directories
def remove_file(path):
print 'Removing "%s"' % path
try:
if os.path.isdir(path):
os.rmdir(path)
else:
os.remove(path)
except:
raise IOError('Could not remove file')
def rename_file(source, target): def rename_file(source, target):
print 'Renaming "%s" to "%s"' % (source, target)
if not os.path.exists(source): if not os.path.exists(source):
raise IOError('Source does not exist') raise IOError('Source does not exist')
elif os.path.exists(target): elif os.path.exists(target):
@ -315,22 +333,18 @@ def execute_organize():
sys.exit('%s not found' % FILES['organize']) sys.exit('%s not found' % FILES['organize'])
data = ox.jsonc.load(open(FILES['organize'])) data = ox.jsonc.load(open(FILES['organize']))
old_empty_directories = get_empty_directories()
remove = map(lambda x: os.path.join(volume_path, x), data['automatic']['remove']) remove = map(lambda x: os.path.join(volume_path, x), data['automatic']['remove'])
rename = map(lambda x: map(lambda y: os.path.join(volume_path, y), x), data['automatic']['rename']) rename = map(lambda x: map(lambda y: os.path.join(volume_path, y), x), data['automatic']['rename'])
errors = [] errors = []
for path in remove: for path in remove:
print 'Removing "%s"' % path
try: try:
if os.path.isdir(path): remove_file(path)
os.rmdir(path)
else:
os.remove(path)
except: except:
errors.append('Could not remove "%s"' % path) errors.append('Could not remove "%s"' % path)
for paths in rename: for paths in rename:
source = paths[0] source = paths[0]
target = paths[1] + '.pandora' target = paths[1] + '.pandora'
print 'Renaming "%s" to "%s"' % (source, target)
try: try:
rename_file(source, target) rename_file(source, target)
except IOError as error: except IOError as error:
@ -338,11 +352,21 @@ def execute_organize():
for paths in rename: for paths in rename:
source = paths[1] + '.pandora' source = paths[1] + '.pandora'
target = paths[1] target = paths[1]
print 'Renaming "%s" to "%s"' % (source, target)
try: try:
rename_file(source, target) rename_file(source, target)
except IOError as error: except IOError as error:
errors.append('Could not rename "%s" to "%s" (%s)' % (source, target, error)) errors.append('Could not rename "%s" to "%s" (%s)' % (source, target, error))
while True:
new_empty_directories = [path for path in get_empty_directories() if not path in old_empty_directories]
if new_empty_directories:
for path in new_empty_directories:
try:
remove_file(path)
except:
errors.append('Could not remove "%s"' % path)
else:
break
for error in errors: for error in errors:
print error print error