Skip to content

Commit

Permalink
Fix ./mfc.sh count_diff
Browse files Browse the repository at this point in the history
  • Loading branch information
henryleberre committed May 29, 2024
1 parent 2efe8b7 commit 0092aeb
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions toolchain/mfc/count.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from .common import MFC_ROOTDIR, format_list_to_string, MFCException
from .printer import cons

def handle_dir(dirpath: str) -> typing.Tuple[typing.List[typing.Tuple[str, int]], int]:
files = []
def handle_dir(mfc_dir: str, srcdirname: str) -> typing.Tuple[typing.Dict[str, int], int]:
files = {}
total = 0

for filepath in glob.glob(os.path.join(dirpath, '*.*f*')):
for filepath in glob.glob(os.path.join(mfc_dir, 'src', srcdirname, '*.*f*')):
with open(filepath) as f:
counter = 0
for l in f.read().split('\n'):
Expand All @@ -21,11 +21,9 @@ def handle_dir(dirpath: str) -> typing.Tuple[typing.List[typing.Tuple[str, int]]
continue
counter += 1

files.append((filepath, counter))
files[os.path.relpath(filepath, mfc_dir)] = counter
total += counter

files.sort(key=lambda x: x[1], reverse=True)

return (files, total)

def count():
Expand All @@ -36,13 +34,13 @@ def count():

total = 0
for codedir in ['common'] + ARG("targets"):
dirfiles, dircount = handle_dir(os.path.join(MFC_ROOTDIR, 'src', codedir))
dirfiles, dircount = handle_dir(MFC_ROOTDIR, codedir)
table = rich.table.Table(show_header=True, box=rich.table.box.SIMPLE)
table.add_column(f"File (in [magenta]{codedir}[/magenta])", justify="left")
table.add_column(f"Lines ([cyan]{dircount}[/cyan])", justify="right")

for filepath, n in dirfiles:
table.add_row(f"{os.path.basename(filepath)}", f"[bold cyan]{n}[/bold cyan]")
for filepath, n in dirfiles.items():
table.add_row(os.path.basename(filepath), f"[bold cyan]{n}[/bold cyan]")

total += dircount

Expand All @@ -67,35 +65,32 @@ def count_diff():

# MFC_COMPAREDIR="/Users/spencer/Downloads/MFC-shbfork"
for codedir in ['common'] + ARG("targets"):
dirfiles_root, dircount = handle_dir(os.path.join(MFC_ROOTDIR, 'src', codedir))
dirfiles_pr, dircount_pr = handle_dir(os.path.join(MFC_COMPAREDIR, 'src', codedir))
dirfiles_root, dircount_root = handle_dir(MFC_ROOTDIR, codedir)
dirfiles_pr, dircount_pr = handle_dir(MFC_COMPAREDIR, codedir)
table = rich.table.Table(show_header=True, box=rich.table.box.SIMPLE)
table.add_column(f"File (in [magenta]{codedir}[/magenta])", justify="left")
table.add_column(f"Lines [HEAD] ([cyan]{dircount}[/cyan])", justify="right")
table.add_column(f"Lines [HEAD] ([cyan]{dircount_root}[/cyan])", justify="right")
table.add_column(f"Lines [PR] ([cyan]{dircount_pr}[/cyan])", justify="right")
table.add_column("", justify="right")
table.add_column("Diff", justify="right")

ii = 0
files_pr = [os.path.basename(dirfiles_pr[i][0]) for i in range(len(dirfiles_pr))]
files_root = [os.path.basename(dirfiles_root[i][0]) for i in range(len(dirfiles_root))]

PLUS = "++ "
MINUS = "-- "
for filepath in set(dirfiles_root.keys()) | set(dirfiles_pr.keys()):
dirfiles_root[filepath] = dirfiles_root.get(filepath, 0)
dirfiles_pr[filepath] = dirfiles_pr.get(filepath, 0)

for filepath, n in dirfiles_pr:
for filepath_root, _ in dirfiles_root:
if os.path.basename(dirfiles_pr[ii][0]) == os.path.basename(filepath_root):
diff_count = n - dirfiles_root[ii][1]
mycolor = "red" if diff_count > 0 else "green"
mysymbol = PLUS if diff_count > 0 else MINUS
table.add_row(f"{os.path.basename(filepath)}", f"[bold cyan]{n}[/bold cyan]", f"[bold cyan]{dirfiles_root[ii][1]}[/bold cyan]", mysymbol, f"[bold {mycolor}]{diff_count}[/bold {mycolor}]")
PLUS = "++ "
MINUS = "-- "

if files_pr[ii] not in files_root:
table.add_row(f"{os.path.basename(files_pr[ii])}", "----", f"[bold green]{n}[/bold green]", PLUS, f"[bold green]{n}[/bold green]")
ii += 1
diff_count = dirfiles_pr[filepath] - dirfiles_root[filepath]
mycolor = "red" if diff_count > 0 else "green"
mysymbol = PLUS if diff_count > 0 else MINUS
table.add_row(os.path.basename(filepath),
f"[bold cyan]{dirfiles_root[filepath]}[/bold cyan]",
f"[bold cyan]{dirfiles_pr[filepath]}[/bold cyan]",
mysymbol,
f"[bold {mycolor}]{diff_count}[/bold {mycolor}]")

total += dircount
total += dircount_root

cons.raw.print(table)

Expand Down

0 comments on commit 0092aeb

Please sign in to comment.