From bf7981f2bc1bf7c91b916554981c8fb4d5aa9877 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Thu, 24 Dec 2020 12:34:55 +0200 Subject: utils: checkstyle.py: Add commit checkers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new category of checkers that operate on a whole commit. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- utils/checkstyle.py | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index 9c2a1837..c0a6b7ab 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -296,6 +296,30 @@ class ClassRegistry(type): return newclass +# ------------------------------------------------------------------------------ +# Commit Checkers +# + +class CommitChecker(metaclass=ClassRegistry): + subclasses = [] + + def __init__(self): + pass + + # + # Class methods + # + @classmethod + def checkers(cls): + for checker in cls.subclasses: + yield checker + + +class CommitIssue(object): + def __init__(self, msg): + self.msg = msg + + # ------------------------------------------------------------------------------ # Style Checkers # @@ -721,25 +745,29 @@ def check_style(top_level, commit): print(commit.title) print(separator) + issues = 0 + + # Apply the commit checkers first. + for checker in CommitChecker.checkers(): + for issue in checker.check(commit, top_level): + print('%s%s%s' % (Colours.fg(Colours.Yellow), issue.msg, Colours.reset())) + issues += 1 + # Filter out files we have no checker for. patterns = set() patterns.update(StyleChecker.all_patterns()) patterns.update(Formatter.all_patterns()) files = [f for f in commit.files() if len([p for p in patterns if fnmatch.fnmatch(os.path.basename(f), p)])] - if len(files) == 0: - print("Commit doesn't touch source files, skipping") - return 0 - issues = 0 for f in files: issues += check_file(top_level, commit, f) if issues == 0: - print("No style issue detected") + print("No issue detected") else: print('---') - print("%u potential style %s detected, please review" % \ - (issues, 'issue' if issues == 1 else 'issues')) + print("%u potential %s detected, please review" % + (issues, 'issue' if issues == 1 else 'issues')) return issues -- cgit v1.2.1