From 93d77c148ff5e537156fe32d121ed6b6c2078227 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 | 50 +++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 6 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..7048be5 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,53 @@ 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. - print(f"Processing COMF metric with threshold >{threshold} for files: {', '.join(files)}") + Args: + files: A list of file paths to check ratio of comments to statements. + threshold: The minimum ratio allowed. - return 0 + Returns: + The number of files that don't comply with comment ratio threshold. + """ + + metric_fail = 0 + stmt_tool = "pmccabe -c " + stmt_index = 2 + total_stmts = 0 + cmnt_tool = "pygount --format=cloc-xml " + cmnt_index = 21 + cmnt_density = 0 + license_cmnt_size = 4 + + 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(stmt_tool + 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(cmnt_tool + str(file)).read().split(' ') + + if lines[cmnt_index].startswith("comment"): + cmnts = int(lines[cmnt_index].split("=")[1].replace('"', '')) + cmnts = cmnts - license_cmnt_size + comment_density = cmnts/total_stmts + if comment_density < threshold: + print("At " + file + " comment density is " + comment_density) + metric_fail += 1 + total_stmts = 0 + + return metric_fail def process_goto(files, threshold): """