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 7, 2024
1 parent 2a2ac26 commit 6a8f110
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 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
46 changes: 43 additions & 3 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,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):
"""
Expand Down

0 comments on commit 6a8f110

Please sign in to comment.