after remove and rename, remove newly emptied directories
This commit is contained in:
parent
41f40ed153
commit
4fad079b84
1 changed files with 31 additions and 7 deletions
|
@ -298,7 +298,25 @@ def 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):
|
||||
print 'Renaming "%s" to "%s"' % (source, target)
|
||||
if not os.path.exists(source):
|
||||
raise IOError('Source does not exist')
|
||||
elif os.path.exists(target):
|
||||
|
@ -315,22 +333,18 @@ def execute_organize():
|
|||
sys.exit('%s not found' % 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'])
|
||||
rename = map(lambda x: map(lambda y: os.path.join(volume_path, y), x), data['automatic']['rename'])
|
||||
errors = []
|
||||
for path in remove:
|
||||
print 'Removing "%s"' % path
|
||||
try:
|
||||
if os.path.isdir(path):
|
||||
os.rmdir(path)
|
||||
else:
|
||||
os.remove(path)
|
||||
remove_file(path)
|
||||
except:
|
||||
errors.append('Could not remove "%s"' % path)
|
||||
for paths in rename:
|
||||
source = paths[0]
|
||||
target = paths[1] + '.pandora'
|
||||
print 'Renaming "%s" to "%s"' % (source, target)
|
||||
try:
|
||||
rename_file(source, target)
|
||||
except IOError as error:
|
||||
|
@ -338,11 +352,21 @@ def execute_organize():
|
|||
for paths in rename:
|
||||
source = paths[1] + '.pandora'
|
||||
target = paths[1]
|
||||
print 'Renaming "%s" to "%s"' % (source, target)
|
||||
try:
|
||||
rename_file(source, target)
|
||||
except IOError as 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:
|
||||
print error
|
||||
|
||||
|
|
Loading…
Reference in a new issue