From 82ffe41c784bd69675f53da2d74e9535a4e10bb9 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 | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/his_checker.py b/his_checker.py index ecd4eca..f326638 100755 --- a/his_checker.py +++ b/his_checker.py @@ -9,6 +9,8 @@ import sys import argparse +import os +import re def process_calling(files, threshold): """ @@ -57,13 +59,47 @@ def process_level(files, threshold): def process_param(files, threshold): """ - Process the parameters metric - """ - - print(f"Processing PARAMETERS metric with threshold [0-{threshold}] for files: \ - {', '.join(files)}") - - return 0 + Process number of parameters in a function. This function checks each function in the provided + files for the number of parameters. If the number of parameters in a function exceeds the + defined threshold, an error message is printed and the error count is incremented. + + Args: + files: A list of file paths to check for the number of parameters. + threshold: The maximum allowed number of parameters. + + Returns: + The number of files that exceed the parameters threshold. + """ + + metric_fail = 0 + cflow = "cflow -l" + + print("--------------------------------------------") + print(f"Processing PARAM metric with threshold >{threshold}") + print("--------------------------------------------") + + # Process each file + for file in 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 > threshold: + print(f"At {file_path}({line_number}): {function_name} has " + f"{num_param} function parameters.") + metric_fail += 1 + + print("--------------------------------------------") + if metric_fail: + print("PARAM metric failed with " + str(metric_fail) + " error(s)\n") + else: + print("PARAM metric passed") + + return metric_fail def process_path(files, threshold): """