From da904bea5b426ad59eee765216bff5a86c7ddf0f 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 | 36 +++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 9 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..5439556 100755 --- a/his_checker.py +++ b/his_checker.py @@ -29,13 +29,35 @@ def process_calls(files, threshold): return 0 def process_comf(files, threshold): - """ - Process the comf metric - """ - - print(f"Processing COMF metric with threshold >{threshold} for files: {', '.join(files)}") - - return 0 + """Process comment density within a file""" + + metric_fail = 0 + statements_tool = "pmccabe -c " + pmccabe_stat_index = 2 + total_statements = 0 + comments_tool = "pygount --format=cloc-xml " + comment_index = 21 + comment_density = 0 + license_comment_size = 4 + print("Checking the file comment density") + for file in files.files: + sline = os.popen(statements_tool + str(file)).read().split('\n') + for fields in [line.split('\t') for line in sline[:-1]]: + statements = int(fields[pmccabe_stat_index]) + total_statements = total_statements + statements + lines = os.popen(comments_tool + str(file)).read().split(' ') + if lines[comment_index].startswith("comment"): + comments = int(lines[comment_index].split("=")[1].replace('"', '')) + comments = comments - license_comment_size + comment_density = comments/total_statements + if comment_density < COMMENTS_THRESHOLD: + print(f"The file {file}: has a comment density (comments/" + f"statements) of {comment_density}. The minimum is a " + f"density of {COMMENTS_THRESHOLD}") + metric_fail += 1 + total_statements = 0 + print("Check done with " + str(metric_fail) + " error(s)\n") + return metric_fail def process_goto(files, threshold): """