Skip to content

Commit 9f25a4f

Browse files
committed
Prettify, skip whitespace, and non-unix support
1 parent 854ce34 commit 9f25a4f

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

toolchain/mfc/count.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
1-
import os, json, time, typing, datetime, subprocess
2-
3-
import rich.table
1+
import os, glob, typing, typing
42

3+
from .common import MFC_ROOTDIR
54
from .printer import cons
6-
from .state import ARG
7-
from .build import build_targets
8-
from .common import system, MFC_SUBDIR
9-
from . import sched
105

11-
import glob
6+
import rich.table
127

13-
def count():
14-
15-
cons.print("[bold]Counting lines of code in [magenta]MFC[/magenta][/bold] (including blank ones)")
16-
cons.indent()
17-
18-
dirs = ['common', 'pre_process', 'simulation', 'post_process']
19-
for mydir in dirs:
20-
cons.print(f"[bold]In [magenta]{mydir}[/magenta]:[/bold]")
21-
dir_path = f'./src/{mydir}/*.*f*'
22-
test_list = glob.glob(dir_path)
23-
result = str(' '.join(test_list))
24-
os.system("wc -l " + result + " | sort | sed \$d")
25-
os.system("wc -l " + result + " | sort | tail -n 1")
26-
cons.print()
8+
def handle_dir(dirpath: str) -> typing.Tuple[typing.List[typing.Tuple[str, int]], int]:
9+
files = []
10+
total = 0
2711

28-
cons.print("[bold]Total MFC lines:[/bold]")
29-
dir_path = f'./src/*/*.*f*'
30-
test_list = glob.glob(dir_path)
31-
result = str(' '.join(test_list))
32-
os.system("wc -l " + result + " | sort | tail -n 1")
12+
for filepath in glob.glob(os.path.join(dirpath, '*.*f*')):
13+
with open(filepath) as f:
14+
count = sum(1 if not l.isspace() else 0 for l in f.read().split('\n'))
15+
files.append((filepath, count))
16+
total += count
3317

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

20+
return (files, total)
3521

22+
def count():
23+
cons.print("[bold]Counting lines of code in [magenta]MFC[/magenta][/bold] (excluding whitespace lines)")
24+
cons.print()
25+
cons.indent()
26+
27+
total = 0
28+
for codedir in ['common', 'pre_process', 'simulation', 'post_process']:
29+
dirfiles, dircount = handle_dir(os.path.join(MFC_ROOTDIR, 'src', codedir))
30+
table = rich.table.Table(show_header=True, box=rich.table.box.SIMPLE)
31+
table.add_column(f"File (in [magenta]{codedir}[/magenta])", justify="left")
32+
table.add_column(f"Lines ([cyan]{dircount}[/cyan])", justify="right")
33+
34+
for filepath, count in dirfiles:
35+
table.add_row(f"{os.path.basename(filepath)}", f"[bold cyan]{count}[/bold cyan]")
36+
37+
total += dircount
38+
39+
cons.raw.print(table)
40+
41+
cons.print(f"[bold]Total MFC lines: [bold cyan]{total}[/bold cyan].[/bold]")
42+
cons.print()
43+
cons.unindent()
3644

0 commit comments

Comments
 (0)