Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | #!/usr/bin/env python3 # SPDX-License-Identifier: Apache-2.0 # Copyright (c) 2021 Intel Corporation # A script to generate twister options based on modified files. import re, os import argparse import yaml import fnmatch import subprocess import json import logging import sys from pathlib import Path from git import Repo from west.manifest import Manifest if "ZEPHYR_BASE" not in os.environ: exit("$ZEPHYR_BASE environment variable undefined.") repository_path = Path(os.environ['ZEPHYR_BASE']) logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) sys.path.append(os.path.join(repository_path, 'scripts')) import list_boards def _get_match_fn(globs, regexes): # Constructs a single regex that tests for matches against the globs in # 'globs' and the regexes in 'regexes'. Parts are joined with '|' (OR). # Returns the search() method of the compiled regex. # # Returns None if there are neither globs nor regexes, which should be # interpreted as no match. if not (globs or regexes): return None regex = "" if globs: glob_regexes = [] for glob in globs: # Construct a regex equivalent to the glob glob_regex = glob.replace(".", "\\.").replace("*", "[^/]*") \ .replace("?", "[^/]") if not glob.endswith("/"): # Require a full match for globs that don't end in / glob_regex += "$" glob_regexes.append(glob_regex) # The glob regexes must anchor to the beginning of the path, since we # return search(). (?:) is a non-capturing group. regex += "^(?:{})".format("|".join(glob_regexes)) if regexes: if regex: regex += "|" regex += "|".join(regexes) return re.compile(regex).search class Tag: """ Represents an entry for a tag in tags.yaml. These attributes are available: name: List of GitHub labels for the area. Empty if the area has no 'labels' key. description: Text from 'description' key, or None if the area has no 'description' key """ def _contains(self, path): # Returns True if the area contains 'path', and False otherwise return self._match_fn and self._match_fn(path) and not \ (self._exclude_match_fn and self._exclude_match_fn(path)) def __repr__(self): return "<Tag {}>".format(self.name) class Filters: def __init__(self, modified_files, pull_request=False, platforms=[]): self.modified_files = modified_files self.twister_options = [] self.full_twister = False self.all_tests = [] self.tag_options = [] self.pull_request = pull_request self.platforms = platforms self.default_run = False def process(self): self.find_modules() self.find_tags() self.find_tests() if not self.platforms: self.find_archs() self.find_boards() if self.default_run: self.find_excludes(skip=["tests/*", "boards/*/*/*"]) else: self.find_excludes() def get_plan(self, options, integration=False): fname = "_test_plan_partial.json" cmd = ["scripts/twister", "-c"] + options + ["--save-tests", fname ] if integration: cmd.append("--integration") logging.info(" ".join(cmd)) _ = subprocess.call(cmd) with open(fname, newline='') as jsonfile: json_data = json.load(jsonfile) suites = json_data.get("testsuites", []) self.all_tests.extend(suites) if os.path.exists(fname): os.remove(fname) def find_modules(self): if 'west.yml' in self.modified_files: print(f"Manifest file 'west.yml' changed") print("=========") old_manifest_content = repo.git.show(f"{args.commits[:-2]}:west.yml") with open("west_old.yml", "w") as manifest: manifest.write(old_manifest_content) old_manifest = Manifest.from_file("west_old.yml") new_manifest = Manifest.from_file("west.yml") old_projs = set((p.name, p.revision) for p in old_manifest.projects) new_projs = set((p.name, p.revision) for p in new_manifest.projects) logging.debug(f'old_projs: {old_projs}') logging.debug(f'new_projs: {new_projs}') # Removed projects rprojs = set(filter(lambda p: p[0] not in list(p[0] for p in new_projs), old_projs - new_projs)) # Updated projects uprojs = set(filter(lambda p: p[0] in list(p[0] for p in old_projs), new_projs - old_projs)) # Added projects aprojs = new_projs - old_projs - uprojs # All projs projs = rprojs | uprojs | aprojs projs_names = [name for name, rev in projs] logging.info(f'rprojs: {rprojs}') logging.info(f'uprojs: {uprojs}') logging.info(f'aprojs: {aprojs}') logging.info(f'project: {projs_names}') _options = [] for p in projs_names: _options.extend(["-t", p ]) if self.platforms: for platform in self.platforms: _options.extend(["-p", platform]) self.get_plan(_options, True) def find_archs(self): # we match both arch/<arch>/* and include/arch/<arch> and skip common. # Some architectures like riscv require special handling, i.e. riscv # directory covers 2 architectures known to twister: riscv32 and riscv64. archs = set() for f in self.modified_files: p = re.match(r"^arch\/([^/]+)\/", f) if not p: p = re.match(r"^include\/arch\/([^/]+)\/", f) if p: if p.group(1) != 'common': if p.group(1) == 'riscv': archs.add('riscv32') archs.add('riscv64') else: archs.add(p.group(1)) _options = [] for arch in archs: _options.extend(["-a", arch ]) if _options: logging.info(f'Potential architecture filters...') if self.platforms: for platform in self.platforms: _options.extend(["-p", platform]) self.get_plan(_options, True) else: self.get_plan(_options, False) def find_boards(self): boards = set() all_boards = set() for f in self.modified_files: if f.endswith(".rst") or f.endswith(".png") or f.endswith(".jpg"): continue p = re.match(r"^boards\/[^/]+\/([^/]+)\/", f) if p and p.groups(): boards.add(p.group(1)) # Limit search to $ZEPHYR_BASE since this is where the changed files are lb_args = argparse.Namespace(**{ 'arch_roots': [repository_path], 'board_roots': [repository_path] }) known_boards = list_boards.find_boards(lb_args) for b in boards: name_re = re.compile(b) for kb in known_boards: if name_re.search(kb.name): all_boards.add(kb.name) _options = [] if len(all_boards) > 20: logging.warning(f"{len(boards)} boards changed, this looks like a global change, skipping test handling, revert to default.") self.default_run = True return for board in all_boards: _options.extend(["-p", board ]) if _options: logging.info(f'Potential board filters...') self.get_plan(_options) def find_tests(self): tests = set() for f in self.modified_files: if f.endswith(".rst"): continue d = os.path.dirname(f) while d: if os.path.exists(os.path.join(d, "testcase.yaml")) or \ os.path.exists(os.path.join(d, "sample.yaml")): tests.add(d) break else: d = os.path.dirname(d) _options = [] for t in tests: _options.extend(["-T", t ]) if len(tests) > 20: logging.warning(f"{len(tests)} tests changed, this looks like a global change, skipping test handling, revert to default") self.default_run = True return if _options: logging.info(f'Potential test filters...({len(tests)} changed...)') if self.platforms: for platform in self.platforms: _options.extend(["-p", platform]) else: _options.append("--all") self.get_plan(_options) def find_tags(self): tag_cfg_file = os.path.join(repository_path, 'scripts', 'ci', 'tags.yaml') with open(tag_cfg_file, 'r') as ymlfile: tags_config = yaml.safe_load(ymlfile) tags = {} for t,x in tags_config.items(): tag = Tag() tag.exclude = True tag.name = t # tag._match_fn(path) tests if the path matches files and/or # files-regex tag._match_fn = _get_match_fn(x.get("files"), x.get("files-regex")) # Like tag._match_fn(path), but for files-exclude and # files-regex-exclude tag._exclude_match_fn = \ _get_match_fn(x.get("files-exclude"), x.get("files-regex-exclude")) tags[tag.name] = tag for f in self.modified_files: for t in tags.values(): if t._contains(f): t.exclude = False exclude_tags = set() for t in tags.values(): if t.exclude: exclude_tags.add(t.name) for tag in exclude_tags: self.tag_options.extend(["-e", tag ]) if exclude_tags: logging.info(f'Potential tag based filters: {exclude_tags}') def find_excludes(self, skip=[]): with open("scripts/ci/twister_ignore.txt", "r") as twister_ignore: ignores = twister_ignore.read().splitlines() ignores = filter(lambda x: not x.startswith("#"), ignores) found = set() files = list(filter(lambda x: x, self.modified_files)) for pattern in ignores: if pattern in skip: continue if pattern: found.update(fnmatch.filter(files, pattern)) logging.debug(found) logging.debug(files) if sorted(files) != sorted(found): _options = [] logging.info(f'Need to run full or partial twister...') self.full_twister = True if self.platforms: for platform in self.platforms: _options.extend(["-p", platform]) _options.extend(self.tag_options) self.get_plan(_options) else: _options.extend(self.tag_options) self.get_plan(_options, True) else: logging.info(f'No twister needed or partial twister run only...') def parse_args(): parser = argparse.ArgumentParser( description="Generate twister argument files based on modified file", allow_abbrev=False) parser.add_argument('-c', '--commits', default=None, help="Commit range in the form: a..b") parser.add_argument('-m', '--modified-files', default=None, help="File with information about changed/deleted/added files.") parser.add_argument('-o', '--output-file', default="testplan.json", help="JSON file with the test plan to be passed to twister") parser.add_argument('-P', '--pull-request', action="store_true", help="This is a pull request") parser.add_argument('-p', '--platform', action="append", help="Limit this for a platform or a list of platforms.") parser.add_argument('-t', '--tests_per_builder', default=700, type=int, help="Number of tests per builder") parser.add_argument('-n', '--default-matrix', default=10, type=int, help="Number of tests per builder") return parser.parse_args() if __name__ == "__main__": args = parse_args() files = [] errors = 0 if args.commits: repo = Repo(repository_path) commit = repo.git.diff("--name-only", args.commits) files = commit.split("\n") elif args.modified_files: with open(args.modified_files, "r") as fp: files = json.load(fp) if files: print("Changed files:\n=========") print("\n".join(files)) print("=========") f = Filters(files, args.pull_request, args.platform) f.process() # remove dupes and filtered cases dup_free = [] dup_free_set = set() logging.info(f'Total tests gathered: {len(f.all_tests)}') for ts in f.all_tests: if ts.get('status') == 'filtered': continue n = ts.get("name") a = ts.get("arch") p = ts.get("platform") if ts.get('status') == 'error': logging.info(f"Error found: {n} on {p} ({ts.get('reason')})") errors += 1 if (n, a, p,) not in dup_free_set: dup_free.append(ts) dup_free_set.add((n, a, p,)) logging.info(f'Total tests to be run: {len(dup_free)}') with open(".testplan", "w") as tp: total_tests = len(dup_free) if total_tests and total_tests < args.tests_per_builder: nodes = 1 else: nodes = round(total_tests / args.tests_per_builder) tp.write(f"TWISTER_TESTS={total_tests}\n") tp.write(f"TWISTER_NODES={nodes}\n") tp.write(f"TWISTER_FULL={f.full_twister}\n") logging.info(f'Total nodes to launch: {nodes}') header = ['test', 'arch', 'platform', 'status', 'extra_args', 'handler', 'handler_time', 'used_ram', 'used_rom'] # write plan if dup_free: data = {} data['testsuites'] = dup_free with open(args.output_file, 'w', newline='') as json_file: json.dump(data, json_file, indent=4, separators=(',',':')) sys.exit(errors) |