|
| 1 | +from dataclasses import dataclass |
| 2 | +from pathlib import Path |
| 3 | +import re |
| 4 | +import yaml |
| 5 | + |
| 6 | + |
| 7 | +@dataclass |
| 8 | +class Metadata: |
| 9 | + py_script_path: str = None |
| 10 | + run: str = None |
| 11 | + app: str = None |
| 12 | + factoryreset: bool = False |
| 13 | + comissioning_method: str = None |
| 14 | + discriminator: int = None |
| 15 | + kvs: str = None |
| 16 | + storage_path: str = None |
| 17 | + on_network_commission: str = None |
| 18 | + passcode: int = None |
| 19 | + trace_to: str = None |
| 20 | + hex_arg: str = None |
| 21 | + endpoint: str = None |
| 22 | + manual_code: str = None |
| 23 | + bool_arg: str = None |
| 24 | + PICS: str = None |
| 25 | + tests: str = None |
| 26 | + int_arg: str = None |
| 27 | + trace_to_appjson: str = None |
| 28 | + trace_to_testjson: str = None |
| 29 | + trace_toperfetto: str = None |
| 30 | + |
| 31 | + def print(self): |
| 32 | + print(repr(self)) |
| 33 | + |
| 34 | + |
| 35 | +class Metadata_Reader: |
| 36 | + # This class will be initialized with |
| 37 | + # the name of the script folder and the environment |
| 38 | + # file name |
| 39 | + |
| 40 | + def __init__(self, script_folder, env_file_name): |
| 41 | + self.script_folder = script_folder |
| 42 | + self.env = self.__build_env_object__(env_file_name) |
| 43 | + |
| 44 | + |
| 45 | + # builds the environment object |
| 46 | + def __build_env_object__(self, env_file_name): |
| 47 | + |
| 48 | + with open(env_file_name) as stream: |
| 49 | + try: |
| 50 | + env = yaml.safe_load(stream) |
| 51 | + except yaml.YAMLError as yaml_exception: |
| 52 | + raise yaml_exception |
| 53 | + |
| 54 | + return env |
| 55 | + |
| 56 | + # resolves the run arguments associated with the environment |
| 57 | + def __resolve_env_vals__(self, metadata_dict): |
| 58 | + |
| 59 | + for run_arg in metadata_dict: |
| 60 | + |
| 61 | + run_arg_val = metadata_dict[run_arg] |
| 62 | + |
| 63 | + if not type(run_arg_val)==str or run_arg=="run": |
| 64 | + metadata_dict[run_arg]=run_arg_val |
| 65 | + continue |
| 66 | + |
| 67 | + if run_arg_val == None: |
| 68 | + continue |
| 69 | + |
| 70 | + sub_args = run_arg_val.split('/') |
| 71 | + |
| 72 | + if len(sub_args)==1: |
| 73 | + run_arg_val=self.env.get(sub_args[0]) |
| 74 | + |
| 75 | + elif len(sub_args)==2: |
| 76 | + run_arg_val=self.env.get(sub_args[0]).get(sub_args[1]) |
| 77 | + |
| 78 | + # if a argument has been specified in the comment header |
| 79 | + # but can't be found in the env file, consider it to be |
| 80 | + # boolean value. |
| 81 | + if run_arg_val == None: |
| 82 | + run_arg_val = True |
| 83 | + |
| 84 | + if not self.__is_run_arg_valid__(run_arg_val): |
| 85 | + raise Exception(str(run_arg_val)+" is not a valid value for "+str(run_arg)) |
| 86 | + |
| 87 | + metadata_dict[run_arg] = run_arg_val |
| 88 | + |
| 89 | + |
| 90 | + # determines if the defined run arguments are valid |
| 91 | + def __is_run_arg_valid__(self, run_arg_val): |
| 92 | + return True |
| 93 | + |
| 94 | + |
| 95 | + # reads the test script file and parses out the run arguments defined in the file |
| 96 | + def __parse_script__(self, py_script_path, runs_metadata): |
| 97 | + runs_def_ptrn=re.compile(r'^\s*#\s*test-runner-runs:\s*(.*)$') |
| 98 | + args_def_ptrn=re.compile(r'^\s*#\s*test-runner-run/([a-zA-Z0-9_]+)/(script|app):\s*(.*)$') |
| 99 | + |
| 100 | + runs_arg_lines = {} |
| 101 | + |
| 102 | + |
| 103 | + with open(py_script_path, 'r', encoding='utf8') as py_script: |
| 104 | + for line in py_script.readlines(): |
| 105 | + |
| 106 | + runs_match = runs_def_ptrn.match(line.strip()) |
| 107 | + args_match = args_def_ptrn.match(line.strip()) |
| 108 | + |
| 109 | + if runs_match: |
| 110 | + for run in runs_match.group(1).strip().split(): |
| 111 | + runs_arg_lines[run]=[] |
| 112 | + |
| 113 | + elif args_match: |
| 114 | + runs_arg_lines[args_match.group(1)].append(args_match.group(3)) |
| 115 | + |
| 116 | + for run in runs_arg_lines: |
| 117 | + metadata = Metadata() |
| 118 | + metadata_dict = vars(metadata) |
| 119 | + metadata_dict['py_script_path'] = str(py_script_path) |
| 120 | + metadata_dict['run'] = str(run) |
| 121 | + self.read_args(runs_arg_lines[run], metadata_dict) |
| 122 | + self.__resolve_env_vals__(metadata_dict) |
| 123 | + runs_metadata[str(metadata.py_script_path)+"+"+str(metadata.run)] = metadata |
| 124 | + |
| 125 | + |
| 126 | + # gets the run metadata associated with all the test scripts in a particular folder |
| 127 | + def get_runs_metadata(self): |
| 128 | + runs_metadata = {} |
| 129 | + for path in Path(self.script_folder).glob('*.py'): |
| 130 | + self.__parse_script__(path, runs_metadata) |
| 131 | + |
| 132 | + return runs_metadata |
| 133 | + |
| 134 | + |
| 135 | + # goes through run argument definition and extracts run arguments from it |
| 136 | + def read_args(self,run_args_lines,metadata_dict): |
| 137 | + for run_line in run_args_lines: |
| 138 | + for run_arg_word in run_line.strip().split(): |
| 139 | + run_arg=run_arg_word.split('/',1)[0] |
| 140 | + if run_arg in metadata_dict: |
| 141 | + metadata_dict[run_arg] = run_arg_word |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
0 commit comments