Change summary format and fixes
This commit is contained in:
parent
c24014b98f
commit
16b3c646c2
|
@ -313,10 +313,10 @@ def clean(**kwargs):
|
||||||
)
|
)
|
||||||
|
|
||||||
if clean_all or folders:
|
if clean_all or folders:
|
||||||
collection.remove_empty_folders(path)
|
summary = collection.remove_empty_folders(path)
|
||||||
|
|
||||||
if kwargs['delete_excluded']:
|
if kwargs['delete_excluded']:
|
||||||
collection.remove_excluded_files()
|
summary = collection.remove_excluded_files()
|
||||||
|
|
||||||
if log_level < 30:
|
if log_level < 30:
|
||||||
summary.print()
|
summary.print()
|
||||||
|
|
|
@ -380,7 +380,7 @@ class Collection:
|
||||||
if self.root in src_path.parents:
|
if self.root in src_path.parents:
|
||||||
self.db.delete_filepath(str(src_path.relative_to(self.root)))
|
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
|
record = True
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -405,9 +405,13 @@ class Collection:
|
||||||
|
|
||||||
for exclude in self.exclude:
|
for exclude in self.exclude:
|
||||||
if fnmatch(file_path, exclude):
|
if fnmatch(file_path, exclude):
|
||||||
self.remove(file_path)
|
if not self.dry_run:
|
||||||
|
self.remove(file_path)
|
||||||
|
self.summary.append((file_path, 'delete'))
|
||||||
break
|
break
|
||||||
|
|
||||||
|
return self.summary
|
||||||
|
|
||||||
def sort_file(self, src_path, dest_path, remove_duplicates=False):
|
def sort_file(self, src_path, dest_path, remove_duplicates=False):
|
||||||
'''
|
'''
|
||||||
Copy or move file to dest_path.
|
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.'
|
f'File in source and destination are identical. Duplicate will be ignored.'
|
||||||
)
|
)
|
||||||
if mode == 'move':
|
if mode == 'move':
|
||||||
self.remove(src_path)
|
if not dry_run:
|
||||||
|
self.remove(src_path)
|
||||||
|
self.summary.append((src_path, 'delete'))
|
||||||
return None
|
return None
|
||||||
else: # name is same, but file is different
|
else: # name is same, but file is different
|
||||||
self.logger.warning(
|
self.logger.warning(
|
||||||
|
@ -774,7 +780,7 @@ class Collection:
|
||||||
metadata = media.get_metadata(self.root, loc, self.db, self.cache)
|
metadata = media.get_metadata(self.root, loc, self.db, self.cache)
|
||||||
media.metadata['file_path'] = os.path.relpath(file_path, self.root)
|
media.metadata['file_path'] = os.path.relpath(file_path, self.root)
|
||||||
self._add_db_data(media.metadata)
|
self._add_db_data(media.metadata)
|
||||||
self.summary.append((file_path, file_path))
|
self.summary.append((file_path, 'record'))
|
||||||
|
|
||||||
return self.summary
|
return self.summary
|
||||||
|
|
||||||
|
@ -784,7 +790,7 @@ class Collection:
|
||||||
checksum = utils.checksum(file_path)
|
checksum = utils.checksum(file_path)
|
||||||
relpath = file_path.relative_to(self.root)
|
relpath = file_path.relative_to(self.root)
|
||||||
if checksum == self.db.get_checksum(relpath):
|
if checksum == self.db.get_checksum(relpath):
|
||||||
self.summary.append((file_path, file_path))
|
self.summary.append((file_path, 'record'))
|
||||||
else:
|
else:
|
||||||
self.logger.error('{file_path} is corrupted')
|
self.logger.error('{file_path} is corrupted')
|
||||||
self.summary.append((file_path, False))
|
self.summary.append((file_path, False))
|
||||||
|
@ -829,7 +835,7 @@ class Collection:
|
||||||
break
|
break
|
||||||
# set row attribute to the file
|
# set row attribute to the file
|
||||||
self._add_db_data(media.metadata)
|
self._add_db_data(media.metadata)
|
||||||
self.summary.append((file_path, file_path))
|
self.summary.append((file_path, 'record'))
|
||||||
|
|
||||||
# Finally delete invalid rows
|
# Finally delete invalid rows
|
||||||
for row in invalid_db_rows:
|
for row in invalid_db_rows:
|
||||||
|
@ -843,8 +849,8 @@ class Collection:
|
||||||
# if folder empty, delete it
|
# if folder empty, delete it
|
||||||
files = os.listdir(directory)
|
files = os.listdir(directory)
|
||||||
if len(files) == 0:
|
if len(files) == 0:
|
||||||
self.logger.info(f"Removing empty folder: {directory}")
|
if not self.dry_run:
|
||||||
directory.rmdir()
|
directory.rmdir()
|
||||||
|
|
||||||
if self.root in directory.parent.parents:
|
if self.root in directory.parent.parents:
|
||||||
parents.add(directory.parent)
|
parents.add(directory.parent)
|
||||||
|
@ -939,24 +945,29 @@ class Collection:
|
||||||
|
|
||||||
return self.summary, record
|
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'
|
'Function to remove empty folders'
|
||||||
if not os.path.isdir(path):
|
if not os.path.isdir(directory):
|
||||||
return
|
self.summary.append((directory, False))
|
||||||
|
return self.summary
|
||||||
|
|
||||||
# remove empty subfolders
|
# remove empty subfolders
|
||||||
files = os.listdir(path)
|
files = os.listdir(directory)
|
||||||
if len(files):
|
if len(files):
|
||||||
for f in files:
|
for f in files:
|
||||||
fullpath = os.path.join(path, f)
|
fullpath = os.path.join(directory, f)
|
||||||
if os.path.isdir(fullpath):
|
if os.path.isdir(fullpath):
|
||||||
self.remove_empty_folders(fullpath)
|
self.remove_empty_folders(fullpath)
|
||||||
|
|
||||||
# if folder empty, delete it
|
# if folder empty, delete it
|
||||||
files = os.listdir(path)
|
files = os.listdir(directory)
|
||||||
if len(files) == 0 and remove_root:
|
if len(files) == 0 and remove_root:
|
||||||
self.logger.info(f"Removing empty folder: {path}")
|
self.logger.info(f"Removing empty folder: {directory}")
|
||||||
os.rmdir(path)
|
if not self.dry_run:
|
||||||
|
os.rmdir(directory)
|
||||||
|
self.summary.append((directory, 'delete'))
|
||||||
|
|
||||||
|
return self.summary
|
||||||
|
|
||||||
def move_file(self, img_path, dest_path):
|
def move_file(self, img_path, dest_path):
|
||||||
if not self.dry_run:
|
if not self.dry_run:
|
||||||
|
@ -1017,7 +1028,7 @@ class Collection:
|
||||||
self.move_file(img_path, dest_path)
|
self.move_file(img_path, dest_path)
|
||||||
moved_imgs.add(img_path)
|
moved_imgs.add(img_path)
|
||||||
if self._record_file(img_path, dest_path, media):
|
if self._record_file(img_path, dest_path, media):
|
||||||
self.summary.append((img_path, dest_path))
|
self.summary.append((img_path, self.mode))
|
||||||
else:
|
else:
|
||||||
self.summary.append((img_path, False))
|
self.summary.append((img_path, False))
|
||||||
result = False
|
result = False
|
||||||
|
@ -1028,7 +1039,7 @@ class Collection:
|
||||||
self.move_file(img_path, dest_path)
|
self.move_file(img_path, dest_path)
|
||||||
moved_imgs.add(img_path)
|
moved_imgs.add(img_path)
|
||||||
if self._record_file(img_path, dest_path, media_ref):
|
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:
|
else:
|
||||||
self.summary.append((img_path, False))
|
self.summary.append((img_path, False))
|
||||||
result = False
|
result = False
|
||||||
|
@ -1069,7 +1080,7 @@ class Collection:
|
||||||
self.move_file(src_path, dest_path)
|
self.move_file(src_path, dest_path)
|
||||||
moved_files.add(src_path)
|
moved_files.add(src_path)
|
||||||
if self._record_file(src_path, dest_path, media):
|
if self._record_file(src_path, dest_path, media):
|
||||||
self.summary.append((src_path, dest_path))
|
self.summary.append((src_path, self.mode))
|
||||||
else:
|
else:
|
||||||
self.summary.append((src_path, False))
|
self.summary.append((src_path, False))
|
||||||
result = False
|
result = False
|
||||||
|
|
|
@ -1,39 +1,52 @@
|
||||||
from tabulate import tabulate
|
from tabulate import tabulate
|
||||||
|
|
||||||
|
|
||||||
class Summary(object):
|
class Summary:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.records = []
|
self.modes = ('record', 'copy', 'move', 'delete')
|
||||||
self.success = 0
|
self.result = {}
|
||||||
self.error = 0
|
for mode in self.modes:
|
||||||
self.error_items = []
|
self.result[mode] = 0
|
||||||
|
|
||||||
|
self.errors = 0
|
||||||
|
self.errors_items = []
|
||||||
|
|
||||||
def append(self, row):
|
def append(self, row):
|
||||||
id, status = row
|
file_path, mode = row
|
||||||
|
|
||||||
if status:
|
if mode:
|
||||||
self.success += 1
|
for m in self.modes:
|
||||||
|
if mode == m:
|
||||||
|
self.result[mode] += 1
|
||||||
else:
|
else:
|
||||||
self.error += 1
|
self.errors += 1
|
||||||
self.error_items.append(id)
|
self.errors_items.append(file_path)
|
||||||
|
|
||||||
def print(self):
|
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()
|
||||||
print('Summary:')
|
for mode in self.result:
|
||||||
print(tabulate(result, tablefmt="plain"))
|
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()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue