From 8c1f1735b28bc6bf3f4977117c1fa47aa1933b80 Mon Sep 17 00:00:00 2001 From: AfonsoSantos96 Date: Tue, 7 Nov 2023 12:00:45 +0000 Subject: [PATCH] feat(HIS): add number of function parameters check to script Signed-off-by: Afonso Santos --- his_checker.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/his_checker.py b/his_checker.py index 2ec03e9..388770e 100755 --- a/his_checker.py +++ b/his_checker.py @@ -9,9 +9,37 @@ import sys import argparse +import os +import re + +PARAMETERS_THRESHOLD = 5 + +def process_parameters(files): + + """Process the number of function parameters.""" + + metric_fail = 0 + cflow = "cflow -l --depth=1" + print("Checking the number of function parameters") + for file in files.files: + with os.popen(f"{cflow} {file}") as pipe: + lines = pipe.read() + pattern = r'\{.*?\} (\w+)\(.*?\(([^)]*)\) at ([^:]+):(\d+)' + matches = re.findall(pattern, lines) + for function_name, parameters, file_path, line_number in matches: + parameters = \ + [param.strip() for param in parameters.split(',') if param.strip()] + num_param = len(parameters) + if num_param > PARAMETERS_THRESHOLD: + print(f"At {file_path}({line_number}): {function_name} has " + f"{num_param} function parameters. The maximum are " + f"{PARAMETERS_THRESHOLD} parameters.") + metric_fail += 1 + print(f"Check done with {metric_fail} error(s)\n") + return metric_fail if __name__ == "__main__": - METRICS_LIST = [] + METRICS_LIST = [process_parameters] CHECK_FAIL = 0 PARSER = argparse.ArgumentParser() PARSER.add_argument("files", nargs="+", help="The files to process")