Change summary format and fixes

This commit is contained in:
Cédric Leporcq 2021-10-18 07:45:54 +02:00
parent c24014b98f
commit 16b3c646c2
3 changed files with 72 additions and 48 deletions

View File

@ -313,10 +313,10 @@ def clean(**kwargs):
)
if clean_all or folders:
collection.remove_empty_folders(path)
summary = collection.remove_empty_folders(path)
if kwargs['delete_excluded']:
collection.remove_excluded_files()
summary = collection.remove_excluded_files()
if log_level < 30:
summary.print()

View File

@ -380,7 +380,7 @@ class Collection:
if self.root in src_path.parents:
self.db.delete_filepath(str(src_path.relative_to(self.root)))
self.summary.append((src_path, dest_path))
self.summary.append((src_path, self.mode))
record = True
else:
@ -405,9 +405,13 @@ class Collection:
for exclude in self.exclude:
if fnmatch(file_path, exclude):
if not self.dry_run:
self.remove(file_path)
self.summary.append((file_path, 'delete'))
break
return self.summary
def sort_file(self, src_path, dest_path, remove_duplicates=False):
'''
Copy or move file to dest_path.
@ -435,7 +439,9 @@ class Collection:
f'File in source and destination are identical. Duplicate will be ignored.'
)
if mode == 'move':
if not dry_run:
self.remove(src_path)
self.summary.append((src_path, 'delete'))
return None
else: # name is same, but file is different
self.logger.warning(
@ -774,7 +780,7 @@ class Collection:
metadata = media.get_metadata(self.root, loc, self.db, self.cache)
media.metadata['file_path'] = os.path.relpath(file_path, self.root)
self._add_db_data(media.metadata)
self.summary.append((file_path, file_path))
self.summary.append((file_path, 'record'))
return self.summary
@ -784,7 +790,7 @@ class Collection:
checksum = utils.checksum(file_path)
relpath = file_path.relative_to(self.root)
if checksum == self.db.get_checksum(relpath):
self.summary.append((file_path, file_path))
self.summary.append((file_path, 'record'))
else:
self.logger.error('{file_path} is corrupted')
self.summary.append((file_path, False))
@ -829,7 +835,7 @@ class Collection:
break
# set row attribute to the file
self._add_db_data(media.metadata)
self.summary.append((file_path, file_path))
self.summary.append((file_path, 'record'))
# Finally delete invalid rows
for row in invalid_db_rows:
@ -843,7 +849,7 @@ class Collection:
# if folder empty, delete it
files = os.listdir(directory)
if len(files) == 0:
self.logger.info(f"Removing empty folder: {directory}")
if not self.dry_run:
directory.rmdir()
if self.root in directory.parent.parents:
@ -939,24 +945,29 @@ class Collection:
return self.summary, record
def remove_empty_folders(self, path, remove_root=True):
def remove_empty_folders(self, directory, remove_root=True):
'Function to remove empty folders'
if not os.path.isdir(path):
return
if not os.path.isdir(directory):
self.summary.append((directory, False))
return self.summary
# remove empty subfolders
files = os.listdir(path)
files = os.listdir(directory)
if len(files):
for f in files:
fullpath = os.path.join(path, f)
fullpath = os.path.join(directory, f)
if os.path.isdir(fullpath):
self.remove_empty_folders(fullpath)
# if folder empty, delete it
files = os.listdir(path)
files = os.listdir(directory)
if len(files) == 0 and remove_root:
self.logger.info(f"Removing empty folder: {path}")
os.rmdir(path)
self.logger.info(f"Removing empty folder: {directory}")
if not self.dry_run:
os.rmdir(directory)
self.summary.append((directory, 'delete'))
return self.summary
def move_file(self, img_path, dest_path):
if not self.dry_run:
@ -1017,7 +1028,7 @@ class Collection:
self.move_file(img_path, dest_path)
moved_imgs.add(img_path)
if self._record_file(img_path, dest_path, media):
self.summary.append((img_path, dest_path))
self.summary.append((img_path, self.mode))
else:
self.summary.append((img_path, False))
result = False
@ -1028,7 +1039,7 @@ class Collection:
self.move_file(img_path, dest_path)
moved_imgs.add(img_path)
if self._record_file(img_path, dest_path, media_ref):
self.summary.append((img_path, dest_path))
self.summary.append((img_path, self.mode))
else:
self.summary.append((img_path, False))
result = False
@ -1069,7 +1080,7 @@ class Collection:
self.move_file(src_path, dest_path)
moved_files.add(src_path)
if self._record_file(src_path, dest_path, media):
self.summary.append((src_path, dest_path))
self.summary.append((src_path, self.mode))
else:
self.summary.append((src_path, False))
result = False

View File

@ -1,39 +1,52 @@
from tabulate import tabulate
class Summary(object):
class Summary:
def __init__(self):
self.records = []
self.success = 0
self.error = 0
self.error_items = []
self.modes = ('record', 'copy', 'move', 'delete')
self.result = {}
for mode in self.modes:
self.result[mode] = 0
self.errors = 0
self.errors_items = []
def append(self, row):
id, status = row
file_path, mode = row
if status:
self.success += 1
if mode:
for m in self.modes:
if mode == m:
self.result[mode] += 1
else:
self.error += 1
self.error_items.append(id)
self.errors += 1
self.errors_items.append(file_path)
def print(self):
if self.error > 0:
error_headers = ["File"]
error_result = []
for id in self.error_items:
error_result.append([id])
print('Errors details:')
print(tabulate(error_result, headers=error_headers))
print("\n")
headers = ["Metric", "Count"]
result = [
["Success", self.success],
["Error", self.error],
]
print()
print('Summary:')
print(tabulate(result, tablefmt="plain"))
for mode in self.result:
nb = self.result[mode]
if self.result[mode] != 0:
if mode == 'record':
print(f"SUMMARY: {nb} files recorded.")
elif mode == 'copy':
print(f"SUMMARY: {nb} files copied.")
elif mode == 'move':
print(f"SUMMARY: {nb} files moved.")
else:
print(f"SUMMARY: {nb} files deleted.")
if sum(self.result.values()) == 0 and not self.errors:
print(f"OK !!")
if self.errors > 0:
print()
errors_headers = [f"ERROR: {self.errors} errors reported in files:"]
errors_result = []
for path in self.errors_items:
errors_result.append([path])
print(tabulate(errors_result, headers=errors_headers))
print()