Skip to content

Commit

Permalink
remove rich dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanMainguy committed Apr 23, 2024
1 parent f3a6bb8 commit cb367cd
Showing 1 changed file with 42 additions and 62 deletions.
104 changes: 42 additions & 62 deletions testingDataset/compare_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,11 @@
import logging
from pathlib import Path

from rich.console import Console
from rich.syntax import Syntax
from rich.panel import Panel

from rich.logging import RichHandler

from rich import box
from rich.theme import Theme
from collections import Counter
import json

import gzip

custom_theme = Theme({
"identical": "bold green",
"neutral": "bold yellow",
"difference": "bold red"
})
console = Console(theme=custom_theme)

def ordered(obj):
if isinstance(obj, dict):
Expand Down Expand Up @@ -53,14 +39,10 @@ def are_json_files_identical(file1, file2):
def are_text_files_identical(expected_file, file2, outdir=Path("./")):
diff_details = []
common_file = expected_file.name
# max_lines_to_display = 10

if filecmp.cmp(expected_file, file2, shallow=False):
return True, diff_details


# console.print(f"\n[bold red]{common_file} is different.[/bold red].")

# Compare file content

proper_open_e = gzip.open if expected_file.suffix == ".gz" else open
Expand Down Expand Up @@ -90,8 +72,9 @@ def are_text_files_identical(expected_file, file2, outdir=Path("./")):
prct_deleted = 100 * diff_event_counter["-"] / f2_line_count
diff_prct = max(prct_inserted, prct_deleted)
diff_details.append(f'{diff_event_counter["+"]} insertions(+), {diff_event_counter["-"]} deletions(-) - {diff_prct:.1f}% difference')
diff_details.append(f'code {diff_file}')
diff_details.append(f'code --diff {expected_file} {file2}')
diff_details.append(f'Check out diff in {diff_file}')
# vs code command to diff the files
# diff_details.append(f'code --diff {expected_file} {file2}')

return False, diff_details

Expand All @@ -115,41 +98,40 @@ def get_suffix_except_gz(path: Path):
return ext


def compare_directories(expected_dir, tested_dir, ignored_files, diff_outdir, extension_to_compare=[".tsv", ".json", '.gff', '.txt', '.csv', '.faa', '.fasta', ".yaml"]):
def compare_directories(expected_dir, tested_dir, ignored_files, diff_outdir, report_identical_files, extension_to_compare=[".tsv", ".json", '.gff', '.txt', '.csv', '.faa', '.fasta', ".yaml"]):

# Define directory information with color
expected_dir_info = f"- Expected directory: [bold green]{expected_dir}[/bold green]"
tested_dir_info = f"- Tested directory: [bold blue]{tested_dir}[/bold blue]"
expected_dir_info = f"- Expected directory: {expected_dir}"
tested_dir_info = f"- Tested directory: {tested_dir}"

# Create the panel
panel_title = "[bold cyan]Comparison of Directories:[/bold cyan]"
panel_content = "\n".join([expected_dir_info, tested_dir_info])
comparison_panel = Panel(panel_content, title=panel_title, title_align="center", expand=False)
console.print(comparison_panel)
print("\n===Comparison of Directories===")
print("\n".join([expected_dir_info, tested_dir_info]))
print('===============================')

# Compare directories
dcmp = compare_dir_recursively(expected_dir, tested_dir, ignored_files=ignored_files)
ignored_files_ext = [common_file for common_file in dcmp.common_files if get_suffix_except_gz(Path(common_file)) not in extension_to_compare]
if ignored_files:
console.print("\nFiles ignored for comparison:", style="bold")
print("\nFiles ignored for comparison:")
for ignored_file in ignored_files:
console.print(f" - {ignored_file}")
print(f" - {ignored_file}")

if ignored_files_ext:
console.print("\nFiles ignored for comparison due to their extensions:", style="bold")
print("\nFiles ignored for comparison due to their extensions:")
for ignored_file in ignored_files_ext:
console.print(f" - {ignored_file}")
print(f" - {ignored_file}")

