From ffdbe146a2050ae2d6f8032e5734a595b7a80fff Mon Sep 17 00:00:00 2001 From: AfonsoSantos96 Date: Thu, 10 Aug 2023 23:23:19 +0100 Subject: [PATCH] feat(HIS): add cyclomatic complexity metric check to script Signed-off-by: Afonso Santos --- docker/Dockerfile | 1 + his_checker.py | 31 +++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 530ebf4..5c1b94e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -27,6 +27,7 @@ RUN apt-get update && apt-get install -y \ clang-tidy-$CLANG_VERSION \ nodejs \ npm \ + pmccabe \ enchant-2 && \ pip3 install gitlint && \ pip3 install license-expression && \ diff --git a/his_checker.py b/his_checker.py index d02582c..d4f539b 100755 --- a/his_checker.py +++ b/his_checker.py @@ -9,22 +9,37 @@ import sys import argparse +import os -METRIC_FAIL = 0 +CYCLOMATIC_THRESHOLD = 10 -def process_metric(filenames): +def process_complexity(files): - """Process each file.""" - print("Checking X metric") - for file in filenames.files: - print(file) + """Process McCabe Cyclomatic Complexity for each function in a file""" + + metric_fail = 0 + cc_tool = "pmccabe -c " + print("== Checking McCabe Cyclomatic Complexity metric ==") + for file in files.files: + sline = os.popen(cc_tool + str(file)).read().split('\n') + for fields in [line.split('\t') for line in sline[:-1]]: + if int(fields[0]) > CYCLOMATIC_THRESHOLD: + fields[5] = fields[5][fields[5].find("src"):] + print("At " + fields[5] + " has a complexity of "+ fields[0] + + ". The maximum is "+str(CYCLOMATIC_THRESHOLD)) + metric_fail += 1 + print("== Check done with " + str(metric_fail) + " error(s) ==\n") + return metric_fail if __name__ == "__main__": + METRICS_LIST = [process_complexity] + CHECK_FAIL = 0 PARSER = argparse.ArgumentParser() PARSER.add_argument("files", nargs="+", help="The files to process") ARGS = PARSER.parse_args() - process_metric(ARGS) + for metric in METRICS_LIST: + CHECK_FAIL += metric(ARGS) - if METRIC_FAIL: + if CHECK_FAIL: sys.exit(-1)