Skip to content

Commit

Permalink
fixed error in summary logging
Browse files Browse the repository at this point in the history
added a final summary to console output

some code cleanups (deleted commented function)
  • Loading branch information
niradar committed Jun 1, 2024
1 parent f9f579d commit d427088
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,11 @@ pip install -r requirements.txt
```

## Possible Future Improvements
- [ ] Improve summary and user interface
- [ ] Add a better summary of the actions taken
- [ ] Add summary also to the console output
- [ ] Better handling of folders with saved html files
- [ ] Deal with `_files` folders in the source folder
- [ ] Move it only if all files are duplicates
- [ ] Deal with `_files` folders in the source folder - Move it only if all files are duplicates
- [ ] More ways to influence how the script works
- [ ] Add an argument to act only if the entire folder is a subfolder of a target folder, recursively (bottom-up)
- [ ] Option to keep the source folder structure in the move_to folder
- [ ] Option to send duplicates to recycle bin instead of move_to folder
- [ ] More safeguards
- [ ] Check any file operation for errors
## Known Issues
- [ ] Even if argument --copy_to_all is not present, still need to move the duplicates to the move_to folder without copying them to other folders
- [ ] Issue with files with non-standard characters in the filename - no reproducible yet
Expand Down
2 changes: 1 addition & 1 deletion df_finder3.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def main(args):

deleted_source_folders = fm.delete_empty_folders_in_tree(args.src, True) if args.delete_empty_folders else 0
hash_manager.save_data()
output_results(args, deleted_source_folders, duplicate_source_files_moved, files_created, files_moved, hash_manager)
output_results(args, files_moved, files_created, deleted_source_folders, duplicate_source_files_moved)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion duplicate_files_in_folders/duplicates_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def process_duplicates(combined: Dict, args) -> (int, int):
copy_or_move_file(tgt['path'], args.move_to, src, args.target, not args.run, move=True)
files_moved += 1

return files_created, files_moved
return files_moved, files_created


def clean_source_duplications(args, combined):
Expand Down
50 changes: 27 additions & 23 deletions duplicate_files_in_folders/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,39 @@ def print_error(message):
logger.critical(f"{message}")
sys.exit(1)


def output_results(args, deleted_source_folders, duplicate_source_files_moved, files_created, files_moved, hash_manager):
def output_results(args, files_moved, files_created, deleted_source_folders, duplicate_source_files_moved):
summary_header = "Summary (Test Mode):" if not args.run else "Summary:"
separator = "-" * max(len(summary_header), 40)
cache_hits = f"Hash requests: {hash_manager.persistent_cache_requests + hash_manager.temporary_cache_requests}," + \
f" Cache hits: {hash_manager.persistent_cache_hits + hash_manager.temporary_cache_hits}"
logger.info(summary_header)
logger.info(separator)
blank_line = ""
fixed_width = 25

# Header
log_and_print("")
log_and_print(summary_header)
log_and_print(separator)
hash_manager = HashManager.get_instance()
# Cache hits information
cache_hits = f"Hash requests: {hash_manager.persistent_cache_requests + hash_manager.temporary_cache_requests}, " \
f"Cache hits: {hash_manager.persistent_cache_hits + hash_manager.temporary_cache_hits}"
logger.debug(cache_hits)
res_str = f'Move: {files_moved} files, Create: {files_created} copies'

# Detailed summary
summary_lines = {
'Files Moved': f"{files_moved}",
'Files Created': f"{files_created} copies",
}

if duplicate_source_files_moved:
res_str += f", Moved {duplicate_source_files_moved} duplicate files from the source folder"
summary_lines['Duplicate Files Moved'] = f"{duplicate_source_files_moved} duplicate files from the source folder"
if deleted_source_folders:
res_str += f", Deleted: {deleted_source_folders} empty folders in the source folder"
logger.info(res_str)
summary_lines['Empty Folders Deleted'] = f"{deleted_source_folders} empty folders in the source folder"

for key, value in summary_lines.items():
log_and_print(f"{key.ljust(fixed_width)}: {value}")

# Footer
log_and_print(separator)
log_and_print("")


def setup_hash_manager(args):
Expand All @@ -252,19 +269,6 @@ def setup_hash_manager(args):
return hash_manager


# def copy_or_move_file(tgt_filepath: str, move_to: str, src_filepath: str, target: str, test_mode, move=True):
# new_src_path = os.path.join(move_to, os.path.relpath(tgt_filepath, target))
# new_src_dir = os.path.dirname(new_src_path)
# fm = FileManager(not test_mode)
# if not os.path.exists(new_src_dir):
# fm.make_dirs(new_src_dir)
# new_filename = check_and_update_filename(new_src_path)
# if move:
# fm.move_file(src_filepath, new_filename)
# else:
# fm.copy_file(src_filepath, new_filename)
# return new_filename

def copy_or_move_file(target_file_path: str, destination_base_path: str, source_file_path: str, base_target_path: str, is_test_mode: bool, move: bool = True) -> str:
destination_path = os.path.join(destination_base_path, os.path.relpath(target_file_path, base_target_path))
destination_dir = os.path.dirname(destination_path)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_duplicates_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def test_process_duplicates(setup_teardown):

args = parse_arguments(common_args)
duplicates, source_stats, target_stats = find_duplicates_files_v3(args, source_dir, target_dir)
files_created, files_moved = process_duplicates(duplicates, args)
files_moved, files_created = process_duplicates(duplicates, args)
assert files_created == 0
assert files_moved == 5

Expand All @@ -195,7 +195,7 @@ def test_process_duplicates(setup_teardown):
"--ignore_diff", "filename"]
args = parse_arguments(common_args)
duplicates, source_stats, target_stats = find_duplicates_files_v3(args, source_dir, target_dir)
files_created, files_moved = process_duplicates(duplicates, args)
files_moved, files_created = process_duplicates(duplicates, args)
assert files_created == 0
assert files_moved == 0

Expand All @@ -205,7 +205,7 @@ def test_process_duplicates(setup_teardown):
"--ignore_diff", "checkall"]
args = parse_arguments(common_args)
duplicates, source_stats, target_stats = find_duplicates_files_v3(args, source_dir, target_dir)
files_created, files_moved = process_duplicates(duplicates, args)
files_moved, files_created = process_duplicates(duplicates, args)
assert files_created == 0
assert files_moved == 0

Expand All @@ -215,7 +215,7 @@ def test_process_duplicates(setup_teardown):
"--ignore_diff", "filename,mdate"]
args = parse_arguments(common_args)
duplicates, source_stats, target_stats = find_duplicates_files_v3(args, source_dir, target_dir)
files_created, files_moved = process_duplicates(duplicates, args)
files_moved, files_created = process_duplicates(duplicates, args)
assert files_created == 0
assert files_moved == 5

Expand All @@ -229,7 +229,7 @@ def test_process_duplicates(setup_teardown):
common_args = ["--src", source_dir, "--target", target_dir, "--move_to", move_to_dir, "--run", "--copy_to_all"]
args = parse_arguments(common_args)
duplicates, source_stats, target_stats = find_duplicates_files_v3(args, source_dir, target_dir)
files_created, files_moved = process_duplicates(duplicates, args)
files_moved, files_created = process_duplicates(duplicates, args)
assert files_created == 5
assert files_moved == 5
assert os.path.exists(os.path.join(move_to_dir, "subfolder"))
Expand All @@ -244,7 +244,7 @@ def test_process_duplicates(setup_teardown):
common_args = ["--src", source_dir, "--target", target_dir, "--move_to", move_to_dir, "--run"]
args = parse_arguments(common_args)
duplicates, source_stats, target_stats = find_duplicates_files_v3(args, source_dir, target_dir)
files_created, files_moved = process_duplicates(duplicates, args)
files_moved, files_created = process_duplicates(duplicates, args)
assert files_created == 0
assert files_moved == 5

0 comments on commit d427088

Please sign in to comment.