|
| 1 | +#!/usr/bin/python3 |
| 2 | +# Copyright (c) 2024 Project CHIP Authors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import re |
| 17 | +from dataclasses import dataclass |
| 18 | +from typing import Dict, List, Optional |
| 19 | + |
| 20 | +import yaml |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class Metadata: |
| 25 | + py_script_path: Optional[str] = None |
| 26 | + run: Optional[str] = None |
| 27 | + app: Optional[str] = None |
| 28 | + discriminator: Optional[str] = None |
| 29 | + passcode: Optional[str] = None |
| 30 | + |
| 31 | + def copy_from_dict(self, attr_dict: Dict[str, str]) -> None: |
| 32 | + """ |
| 33 | + Sets the value of the attributes from a dictionary. |
| 34 | +
|
| 35 | + Attributes: |
| 36 | +
|
| 37 | + attr_dict: |
| 38 | + Dictionary that stores attributes value that should |
| 39 | + be transferred to this class. |
| 40 | + """ |
| 41 | + |
| 42 | + if "app" in attr_dict: |
| 43 | + self.app = attr_dict["app"] |
| 44 | + |
| 45 | + if "run" in attr_dict: |
| 46 | + self.run = attr_dict["run"] |
| 47 | + |
| 48 | + if "discriminator" in attr_dict: |
| 49 | + self.discriminator = attr_dict["discriminator"] |
| 50 | + |
| 51 | + if "passcode" in attr_dict: |
| 52 | + self.passcode = attr_dict["passcode"] |
| 53 | + |
| 54 | + if "py_script_path" in attr_dict: |
| 55 | + self.py_script_path = attr_dict["py_script_path"] |
| 56 | + |
| 57 | + # TODO - set other attributes as well |
| 58 | + |
| 59 | + |
| 60 | +class MetadataReader: |
| 61 | + """ |
| 62 | + A class to parse run arguments from the test scripts and |
| 63 | + resolve them to environment specific values. |
| 64 | + """ |
| 65 | + |
| 66 | + def __init__(self, env_yaml_file_path: str): |
| 67 | + """ |
| 68 | + Reads the YAML file and Constructs the environment object |
| 69 | +
|
| 70 | + Parameters: |
| 71 | +
|
| 72 | + env_yaml_file_path: |
| 73 | + Path to the environment file that contains the YAML configuration. |
| 74 | + """ |
| 75 | + with open(env_yaml_file_path) as stream: |
| 76 | + self.env = yaml.safe_load(stream) |
| 77 | + |
| 78 | + def __resolve_env_vals__(self, metadata_dict: Dict[str, str]) -> None: |
| 79 | + """ |
| 80 | + Resolves the argument defined in the test script to environment values. |
| 81 | + For example, if a test script defines "all_clusters" as the value for app |
| 82 | + name, we will check the environment configuration to see what raw value is |
| 83 | + assocaited with the "all_cluster" variable and set the value for "app" option |
| 84 | + to this raw value. |
| 85 | +
|
| 86 | + Parameter: |
| 87 | +
|
| 88 | + metadata_dict: |
| 89 | + Dictionary where each key represent a particular argument and its value represent |
| 90 | + the value for that argument defined in the test script. |
| 91 | + """ |
| 92 | + |
| 93 | + for run_arg, run_arg_val in metadata_dict.items(): |
| 94 | + |
| 95 | + if not type(run_arg_val) == str or run_arg == "run": |
| 96 | + metadata_dict[run_arg] = run_arg_val |
| 97 | + continue |
| 98 | + |
| 99 | + if run_arg_val is None: |
| 100 | + continue |
| 101 | + |
| 102 | + sub_args = run_arg_val.split('/') |
| 103 | + |
| 104 | + if len(sub_args) not in [1, 2]: |
| 105 | + err = """The argument is not in the correct format. |
| 106 | + The argument must follow the format of arg1 or arg1/arg2. |
| 107 | + For example, arg1 represents the argument type and optionally arg2 |
| 108 | + represents a specific variable defined in the environment file whose |
| 109 | + value should be used as the argument value. If arg2 is not specified, |
| 110 | + we will just use the first value associated with arg1 in the environment file.""" |
| 111 | + raise Exception(err) |
| 112 | + |
| 113 | + if len(sub_args) == 1: |
| 114 | + run_arg_val = self.env.get(sub_args[0]) |
| 115 | + |
| 116 | + elif len(sub_args) == 2: |
| 117 | + run_arg_val = self.env.get(sub_args[0]).get(sub_args[1]) |
| 118 | + |
| 119 | + # if a argument has been specified in the comment header |
| 120 | + # but can't be found in the env file, consider it to be |
| 121 | + # boolean value. |
| 122 | + if run_arg_val is None: |
| 123 | + run_arg_val = True |
| 124 | + |
| 125 | + metadata_dict[run_arg] = run_arg_val |
| 126 | + |
| 127 | + def __read_args__(self, run_args_lines: List[str]) -> Dict[str, str]: |
| 128 | + """ |
| 129 | + Parses a list of lines and extracts argument |
| 130 | + values from it. |
| 131 | +
|
| 132 | + Parameters: |
| 133 | +
|
| 134 | + run_args_lines: |
| 135 | + Line in test script header that contains run argument definition. |
| 136 | + Each line will contain a list of run arguments separated by a space. |
| 137 | + Line below is one example of what the run argument line will look like: |
| 138 | + "app/all-clusters discriminator KVS storage-path" |
| 139 | +
|
| 140 | + In this case the line defines that app, discriminator, KVS, and storage-path |
| 141 | + are the arguments that should be used with this run. |
| 142 | +
|
| 143 | + An argument can be defined multiple times in the same line or in different lines. |
| 144 | + The last definition will override any previous definition. For example, |
| 145 | + "KVS/kvs1 KVS/kvs2 KVS/kvs3" line will lead to KVS value of kvs3. |
| 146 | + """ |
| 147 | + metadata_dict = {} |
| 148 | + |
| 149 | + for run_line in run_args_lines: |
| 150 | + for run_arg_word in run_line.strip().split(): |
| 151 | + ''' |
| 152 | + We expect the run arg to be defined in one of the |
| 153 | + following two formats: |
| 154 | + 1. run_arg |
| 155 | + 2. run_arg/run_arg_val |
| 156 | +
|
| 157 | + Examples: "discriminator" and "app/all_clusters" |
| 158 | +
|
| 159 | + ''' |
| 160 | + run_arg = run_arg_word.split('/', 1)[0] |
| 161 | + metadata_dict[run_arg] = run_arg_word |
| 162 | + |
| 163 | + return metadata_dict |
| 164 | + |
| 165 | + def parse_script(self, py_script_path: str) -> List[Metadata]: |
| 166 | + """ |
| 167 | + Parses a script and returns a list of metadata object where |
| 168 | + each element of that list representing run arguments associated |
| 169 | + with a particular run. |
| 170 | +
|
| 171 | + Parameter: |
| 172 | +
|
| 173 | + py_script_path: |
| 174 | + path to the python test script |
| 175 | +
|
| 176 | + Return: |
| 177 | +
|
| 178 | + List[Metadata] |
| 179 | + List of Metadata object where each Metadata element represents |
| 180 | + the run arguments associated with a particular run defined in |
| 181 | + the script file. |
| 182 | + """ |
| 183 | + |
| 184 | + runs_def_ptrn = re.compile(r'^\s*#\s*test-runner-runs:\s*(.*)$') |
| 185 | + args_def_ptrn = re.compile(r'^\s*#\s*test-runner-run/([a-zA-Z0-9_]+):\s*(.*)$') |
| 186 | + |
| 187 | + runs_arg_lines: Dict[str, List[str]] = {} |
| 188 | + runs_metadata = [] |
| 189 | + |
| 190 | + with open(py_script_path, 'r', encoding='utf8') as py_script: |
| 191 | + for line in py_script.readlines(): |
| 192 | + |
| 193 | + runs_match = runs_def_ptrn.match(line.strip()) |
| 194 | + args_match = args_def_ptrn.match(line.strip()) |
| 195 | + |
| 196 | + if runs_match: |
| 197 | + for run in runs_match.group(1).strip().split(): |
| 198 | + runs_arg_lines[run] = [] |
| 199 | + |
| 200 | + elif args_match: |
| 201 | + runs_arg_lines[args_match.group(1)].append(args_match.group(2)) |
| 202 | + |
| 203 | + for run, lines in runs_arg_lines.items(): |
| 204 | + metadata_dict = self.__read_args__(lines) |
| 205 | + self.__resolve_env_vals__(metadata_dict) |
| 206 | + |
| 207 | + # store the run value and script location in the |
| 208 | + # metadata object |
| 209 | + metadata_dict['py_script_path'] = py_script_path |
| 210 | + metadata_dict['run'] = run |
| 211 | + |
| 212 | + metadata = Metadata() |
| 213 | + |
| 214 | + metadata.copy_from_dict(metadata_dict) |
| 215 | + runs_metadata.append(metadata) |
| 216 | + |
| 217 | + return runs_metadata |
0 commit comments