From 6a8f110a78140bc75ef7585ffc2f4bd33cae88ce Mon Sep 17 00:00:00 2001 From: AfonsoSantos96 Date: Wed, 22 Nov 2023 11:41:47 +0000 Subject: [PATCH] feat(HIS): added checker of a file comment density Signed-off-by: Afonso Santos --- docker/Dockerfile | 6 ++++-- his_checker.py | 46 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 9fe1a6d..5f47e88 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -38,7 +38,8 @@ RUN apt-get update && apt-get install -y \ tree \ vim \ nano \ - device-tree-compiler + device-tree-compiler \ + pmccabe # Install python packages RUN pip3 install \ @@ -56,7 +57,8 @@ RUN pip3 install \ pyspellchecker \ ROPgadget \ capstone \ - GitPython + GitPython \ + pygount # Install javascript packages RUN npm install -g cspell@latest diff --git a/his_checker.py b/his_checker.py index 9f2a17d..9eb41fc 100755 --- a/his_checker.py +++ b/his_checker.py @@ -9,6 +9,7 @@ import sys import argparse +import os def process_calling(files, threshold): """ @@ -30,12 +31,51 @@ def process_calls(files, threshold): def process_comf(files, threshold): """ - Process the comf metric + Process the relationship of the number of comments (outside of and within functions) to the + number of statements. Statements are counted using the pmmcabe tool, while the comments are + counted using pygount. + + Args: + files: A list of file paths to check ratio of comments to statements. + threshold: The minimum ratio allowed. + + Returns: + The number of files that don't comply with comment ratio threshold. """ - print(f"Processing COMF metric with threshold >{threshold} for files: {', '.join(files)}") + metric_fail = 0 + total_stmts = 0 + stmt_index = 2 + cmnt_index = 21 + license_cmnt_size = 4 - return 0 + print("--------------------------------------------") + print(f"Processing COMF metric with threshold >{threshold}") + print("--------------------------------------------") + + # Process each file + for file in files: + # Run 'pmccabe' on the file and split the output into lines + sline = os.popen("pmccabe -c " + str(file)).read().split('\n') + + for fields in [line.split('\t') for line in sline[:-1]]: + stmts = int(fields[stmt_index]) + total_stmts = total_stmts + stmts + + # Run 'pygount' on the file and split the output into lines + lines = os.popen("pygount --format=cloc-xml " + str(file)).read().split(' ') + + if lines[cmnt_index].startswith("comment"): + cmnts = int(lines[cmnt_index].split("=")[1].replace('"', '')) + + if total_stmts != 0: + cmnt_density = (cmnts - license_cmnt_size)/total_stmts + if cmnt_density < threshold: + print(f"At {file} comment density is {cmnt_density:.2f}") + metric_fail += 1 + total_stmts = 0 + + return metric_fail def process_goto(files, threshold): """