From patchwork Mon Apr 26 10:54:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruce Richardson X-Patchwork-Id: 92166 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id B6094A0548; Mon, 26 Apr 2021 12:54:18 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3277741104; Mon, 26 Apr 2021 12:54:18 +0200 (CEST) Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by mails.dpdk.org (Postfix) with ESMTP id E03F740140 for ; Mon, 26 Apr 2021 12:54:15 +0200 (CEST) IronPort-SDR: tziwa8vRhs9bw9WpokY8RDXDMdB2bSulP9Fxjb2l8HDtWuXzbas7iVBTmHypr0m0jUej0HjSCD 0Rii2tx/0C5Q== X-IronPort-AV: E=McAfee;i="6200,9189,9965"; a="193129092" X-IronPort-AV: E=Sophos;i="5.82,252,1613462400"; d="scan'208";a="193129092" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Apr 2021 03:54:12 -0700 IronPort-SDR: ge6JjpTJyTRd+7FljM383D4wqHXPcI18JNJci6uv1e/6jCJAUTofG3ibYsFbJLr5RxXob1FFjg ze7Av4S64+NA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,252,1613462400"; d="scan'208";a="615338335" Received: from silpixa00399126.ir.intel.com ([10.237.223.81]) by fmsmga006.fm.intel.com with ESMTP; 26 Apr 2021 03:54:11 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: thomas@monjalon.net, anatoly.burakov@intel.com, Bruce Richardson Date: Mon, 26 Apr 2021 11:54:02 +0100 Message-Id: <20210426105403.226004-1-bruce.richardson@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210422090211.320855-1-bruce.richardson@intel.com> References: <20210422090211.320855-1-bruce.richardson@intel.com> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH v2 1/2] devtools: script to check meson indentation of lists X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" This is a script to fix up minor formatting issues in meson files. It scans for, and can optionally fix, indentation issues and missing trailing commas in the lists in meson.build files. It also detects, and can fix, multi-line lists where more than one entry appears on a line. Signed-off-by: Bruce Richardson Reviewed-by: Anatoly Burakov --- devtools/dpdk_meson_check.py | 125 +++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100755 devtools/dpdk_meson_check.py diff --git a/devtools/dpdk_meson_check.py b/devtools/dpdk_meson_check.py new file mode 100755 index 000000000..29f788796 --- /dev/null +++ b/devtools/dpdk_meson_check.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2021 Intel Corporation + +''' +A Python script to run some checks on meson.build files in DPDK +''' + +import sys +import os +from os.path import relpath, join +from argparse import ArgumentParser + +VERBOSE = False + + +def scan_dir(path): + '''return meson.build files found in path''' + for root, dirs, files in os.walk(path): + if 'meson.build' in files: + yield(relpath(join(root, 'meson.build'))) + + +def split_code_comments(line): + 'splits a line into a code part and a comment part, returns (code, comment) tuple' + if line.lstrip().startswith('#'): + return ('', line) + elif '#' in line and '#include' not in line: # catch 99% of cases, not 100% + idx = line.index('#') + while (line[idx - 1].isspace()): + idx -= 1 + return line[:idx], line[idx:] + else: + return (line, '') + + +def setline(contents, index, value): + 'sets the contents[index] to value. Returns the line, along with code and comments part' + line = contents[index] = value + code, comments = split_code_comments(line) + return line, code, comments + + +def check_indentation(filename, contents): + '''check that a list or files() is correctly indented''' + infiles = False + inlist = False + edit_count = 0 + for lineno, line in enumerate(contents): + code, comments = split_code_comments(line) + if not code.strip(): + continue + if code.endswith('files('): + if infiles: + raise(f'Error parsing {filename}:{lineno}, got "files(" when already parsing files list') + if inlist: + print(f'Error parsing {filename}:{lineno}, got "files(" when already parsing array list') + infiles = True + indent_count = len(code) - len(code.lstrip(' ')) + indent = ' ' * (indent_count + 8) # double indent required + elif code.endswith('= ['): + if infiles: + raise(f'Error parsing {filename}:{lineno}, got start of array when already parsing files list') + if inlist: + print(f'Error parsing {filename}:{lineno}, got start of array when already parsing array list') + inlist = True + indent_count = len(code) - len(code.lstrip(' ')) + indent = ' ' * (indent_count + 8) # double indent required + elif infiles and (code.endswith(')') or code.strip().startswith(')')): + infiles = False + continue + elif inlist and (code.endswith(']') or code.strip().startswith(']')): + inlist = False + continue + elif inlist or infiles: + # skip further subarrays or lists + if '[' in code or ']' in code: + continue + if not code.startswith(indent) or code[len(indent)] == ' ': + print(f'Error: Incorrect indent at {filename}:{lineno + 1}') + line, code, comments = setline(contents, lineno, indent + line.strip()) + edit_count += 1 + if not code.endswith(','): + print(f'Error: Missing trailing "," in list at {filename}:{lineno + 1}') + line, code, comments = setline(contents, lineno, code + ',' + comments) + edit_count += 1 + if len(code.split(',')) > 2: # only one comma per line + print(f'Error: multiple entries per line in list at {filename}:{lineno +1}') + entries = [e.strip() for e in code.split(',') if e.strip()] + line, code, comments = setline(contents, lineno, + indent + (',\n' + indent).join(entries) + + ',' + comments) + edit_count += 1 + return edit_count + + +def process_file(filename, fix): + '''run checks on file "filename"''' + if VERBOSE: + print(f'Processing {filename}') + with open(filename) as f: + contents = [ln.rstrip() for ln in f.readlines()] + + if check_indentation(filename, contents) > 0 and fix: + print(f"Fixing {filename}") + with open(filename, 'w') as f: + f.writelines([f'{ln}\n' for ln in contents]) + + +def main(): + '''parse arguments and then call other functions to do work''' + global VERBOSE + parser = ArgumentParser(description='Run syntax checks on DPDK meson.build files') + parser.add_argument('-d', metavar='directory', default='.', help='Directory to process') + parser.add_argument('--fix', action='store_true', help='Attempt to fix errors') + parser.add_argument('-v', action='store_true', help='Verbose output') + args = parser.parse_args() + + VERBOSE = args.v + for f in scan_dir(args.d): + process_file(f, args.fix) + + +if __name__ == "__main__": + main()