# Report missing files
if dcmp.left_only:
console.print("\nMissing files or directories in Tested results:", style='neutral')
print("\nMissing files or directories in Tested results:")
for file in dcmp.left_only:
console.print(f" - {file}")
print(f" - {file}")

if dcmp.right_only:
console.print("\nUnexpected files or directories found in Tested results:", style='neutral')
print("\nUnexpected files or directories found in Tested results:")
for file in dcmp.right_only:
console.print(f" - {file}")
print(f" - {file}")

different_files = []
identical_files = []
Expand All @@ -170,49 +152,45 @@ def compare_directories(expected_dir, tested_dir, ignored_files, diff_outdir, ex
identical_files.append(common_file)
else:
different_files.append((common_file, details))
# if identical_files:
# console.print("\nIdentical files:", style='identical')
# for file in identical_files:
# console.print(f" - {file}")

if report_identical_files and identical_files:
print("\nIdentical files:")
for file in identical_files:
print(f" - {file}")

if different_files:
console.print("\nDifferent files:", style='difference')
print("\nDifferent files:")
for file, details in different_files:
console.print(f" - {file}")
print(f" - {file}")
for detail in details:
console.print(f"{detail}")
print(f"{detail}")
print()


# Generate summary report
console.print("\n[bold]Summary:[/bold]")

console.print(f"{len(identical_files)} file(s) identical.")

console.print(f"{len(ignored_files_ext)} file(s) ignored because of their extension.")
print("\nSummary:")

console.print(f"{len(dcmp.left_only)} file(s) unexpected in Tested results.", )
console.print(f"{len(dcmp.right_only)} file(s) missing in Tested results.")
# Display identical files count
print(f"{len(identical_files)} file(s) identical.")

console.print(f"{len(different_files)} file(s) different.")
# Display ignored files due to extension
print(f"{len(ignored_files_ext)} file(s) ignored due to their extension.")

# Display unexpected files in Tested results
print(f"{len(dcmp.left_only)} file(s) unexpectedly found in Tested results.")
print(f"{len(dcmp.right_only)} file(s) missing in Tested results.")

# Display different files count
print(f"{len(different_files)} file(s) differ.")

# Suggest updating expected directory if changes seem legitimate
# if missing_files or different_files:
# code_snippet_update = f"\ncp -r {tested_dir}/* {expected_dir}\n"

# console.print("\nIf changes in tested directory seem legitimate, please update the expected directory with the following command:")
# syntax = Syntax(code_snippet_update, "bash", theme="ansi_dark")
# console.print(Panel(syntax, title="Update expected results", expand=False, box=box.HORIZONTALS))


if different_files or dcmp.left_only or dcmp.right_only:
console.print('\nSome difference exist between the tested and the expected result directories', style="difference")
print('\nSome difference exist between the tested and the expected result directories')
exit(1)

else:
console.print('\nNo difference have been found between the tested and the expected result directories', style="identical")
print('\nNo difference have been found between the tested and the expected result directories')

def parse_arguments():
"""Parse script arguments."""
Expand All @@ -237,12 +215,14 @@ def main():

args = parse_arguments()

logging.basicConfig(level=logging.INFO, format="%(message)s", datefmt="[%X]", handlers=[RichHandler()])
report_identical_files = True

logging.basicConfig(level=logging.INFO, format="%(message)s")

Path.mkdir(args.outdir, exist_ok=True)


compare_directories(expected_dir=args.expected_dir, tested_dir=args.tested_dir, diff_outdir=args.outdir, ignored_files=args.ignored_files)
compare_directories(expected_dir=args.expected_dir, tested_dir=args.tested_dir, diff_outdir=args.outdir, ignored_files=args.ignored_files, report_identical_files=report_identical_files)

if __name__ == '__main__':
main()
Expand Down

0 comments on commit cb367cd

Please sign in to comment.