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 2ec03e9..0a85427 100755 --- a/his_checker.py +++ b/his_checker.py @@ -9,9 +9,34 @@ import sys import argparse +import os + +CYCLOMATIC_THRESHOLD = 10 + +def process_complexity(files): + + """Process McCabe Cyclomatic Complexity for each function in a file""" + + metric_fail = 0 + pmccabe_cc_index = 0 + function_index = 5 + 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]]: + cyc_cmplx = int(fields[pmccabe_cc_index]) + if cyc_cmplx > CYCLOMATIC_THRESHOLD: + function_name = fields[function_index] + print(function_name + " function has a complexity of " + + str(cyc_cmplx) + ". 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 = [] + METRICS_LIST = [process_complexity] CHECK_FAIL = 0 PARSER = argparse.ArgumentParser() PARSER.add_argument("files", nargs="+", help="The files to process")