From patchwork Mon May 1 20:14:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Conole X-Patchwork-Id: 757306 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wGwd01t9nz9sMN for ; Tue, 2 May 2017 06:14:44 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id B9384C50; Mon, 1 May 2017 20:14:15 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 0328FBF8 for ; Mon, 1 May 2017 20:14:14 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 8C667189 for ; Mon, 1 May 2017 20:14:13 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C9B4542101; Mon, 1 May 2017 20:14:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C9B4542101 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=aconole@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C9B4542101 Received: from dhcp-25-97.bos.redhat.com (unknown [10.18.25.172]) by smtp.corp.redhat.com (Postfix) with ESMTP id 72BD718B93; Mon, 1 May 2017 20:14:12 +0000 (UTC) From: Aaron Conole To: dev@openvswitch.org Date: Mon, 1 May 2017 16:14:03 -0400 Message-Id: <20170501201409.12116-2-aconole@redhat.com> In-Reply-To: <20170501201409.12116-1-aconole@redhat.com> References: <20170501201409.12116-1-aconole@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 01 May 2017 20:14:13 +0000 (UTC) X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [PATCH v4 1/7] checkpatch: introduce a flexible framework X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Developers wishing to add checks to checkpatch sift through an adhoc mess, currently. The process goes something like: 1. Figure out what to test in the patch 2. Write some code, quickly, that checks for that condition 3. Look through the statemachine to find where the check should go 4. ignore parts of the above and just throw something together That worked fine for the initial development, but as interesting new tests are developed, it is important to have a more flexible framework that lets a developer just plug in a new test, easily. This commit brings in a new framework that allows plugging in checks very quickly. Hook up the line-length test as an initial demonstration. Signed-off-by: Aaron Conole --- utilities/checkpatch.py | 51 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py index 638ac97..b3b833b 100755 --- a/utilities/checkpatch.py +++ b/utilities/checkpatch.py @@ -164,6 +164,47 @@ def pointer_whitespace_check(line): return __regex_ptr_declaration_missing_whitespace.search(line) is not None +def line_length_check(line): + """Return TRUE if the line length is too long""" + if len(line) > 79: + return True + return False + + +checks = [ + {'regex': None, + 'match_name': + lambda x: not any([fmt in x for fmt in line_length_blacklist]), + 'check': lambda x: line_length_check(x), + 'print': + lambda x: print_warning("Line is greater than 79-characters long", x)} +] + + +def get_file_type_checks(filename): + """Returns the list of checks for a file based on matching the filename + against regex.""" + global checks + checkList = [] + for check in checks: + if check['regex'] is None and check['match_name'] is None: + checkList.append(check) + if check['regex'] is not None and \ + re.compile(check['regex']).search(filename) is not None: + checkList.append(check) + elif check['match_name'] is not None and check['match_name'](filename): + checkList.append(check) + return checkList + + +def run_checks(current_file, line, lineno): + """Runs the various checks for the particular line. This will take + filename into account.""" + for check in get_file_type_checks(current_file): + if check['check'](line): + check['print'](lineno) + + def ovs_checkpatch_parse(text): global print_file_name lineno = 0 @@ -180,15 +221,10 @@ def ovs_checkpatch_parse(text): re.I | re.M | re.S) is_co_author = re.compile(r'(\s*(Co-authored-by: )(.*))$', re.I | re.M | re.S) - skip_line_length_check = False for line in text.decode().split('\n'): if current_file != previous_file: previous_file = current_file - if any([fmt in current_file for fmt in line_length_blacklist]): - skip_line_length_check = True - else: - skip_line_length_check = False lineno = lineno + 1 if len(line) <= 0: @@ -250,13 +286,10 @@ def ovs_checkpatch_parse(text): print_line = True print_warning("Line has non-spaces leading whitespace", lineno) + run_checks(current_file, cmp_line, lineno) if trailing_whitespace_or_crlf(cmp_line): print_line = True print_warning("Line has trailing whitespace", lineno) - if len(cmp_line) > 79 and not skip_line_length_check: - print_line = True - print_warning("Line is greater than 79-characters long", - lineno) if not if_and_for_whitespace_checks(cmp_line): print_line = True print_error("Improper whitespace around control block",