From 0e558059d10bfcc6bb8564c8b398a2e57935782d Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Mon, 30 Mar 2020 15:10:00 -0400 Subject: [PATCH] ci: detect what changed in a PR and if full sanitycheck is needed. Script to detect if full sanitycheck should run or if we can skip it and just run the code that actuallt changed (in samples/tests). Signed-off-by: Anas Nashif --- scripts/ci/sanitycheck_ignore.txt | 26 +++++++++++++++ scripts/ci/what_changed.py | 54 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 scripts/ci/sanitycheck_ignore.txt create mode 100755 scripts/ci/what_changed.py diff --git a/scripts/ci/sanitycheck_ignore.txt b/scripts/ci/sanitycheck_ignore.txt new file mode 100644 index 00000000000..f452fdc0648 --- /dev/null +++ b/scripts/ci/sanitycheck_ignore.txt @@ -0,0 +1,26 @@ +# Add one pattern per line. +# +# The patterns listed in this file will be compared with the list of files +# changed in a patch series (Pull Request) and if all files in the pull request +# are matched, then sanitycheck will not do a full run and optionally will only +# run on changed tests or boards. +# +.clang-format +.codecov.yml +.editorconfig +.gitattributes +.gitignore +.known-issues +.mailmap +.uncrustify.cfg +CODEOWNERS +LICENSE +Makefile +tests/* +samples/* +*.rst +*.md +# if we change this file or associated script, it should not trigger a full +# sanitycheck. +scripts/ci/sanitycheck_ignore.txt +scripts/ci/what_changed.py diff --git a/scripts/ci/what_changed.py b/scripts/ci/what_changed.py new file mode 100755 index 00000000000..bfd1ba20f08 --- /dev/null +++ b/scripts/ci/what_changed.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2020 Intel Corporation +# Check if full sanitycheck is needed. + +import os +import sh +import argparse +import fnmatch + + +if "ZEPHYR_BASE" not in os.environ: + exit("$ZEPHYR_BASE environment variable undefined.") + +repository_path = os.environ['ZEPHYR_BASE'] +sh_special_args = { + '_tty_out': False, + '_cwd': repository_path +} + +def parse_args(): + parser = argparse.ArgumentParser( + description="Check if change requires full sanitycheck") + parser.add_argument('-c', '--commits', default=None, + help="Commit range in the form: a..b") + return parser.parse_args() + +def main(): + args = parse_args() + if not args.commits: + exit(1) + + # pylint does not like the 'sh' library + # pylint: disable=too-many-function-args,unexpected-keyword-arg + commit = sh.git("diff", "--name-only", args.commits, **sh_special_args) + files = commit.split("\n") + + with open("scripts/ci/sanitycheck_ignore.txt", "r") as sc_ignore: + ignores = sc_ignore.read().splitlines() + ignores = filter(lambda x: not x.startswith("#"), ignores) + + found = [] + files = list(filter(lambda x: x, files)) + + for pattern in ignores: + if pattern: + found.extend(fnmatch.filter(files, pattern)) + + if sorted(files) != sorted(found): + print("full") + + +if __name__ == "__main__": + main()