From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 5DBA1A0548; Mon, 26 Apr 2021 15:40:33 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4D46741104; Mon, 26 Apr 2021 15:40:33 +0200 (CEST) Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by mails.dpdk.org (Postfix) with ESMTP id F1A1440140 for ; Mon, 26 Apr 2021 15:40:31 +0200 (CEST) IronPort-SDR: Nm0N5txfbZztkbpRb99Hzu/I3AsIkxXKD+k+bJrfl61VaHxip/4U3yNhlREgNiFll55bXdFnbH erntZRliGsAQ== X-IronPort-AV: E=McAfee;i="6200,9189,9966"; a="196447233" X-IronPort-AV: E=Sophos;i="5.82,252,1613462400"; d="scan'208";a="196447233" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Apr 2021 06:40:30 -0700 IronPort-SDR: nCgM8RAaNiX3+qx8+gsj1/nGe1+G9yX7eDrhzPJ9MeFUMuNCCa+kRR0lByrAyoTbrYcNNHVtZs 1cVXvxdncDxQ== X-IronPort-AV: E=Sophos;i="5.82,252,1613462400"; d="scan'208";a="536230973" Received: from aburakov-mobl.ger.corp.intel.com (HELO [10.213.222.4]) ([10.213.222.4]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Apr 2021 06:40:29 -0700 To: Bruce Richardson , dev@dpdk.org Cc: thomas@monjalon.net References: <20210422090211.320855-1-bruce.richardson@intel.com> <20210426105403.226004-1-bruce.richardson@intel.com> From: "Burakov, Anatoly" Message-ID: <32b16ecf-be20-1ad2-43b1-df0f4864f681@intel.com> Date: Mon, 26 Apr 2021 14:40:25 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <20210426105403.226004-1-bruce.richardson@intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [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" On 26-Apr-21 11:54 AM, Bruce Richardson wrote: > 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 > --- > +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:] I think this could be simplified with regex: # find any occurrences of '#' but only if it's not an '#include' if not re.search(r'#(?!include)', line) return line, '' return line.split('#', maxsplit=1) > + 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) Nitpicking, but maybe instead of calling strip() all over the place, just count the number of spaces and strip right at the outset? E.g. something like: stripped = code.strip() line_indent = len(code) - len(stripped) You can then reason about indent levels by comparing stripped to code afterwards, and avoid doing this: > + # skip further subarrays or lists > + if '[' in code or ']' in code: > + continue > + if not code.startswith(indent) or code[len(indent)] == ' ': Opting to just check the indent size you calculated initially. Unless i'm missing something :) You could also increment edit_count if `calculated indent + stripped` is equal to `code`. Seems easier logic than raw string manipulation you're going for here... > +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()] So any trailing whitespace gets automatically and silently fixed? > + > + 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]) Something seems suspect here. So, if `fix` is *not* specified, the script just opens the file, reads it, and... does nothing else? -- Thanks, Anatoly