Skip to content

Commit

Permalink
feat(HIS): added checker of a file comment density
Browse files Browse the repository at this point in the history
Signed-off-by: Afonso Santos <[email protected]>
  • Loading branch information
AfonsoSantos96 authored and danielRep committed Mar 6, 2024
1 parent 2a2ac26 commit 93d77c1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
6 changes: 4 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -56,7 +57,8 @@ RUN pip3 install \
pyspellchecker \
ROPgadget \
capstone \
GitPython
GitPython \
pygount

# Install javascript packages
RUN npm install -g cspell@latest
Expand Down
50 changes: 46 additions & 4 deletions his_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import sys
import argparse
import os

def process_calling(files, threshold):
"""
Expand All @@ -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):
"""
Expand Down

0 comments on commit 93d77c1

Please sign in to comment.