From nobody Tue Nov 26 14:32:30 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1573483533; cv=none; d=zoho.com; s=zohoarc; b=fNBY02APVZQD0tqVfVLMBB5CECYyS1TXCQlYYzy7NDnHdz1su04aEbg4kBdr26PNEc/he08Gv5PqR+kPYOUKt8Svas1Q6ihXUe/SMXoV2MEgeQ7F7nsgMPY2saysCcmBJg9dVgUu6RmBh0PxStDCDjkLkBWJa5yWmL/oDCo2kbU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573483533; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=9YbELyDUG90M4vLIhp5Vj4/jVfVt7SaOSBxPNma/Lcs=; b=mMh7+EZ1whnkM7Gcgpvi+s3wO/8fo5C/t+u43nZ73E8CtCwCzba2eip8s8xc48UBL89sMV+Nyy/+4NJ+6U51sW6XH8efxYg0CKWM1YhcfM1XnMY4Am+2rcBWiwePJPsGfwX6/b074MMX2Qc4ErB0GzWySXZ6bhBrmd9PGBbpvg4= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1573483533150174.90540112526242; Mon, 11 Nov 2019 06:45:33 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-288-zuudlnZGPKCYkSmU_x8sgg-1; Mon, 11 Nov 2019 09:45:29 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EA9908A2422; Mon, 11 Nov 2019 14:45:22 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BD35D4B5; Mon, 11 Nov 2019 14:45:22 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 72BA6180221C; Mon, 11 Nov 2019 14:45:22 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xABEj8kC020534 for ; Mon, 11 Nov 2019 09:45:08 -0500 Received: by smtp.corp.redhat.com (Postfix) id A9D575DD73; Mon, 11 Nov 2019 14:45:08 +0000 (UTC) Received: from catbus.gsslab.fab.redhat.com (dhcp-32.gsslab.fab.redhat.com [10.33.9.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id E0DEF1B41F; Mon, 11 Nov 2019 14:45:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1573483531; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=9YbELyDUG90M4vLIhp5Vj4/jVfVt7SaOSBxPNma/Lcs=; b=TiPXX+ofrU7p5GaSgh7EzdAuAQXY28jiOx+yc3E/gS9er2yBpo2qA64p5sQ+afIqqJWkq7 pwETFlWa7Ea1vg9MeZHA4vrLUoWswdbjv5arOevyILwa3i4cdK02DW7O9LRurwY1QA+R6e 4cSxLq76nNA6sqQccnhYYfoDaW4CPoI= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Mon, 11 Nov 2019 14:38:19 +0000 Message-Id: <20191111143826.16050-17-berrange@redhat.com> In-Reply-To: <20191111143826.16050-1-berrange@redhat.com> References: <20191111143826.16050-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v5 16/23] tests: rewrite qemu capability grouper in Python X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: zuudlnZGPKCYkSmU_x8sgg-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" As part of an goal to eliminate Perl from libvirt build tools, rewrite the group-qemu-caps.pl tool in Python. This was a straight conversion, manually going line-by-line to change the syntax from Perl to Python. Thus the overall structure of the file and approach is the same. Signed-off-by: Daniel P. Berrang=C3=A9 Tested-by: Cole Robinson --- Makefile.am | 1 + build-aux/syntax-check.mk | 3 +- scripts/group-qemu-caps.py | 123 ++++++++++++++++++++++++++++++++++++ tests/group-qemu-caps.pl | 124 ------------------------------------- 4 files changed, 126 insertions(+), 125 deletions(-) create mode 100755 scripts/group-qemu-caps.py delete mode 100755 tests/group-qemu-caps.pl diff --git a/Makefile.am b/Makefile.am index 6f6cead526..769cd4ce64 100644 --- a/Makefile.am +++ b/Makefile.am @@ -57,6 +57,7 @@ EXTRA_DIST =3D \ scripts/dtrace2systemtap.py \ scripts/genpolkit.py \ scripts/gensystemtap.py \ + scripts/group-qemu-caps.py \ scripts/header-ifdef.py \ scripts/minimize-po.py \ scripts/mock-noinline.py \ diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index 7d54df182a..44639f499e 100644 --- a/build-aux/syntax-check.mk +++ b/build-aux/syntax-check.mk @@ -2176,7 +2176,8 @@ test-wrap-argv: $(PYTHON) $(top_srcdir)/scripts/test-wrap-argv.py --check =20 group-qemu-caps: - $(AM_V_GEN)$(PERL) $(top_srcdir)/tests/group-qemu-caps.pl --check $(top_s= rcdir)/ + $(AM_V_GEN)$(RUNUTF8) $(PYTHON) $(top_srcdir)/scripts/group-qemu-caps.py \ + --check --prefix $(top_srcdir)/ =20 # List all syntax-check exemptions: exclude_file_name_regexp--sc_avoid_strcase =3D ^tools/vsh\.h$$ diff --git a/scripts/group-qemu-caps.py b/scripts/group-qemu-caps.py new file mode 100755 index 0000000000..3edaf5d09f --- /dev/null +++ b/scripts/group-qemu-caps.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see +# . +# +# +# Regroup array values into smaller groups separated by numbered comments. +# +# If --check is the first parameter, the script will return +# a non-zero value if a file is not grouped correctly. +# Otherwise the files are regrouped in place. + +from __future__ import print_function + +import argparse +import re +import subprocess +import sys + + +def regroup_caps(check, filename, start_regex, end_regex, + trailing_newline, counter_prefix): + step =3D 5 + + original =3D [] + with open(filename, "r") as fh: + for line in fh: + original.append(line) + + fixed =3D [] + game_on =3D False + counter =3D 0 + for line in original: + line =3D line.rstrip("\n") + if game_on: + if re.search(r'''.*/\* [0-9]+ \*/.*''', line): + continue + if re.search(r'''^\s*$''', line): + continue + if counter % step =3D=3D 0: + if counter !=3D 0: + fixed.append("\n") + fixed.append("%s/* %d */\n" % (counter_prefix, counter)) + + if not (line.find("/*") !=3D -1 and line.find("*/") =3D=3D -1): + # count two-line comments as one line + counter =3D counter + 1 + + if re.search(start_regex, line): + game_on =3D True + elif game_on and re.search(end_regex, line): + if (counter - 1) % step =3D=3D 0: + fixed =3D fixed[:-1] # /* $counter */ + if counter !=3D 1: + fixed =3D fixed[:-1] # \n + + if trailing_newline: + fixed.append("\n") + + game_on =3D False + + fixed.append(line + "\n") + + if check: + orig =3D "".join(original) + new =3D "".join(fixed) + if new !=3D orig: + diff =3D subprocess.Popen(["diff", "-u", filename, "-"], + stdin=3Dsubprocess.PIPE) + diff.communicate(input=3Dnew.encode('utf-8')) + + print("Incorrect line wrapping in $file", + file=3Dsys.stderr) + print("Use test-wrap-argv.py to wrap test data files", + file=3Dsys.stderr) + return False + else: + with open(filename, "w") as fh: + for line in fixed: + print(line, file=3Dfh, end=3D'') + + return True + + +parser =3D argparse.ArgumentParser(description=3D'Test arg line wrapper') +parser.add_argument('--check', action=3D"store_true", + help=3D'check existing files only') +parser.add_argument('--prefix', default=3D'', + help=3D'source code tree prefix') +args =3D parser.parse_args() + +errs =3D False + +if not regroup_caps(args.check, + args.prefix + 'src/qemu/qemu_capabilities.c', + r'virQEMUCaps grouping marker', + r'\);', + 0, + " "): + errs =3D True + +if not regroup_caps(args.check, + args.prefix + 'src/qemu/qemu_capabilities.h', + r'virQEMUCapsFlags grouping marker', + r'QEMU_CAPS_LAST \/\* this must', + 1, + " "): + errs =3D True + +if errs: + sys.exit(1) +sys.exit(0) diff --git a/tests/group-qemu-caps.pl b/tests/group-qemu-caps.pl deleted file mode 100755 index 829e63a562..0000000000 --- a/tests/group-qemu-caps.pl +++ /dev/null @@ -1,124 +0,0 @@ -#!/usr/bin/env perl -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see -# . -# -# -# Regroup array values into smaller groups separated by numbered comments. -# -# If --check is the first parameter, the script will return -# a non-zero value if a file is not grouped correctly. -# Otherwise the files are regrouped in place. - -use strict; -use warnings; - -my $check =3D 0; - -if (defined $ARGV[0] && $ARGV[0] eq "--check") { - $check =3D 1; - shift @ARGV; -} - -my $prefix =3D ''; -if (defined $ARGV[0]) { - $prefix =3D $ARGV[0]; - shift @ARGV; -} - -my $ret =3D 0; -if (®roup_caps($prefix . 'src/qemu/qemu_capabilities.c', - 'virQEMUCaps grouping marker', - '\);', - 0, - " ") < 0) { - $ret =3D 1; -} -if (®roup_caps($prefix . 'src/qemu/qemu_capabilities.h', - 'virQEMUCapsFlags grouping marker', - 'QEMU_CAPS_LAST \/\* this must', - 1, - " ") < 0) { - $ret =3D 1; -} - -exit $ret; - -sub regroup_caps { - my $filename =3D shift; - my $start_regex =3D shift; - my $end_regex =3D shift; - my $trailing_newline =3D shift; - my $counter_prefix =3D shift; - my $step =3D 5; - - open FILE, '<', $filename or die "cannot open $filename: $!"; - my @original =3D ; - close FILE; - - my @fixed; - my $game_on =3D 0; - my $counter =3D 0; - foreach (@original) { - if ($game_on) { - next if ($_ =3D~ '/\* [0-9]+ \*/'); - next if (/^\s+$/); - if ($counter % $step =3D=3D 0) { - if ($counter !=3D 0) { - push @fixed, "\n"; - } - push @fixed, "$counter_prefix/* $counter */\n"; - } - if (!($_ =3D~ '/\*' && !($_ =3D~ '\*/'))) { - # count two-line comments as one line - $counter++; - } - } - if (/$start_regex/) { - $game_on =3D 1; - } elsif ($game_on && $_ =3D~ /$end_regex/) { - if (($counter -1) % $step =3D=3D 0) { - pop @fixed; # /* $counter */ - if ($counter !=3D 1) { - pop @fixed; # \n - } - } - if ($trailing_newline) { - push @fixed, "\n"; - } - $game_on =3D 0; - } - push @fixed, $_; - } - - if ($check) { - my $nl =3D join('', @fixed); - my $ol =3D join('', @original); - unless ($nl eq $ol) { - open DIFF, "| diff -u $filename -" or die "cannot run diff: $!= "; - print DIFF $nl; - close DIFF; - - print STDERR "Incorrect array grouping in $filename\n"; - print STDERR "Use group-qemu-caps.pl to group long array membe= rs\n"; - return -1; - } - } else { - open FILE, '>', $filename or die "cannot open $filename: $!"; - foreach my $line (@fixed) { - print FILE $line; - } - close FILE; - } -} --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list