Skip to content

Commit 0b05ff3

Browse files
Improve count with intent to add to CI (#281)
Co-authored-by: Henry LE BERRE <[email protected]>
1 parent 334046b commit 0b05ff3

File tree

6 files changed

+127
-8
lines changed

6 files changed

+127
-8
lines changed

.github/workflows/count.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Check Line Counts
2+
on: [push]
3+
4+
jobs:
5+
sz:
6+
name: Core MFC Line Difference
7+
permissions:
8+
contents: read
9+
pull-requests: write
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code from PR branch
13+
uses: actions/checkout@v4
14+
with:
15+
path: pr
16+
17+
- name: Checkout code from MFC master
18+
uses: actions/checkout@v4
19+
with:
20+
repository: ${{ github.event.pull_request.repository }}
21+
ref: ${{ github.event.pull_request.base.ref }}
22+
path: base
23+
# repository: MFlowCode/MFC
24+
# ref: master
25+
# path: base
26+
27+
- name: Setup MFCs
28+
run: |
29+
BASE="$GITHUB_WORKSPACE/base"
30+
PR="$GITHUB_WORKSPACE/pr"
31+
cd $BASE
32+
./mfc.sh count
33+
cd $PR
34+
./mfc.sh count
35+
36+
- name: Counting Lines
37+
run: |
38+
BASE="$GITHUB_WORKSPACE/base"
39+
PR="$GITHUB_WORKSPACE/pr"
40+
cd $BASE
41+
echo "___BASE______________"
42+
./mfc.sh count
43+
cd $PR
44+
echo "___PR________________"
45+
./mfc.sh count
46+
47+
# - name: Get Line Diff
48+
# run: |
49+
# BASE="$GITHUB_WORKSPACE/base"
50+
# PR="$GITHUB_WORKSPACE/pr"
51+
# cd $BASE
52+
# export MFC_PR=$PR
53+
# pwd
54+
# ./mfc.sh count_diff
55+

.github/workflows/docker.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@ jobs:
2727
file: toolchain/Dockerfile
2828
push: true
2929
tags: ${{ secrets.DOCKER_USERNAME }}/mfc:latest
30-

.github/workflows/spelling.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ jobs:
1212
uses: actions/checkout@v3
1313

1414
- name: Spell Check
15-
uses: crate-ci/typos@master
15+
uses: crate-ci/typos@master

toolchain/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __checks():
4949
def __run():
5050
{"test": test.test, "run": run.run, "build": build.build,
5151
"clean": build.clean, "bench": bench.bench, "count": count.count,
52-
"packer": packer.packer
52+
"packer": packer.packer, "count_diff": count.count_diff
5353
}[ARG("command")]()
5454

5555

toolchain/mfc/args.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def parse(config):
2424
clean = parsers.add_parser(name="clean", help="Clean build artifacts.", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
2525
bench = parsers.add_parser(name="bench", help="Benchmark MFC (for CI).", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
2626
count = parsers.add_parser(name="count", help="Count LOC in MFC.", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
27+
count_diff = parsers.add_parser(name="count_diff", help="Count LOC in MFC.", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
2728
packer = parsers.add_parser(name="packer", help="Packer utility (pack/unpack/compare)", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
2829

2930
packers = packer.add_subparsers(dest="packer")
@@ -126,12 +127,16 @@ def add_common_arguments(p, mask = None):
126127
# === COUNT ===
127128
add_common_arguments(count, "g")
128129

130+
# === COUNT ===
131+
add_common_arguments(count_diff, "g")
132+
129133
args: dict = vars(parser.parse_args())
130134
args["--"] = args.get("--", [])
131135

132136
# Add default arguments of other subparsers
133137
for name, parser in [("run", run), ("test", test), ("build", build),
134-
("clean", clean), ("bench", bench), ("count", count)]:
138+
("clean", clean), ("bench", bench), ("count", count),
139+
("count_diff", count_diff)]:
135140
if args["command"] == name:
136141
continue
137142

toolchain/mfc/count.py

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import rich.table
33

44
from .state import ARG
5-
from .common import MFC_ROOTDIR, format_list_to_string
5+
from .common import MFC_ROOTDIR, format_list_to_string, MFCException
66
from .printer import cons
77

88
def handle_dir(dirpath: str) -> typing.Tuple[typing.List[typing.Tuple[str, int]], int]:
@@ -11,9 +11,18 @@ def handle_dir(dirpath: str) -> typing.Tuple[typing.List[typing.Tuple[str, int]]
1111

1212
for filepath in glob.glob(os.path.join(dirpath, '*.*f*')):
1313
with open(filepath) as f:
14-
n = sum(1 if not l.isspace() else 0 for l in f.read().split('\n'))
15-
files.append((filepath, count))
16-
total += n
14+
counter = 0
15+
for l in f.read().split('\n'):
16+
# Skip whitespace
17+
if l.isspace() or len(l) == 0:
18+
continue
19+
# Skip comments but not !$acc ones!
20+
if l.lstrip().startswith("!") and not l.lstrip().startswith("!$acc"):
21+
continue
22+
counter += 1
23+
24+
files.append((filepath, counter))
25+
total += counter
1726

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

@@ -42,3 +51,54 @@ def count():
4251
cons.print(f"[bold]Total {target_str_list} lines: [bold cyan]{total}[/bold cyan].[/bold]")
4352
cons.print()
4453
cons.unindent()
54+
55+
# pylint: disable=too-many-locals
56+
def count_diff():
57+
target_str_list = format_list_to_string(ARG('targets'), 'magenta')
58+
cons.print(f"[bold]Counting lines of code in {target_str_list}[/bold] (excluding whitespace lines)")
59+
cons.indent()
60+
61+
total = 0
62+
MFC_COMPAREDIR=os.getenv('MFC_PR')
63+
if MFC_COMPAREDIR is None:
64+
raise MFCException("MFC_PR is not in your environment.")
65+
66+
print('compare dir', MFC_COMPAREDIR)
67+
68+
# MFC_COMPAREDIR="/Users/spencer/Downloads/MFC-shbfork"
69+
for codedir in ['common'] + ARG("targets"):
70+
dirfiles_root, dircount = handle_dir(os.path.join(MFC_ROOTDIR, 'src', codedir))
71+
dirfiles_pr, dircount_pr = handle_dir(os.path.join(MFC_COMPAREDIR, 'src', codedir))
72+
table = rich.table.Table(show_header=True, box=rich.table.box.SIMPLE)
73+
table.add_column(f"File (in [magenta]{codedir}[/magenta])", justify="left")
74+
table.add_column(f"Lines [HEAD] ([cyan]{dircount}[/cyan])", justify="right")
75+
table.add_column(f"Lines [PR] ([cyan]{dircount_pr}[/cyan])", justify="right")
76+
table.add_column("", justify="right")
77+
table.add_column("Diff", justify="right")
78+
79+
ii = 0
80+
files_pr = [os.path.basename(dirfiles_pr[i][0]) for i in range(len(dirfiles_pr))]
81+
files_root = [os.path.basename(dirfiles_root[i][0]) for i in range(len(dirfiles_root))]
82+
83+
PLUS = "++ "
84+
MINUS = "-- "
85+
86+
for filepath, n in dirfiles_pr:
87+
for filepath_root, _ in dirfiles_root:
88+
if os.path.basename(dirfiles_pr[ii][0]) == os.path.basename(filepath_root):
89+
diff_count = n - dirfiles_root[ii][1]
90+
mycolor = "red" if diff_count > 0 else "green"
91+
mysymbol = PLUS if diff_count > 0 else MINUS
92+
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}]")
93+
94+
if files_pr[ii] not in files_root:
95+
table.add_row(f"{os.path.basename(files_pr[ii])}", "----", f"[bold green]{n}[/bold green]", PLUS, f"[bold green]{n}[/bold green]")
96+
ii += 1
97+
98+
total += dircount
99+
100+
cons.raw.print(table)
101+
102+
cons.print(f"[bold]Total {target_str_list} lines: [bold cyan]{total}[/bold cyan].[/bold]")
103+
cons.print()
104+
cons.unindent()

0 commit comments

Comments
 (0)