[libvirt] [PATCH v3 02/22] build-aux: rewrite augest test generator in Python

Daniel P. Berrangé berrange at redhat.com
Tue Sep 24 14:58:43 UTC 2019


As part of an goal to eliminate Perl from libvirt build tools,
rewrite the augeas-gentest.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.

The use of $(AUG_GENTEST) as a dependancy in the makefiles needed
to be fixed, because this was assumed to be the filename of the
script, but is in fact a full shell command line.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 Makefile.am                     |  2 +-
 build-aux/augeas-gentest.pl     | 60 ---------------------------
 build-aux/augeas-gentest.py     | 72 +++++++++++++++++++++++++++++++++
 src/Makefile.am                 |  3 +-
 src/bhyve/Makefile.inc.am       |  4 +-
 src/interface/Makefile.inc.am   |  2 +-
 src/libxl/Makefile.inc.am       |  4 +-
 src/locking/Makefile.inc.am     |  6 +--
 src/logging/Makefile.inc.am     |  2 +-
 src/lxc/Makefile.inc.am         |  4 +-
 src/network/Makefile.inc.am     |  2 +-
 src/node_device/Makefile.inc.am |  2 +-
 src/nwfilter/Makefile.inc.am    |  2 +-
 src/qemu/Makefile.inc.am        |  4 +-
 src/remote/Makefile.inc.am      |  4 +-
 src/secret/Makefile.inc.am      |  2 +-
 src/storage/Makefile.inc.am     |  2 +-
 src/vbox/Makefile.inc.am        |  2 +-
 src/vz/Makefile.inc.am          |  2 +-
 19 files changed, 97 insertions(+), 84 deletions(-)
 delete mode 100755 build-aux/augeas-gentest.pl
 create mode 100755 build-aux/augeas-gentest.py

diff --git a/Makefile.am b/Makefile.am
index 711f365504..17448a914e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -41,7 +41,7 @@ EXTRA_DIST = \
   run.in \
   README.md \
   AUTHORS.in \
-  build-aux/augeas-gentest.pl \
+  build-aux/augeas-gentest.py \
   build-aux/check-spacing.pl \
   build-aux/gitlog-to-changelog \
   build-aux/header-ifdef.pl \
diff --git a/build-aux/augeas-gentest.pl b/build-aux/augeas-gentest.pl
deleted file mode 100755
index 65834b533b..0000000000
--- a/build-aux/augeas-gentest.pl
+++ /dev/null
@@ -1,60 +0,0 @@
-#!/usr/bin/env perl
-#
-# augeas-gentest.pl: Generate an augeas test file, from an
-#                    example config file + test file template
-#
-# 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
-# <http://www.gnu.org/licenses/>.
-
-use strict;
-use warnings;
-
-die "syntax: $0 CONFIG TEMPLATE\n" unless @ARGV == 2;
-
-my $config = shift @ARGV;
-my $template = shift @ARGV;
-
-open CONFIG, "<", $config or die "cannot read $config: $!";
-open TEMPLATE, "<", $template or die "cannot read $template: $!";
-
-my $group = 0;
-while (<TEMPLATE>) {
-    if (/\@CONFIG\@/) {
-        my $group = 0;
-        print "  let conf = \"";
-        while (<CONFIG>) {
-            if (/^#\w/) {
-                s/^#//;
-                s/\"/\\\"/g;
-                print $_;
-                $group = /\[\s$/;
-            } elsif ($group) {
-                s/\"/\\\"/g;
-                if (/#\s*\]/) {
-                    $group = 0;
-                }
-                if (/^#/) {
-                    s/^#//;
-                    print $_;
-                }
-            }
-        }
-        print "\"\n";
-    } else {
-        print $_;
-    }
-}
-
-close TEMPLATE;
-close CONFIG;
diff --git a/build-aux/augeas-gentest.py b/build-aux/augeas-gentest.py
new file mode 100755
index 0000000000..18c6490a08
--- /dev/null
+++ b/build-aux/augeas-gentest.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2012-2019 Red Hat, Inc.
+#
+# augeas-gentest.py: Generate an augeas test file, from an
+#                    example config file + test file template
+#
+# 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
+# <http://www.gnu.org/licenses/>.
+
+from __future__ import print_function
+
+import re
+import sys
+
+if len(sys.argv) != 3:
+    print("syntax: %s CONFIG TEMPLATE" % sys.argv[0], file=sys.stderr)
+    sys.exit(1)
+
+config = sys.argv[1]
+template = sys.argv[2]
+
+
+def expand_config(config):
+    with open(config) as fh:
+        optprog = re.compile(r'''^#\w.*''')
+        groupstartprog = re.compile(r'''.*\[\s$''')
+        groupendprog = re.compile(r'''#\s*\].*$''')
+
+        group = False
+        for line in fh:
+            if optprog.match(line) is not None:
+                line = line[1:]
+                line = line.replace('"', '\\"')
+                print(line, end='')
+                if groupstartprog.match(line):
+                    group = True
+            elif group:
+                line = line.replace('"', '\\"')
+
+                if groupendprog.match(line):
+                    group = False
+
+                if line[0] == '#':
+                    line = line[1:]
+                    print(line, end='')
+
+
+def expand_template(template, config):
+    with open(template) as fh:
+        markerprog = re.compile(r'''\s*@CONFIG@\s*''')
+        for line in fh:
+            if markerprog.match(line) is not None:
+                print('   let conf = "', end='')
+                expand_config(config)
+                print('"')
+            else:
+                print(line, end='')
+
+
+expand_template(template, config)
diff --git a/src/Makefile.am b/src/Makefile.am
index cd955ee552..2956e4bf35 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -421,7 +421,8 @@ check-augeas: $(augeas_DATA) $(augeastest_DATA)
 	fi
 .PHONY: check-augeas
 
-AUG_GENTEST = $(PERL) $(top_srcdir)/build-aux/augeas-gentest.pl
+AUG_GENTEST_SCRIPT = $(top_srcdir)/build-aux/augeas-gentest.py
+AUG_GENTEST = $(RUNUTF8) $(PYTHON) $(AUG_GENTEST_SCRIPT)
 
 
 #
diff --git a/src/bhyve/Makefile.inc.am b/src/bhyve/Makefile.inc.am
index 195069872a..7e49a8bb5c 100644
--- a/src/bhyve/Makefile.inc.am
+++ b/src/bhyve/Makefile.inc.am
@@ -77,7 +77,7 @@ bhyve/virtbhyved.aug: remote/libvirtd.aug.in
 		$< > $@
 
 bhyve/test_virtbhyved.aug: remote/test_libvirtd.aug.in \
-		bhyve/virtbhyved.conf $(AUG_GENTEST)
+		bhyve/virtbhyved.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) bhyve/virtbhyved.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
@@ -91,7 +91,7 @@ augeas_DATA += bhyve/libvirtd_bhyve.aug
 augeastest_DATA += bhyve/test_libvirtd_bhyve.aug
 
 bhyve/test_libvirtd_bhyve.aug: bhyve/test_libvirtd_bhyve.aug.in \
-		$(srcdir)/bhyve/bhyve.conf $(AUG_GENTEST)
+		$(srcdir)/bhyve/bhyve.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) $(srcdir)/bhyve/bhyve.conf $< > $@
 
 endif WITH_BHYVE
diff --git a/src/interface/Makefile.inc.am b/src/interface/Makefile.inc.am
index baa85b4ba9..7f161c487f 100644
--- a/src/interface/Makefile.inc.am
+++ b/src/interface/Makefile.inc.am
@@ -97,7 +97,7 @@ interface/virtinterfaced.aug: remote/libvirtd.aug.in
 		$< > $@
 
 interface/test_virtinterfaced.aug: remote/test_libvirtd.aug.in \
-		interface/virtinterfaced.conf $(AUG_GENTEST)
+		interface/virtinterfaced.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) interface/virtinterfaced.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
diff --git a/src/libxl/Makefile.inc.am b/src/libxl/Makefile.inc.am
index c53396b7f3..3f35d1f2eb 100644
--- a/src/libxl/Makefile.inc.am
+++ b/src/libxl/Makefile.inc.am
@@ -111,7 +111,7 @@ libxl/virtxend.aug: remote/libvirtd.aug.in
 		$< > $@
 
 libxl/test_virtxend.aug: remote/test_libvirtd.aug.in \
-		libxl/virtxend.conf $(AUG_GENTEST)
+		libxl/virtxend.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) libxl/virtxend.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
@@ -125,7 +125,7 @@ augeas_DATA += libxl/libvirtd_libxl.aug
 augeastest_DATA += libxl/test_libvirtd_libxl.aug
 
 libxl/test_libvirtd_libxl.aug: libxl/test_libvirtd_libxl.aug.in \
-		$(srcdir)/libxl/libxl.conf $(AUG_GENTEST)
+		$(srcdir)/libxl/libxl.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) $(srcdir)/libxl/libxl.conf $< > $@
 
 INSTALL_DATA_DIRS += libxl
diff --git a/src/locking/Makefile.inc.am b/src/locking/Makefile.inc.am
index fae92a6e45..df0c51aaf6 100644
--- a/src/locking/Makefile.inc.am
+++ b/src/locking/Makefile.inc.am
@@ -219,7 +219,7 @@ endif WITH_SANLOCK
 if WITH_SANLOCK
 if WITH_QEMU
 locking/test_libvirt_sanlock.aug: locking/test_libvirt_sanlock.aug.in \
-		locking/qemu-sanlock.conf $(AUG_GENTEST)
+		locking/qemu-sanlock.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) locking/qemu-sanlock.conf $< > $@
 
 endif WITH_QEMU
@@ -227,12 +227,12 @@ endif WITH_SANLOCK
 
 if WITH_QEMU
 locking/test_libvirt_lockd.aug: locking/test_libvirt_lockd.aug.in \
-		locking/qemu-lockd.conf $(AUG_GENTEST)
+		locking/qemu-lockd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) locking/qemu-lockd.conf $< > $@
 endif WITH_QEMU
 
 locking/test_virtlockd.aug: locking/test_virtlockd.aug.in \
-		locking/virtlockd.conf $(AUG_GENTEST)
+		locking/virtlockd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) $(srcdir)/locking/virtlockd.conf $< > $@
 
 endif WITH_LIBVIRTD
diff --git a/src/logging/Makefile.inc.am b/src/logging/Makefile.inc.am
index 7e441dbffb..ce777e278a 100644
--- a/src/logging/Makefile.inc.am
+++ b/src/logging/Makefile.inc.am
@@ -101,7 +101,7 @@ augeas_DATA += logging/virtlogd.aug
 augeastest_DATA += logging/test_virtlogd.aug
 
 logging/test_virtlogd.aug: logging/test_virtlogd.aug.in \
-		logging/virtlogd.conf $(AUG_GENTEST)
+		logging/virtlogd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) $(srcdir)/logging/virtlogd.conf $< > $@
 
 endif WITH_LIBVIRTD
diff --git a/src/lxc/Makefile.inc.am b/src/lxc/Makefile.inc.am
index b4d560702c..0e12800886 100644
--- a/src/lxc/Makefile.inc.am
+++ b/src/lxc/Makefile.inc.am
@@ -164,7 +164,7 @@ lxc/virtlxcd.aug: remote/libvirtd.aug.in
 		$< > $@
 
 lxc/test_virtlxcd.aug: remote/test_libvirtd.aug.in \
-		lxc/virtlxcd.conf $(AUG_GENTEST)
+		lxc/virtlxcd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) lxc/virtlxcd.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
@@ -222,7 +222,7 @@ augeas_DATA += lxc/libvirtd_lxc.aug
 augeastest_DATA += lxc/test_libvirtd_lxc.aug
 
 lxc/test_libvirtd_lxc.aug: lxc/test_libvirtd_lxc.aug.in \
-		$(srcdir)/lxc/lxc.conf $(AUG_GENTEST)
+		$(srcdir)/lxc/lxc.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) $(srcdir)/lxc/lxc.conf $< > $@
 
 INSTALL_DATA_DIRS += lxc
diff --git a/src/network/Makefile.inc.am b/src/network/Makefile.inc.am
index 17467a65ad..0ac0e03957 100644
--- a/src/network/Makefile.inc.am
+++ b/src/network/Makefile.inc.am
@@ -104,7 +104,7 @@ network/virtnetworkd.aug: remote/libvirtd.aug.in
 		$< > $@
 
 network/test_virtnetworkd.aug: remote/test_libvirtd.aug.in \
-		network/virtnetworkd.conf $(AUG_GENTEST)
+		network/virtnetworkd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) network/virtnetworkd.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
diff --git a/src/node_device/Makefile.inc.am b/src/node_device/Makefile.inc.am
index eac7f92e88..a42cf8bc6a 100644
--- a/src/node_device/Makefile.inc.am
+++ b/src/node_device/Makefile.inc.am
@@ -120,7 +120,7 @@ node_device/virtnodedevd.aug: remote/libvirtd.aug.in
 		$< > $@
 
 node_device/test_virtnodedevd.aug: remote/test_libvirtd.aug.in \
-		node_device/virtnodedevd.conf $(AUG_GENTEST)
+		node_device/virtnodedevd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) node_device/virtnodedevd.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
diff --git a/src/nwfilter/Makefile.inc.am b/src/nwfilter/Makefile.inc.am
index 6acb45705c..45be065f7c 100644
--- a/src/nwfilter/Makefile.inc.am
+++ b/src/nwfilter/Makefile.inc.am
@@ -105,7 +105,7 @@ nwfilter/virtnwfilterd.aug: remote/libvirtd.aug.in
 		$< > $@
 
 nwfilter/test_virtnwfilterd.aug: remote/test_libvirtd.aug.in \
-		nwfilter/virtnwfilterd.conf $(AUG_GENTEST)
+		nwfilter/virtnwfilterd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) nwfilter/virtnwfilterd.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
diff --git a/src/qemu/Makefile.inc.am b/src/qemu/Makefile.inc.am
index 48fd0332ec..e9794520cf 100644
--- a/src/qemu/Makefile.inc.am
+++ b/src/qemu/Makefile.inc.am
@@ -169,7 +169,7 @@ qemu/virtqemud.aug: remote/libvirtd.aug.in
 		$< > $@
 
 qemu/test_virtqemud.aug: remote/test_libvirtd.aug.in \
-		qemu/virtqemud.conf $(AUG_GENTEST)
+		qemu/virtqemud.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) qemu/virtqemud.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
@@ -184,7 +184,7 @@ augeas_DATA += qemu/libvirtd_qemu.aug
 augeastest_DATA += qemu/test_libvirtd_qemu.aug
 
 qemu/test_libvirtd_qemu.aug: qemu/test_libvirtd_qemu.aug.in \
-		$(srcdir)/qemu/qemu.conf $(AUG_GENTEST)
+		$(srcdir)/qemu/qemu.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) $(srcdir)/qemu/qemu.conf $< > $@
 
 INSTALL_DATA_DIRS += qemu
diff --git a/src/remote/Makefile.inc.am b/src/remote/Makefile.inc.am
index 5a5c90a922..b42b700488 100644
--- a/src/remote/Makefile.inc.am
+++ b/src/remote/Makefile.inc.am
@@ -285,7 +285,7 @@ remote/virtproxyd.aug: remote/libvirtd.aug.in
 		$< > $@
 
 remote/test_libvirtd.aug: remote/test_libvirtd.aug.in \
-		remote/libvirtd.conf $(AUG_GENTEST)
+		remote/libvirtd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) remote/libvirtd.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
@@ -298,7 +298,7 @@ remote/test_libvirtd.aug: remote/test_libvirtd.aug.in \
 		> $@ || rm -f $@
 
 remote/test_virtproxyd.aug: remote/test_libvirtd.aug.in \
-		remote/virtproxyd.conf $(AUG_GENTEST)
+		remote/virtproxyd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) remote/virtproxyd.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
diff --git a/src/secret/Makefile.inc.am b/src/secret/Makefile.inc.am
index 76bc67418c..1020fb64c8 100644
--- a/src/secret/Makefile.inc.am
+++ b/src/secret/Makefile.inc.am
@@ -93,7 +93,7 @@ secret/virtsecretd.aug: remote/libvirtd.aug.in
 		$< > $@
 
 secret/test_virtsecretd.aug: remote/test_libvirtd.aug.in \
-		secret/virtsecretd.conf $(AUG_GENTEST)
+		secret/virtsecretd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) secret/virtsecretd.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
diff --git a/src/storage/Makefile.inc.am b/src/storage/Makefile.inc.am
index 4dccb14ac1..28ddfeec72 100644
--- a/src/storage/Makefile.inc.am
+++ b/src/storage/Makefile.inc.am
@@ -197,7 +197,7 @@ storage/virtstoraged.aug: remote/libvirtd.aug.in
 		$< > $@
 
 storage/test_virtstoraged.aug: remote/test_libvirtd.aug.in \
-		storage/virtstoraged.conf $(AUG_GENTEST)
+		storage/virtstoraged.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) storage/virtstoraged.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
diff --git a/src/vbox/Makefile.inc.am b/src/vbox/Makefile.inc.am
index 178c360b99..1f2bcb5488 100644
--- a/src/vbox/Makefile.inc.am
+++ b/src/vbox/Makefile.inc.am
@@ -117,7 +117,7 @@ vbox/virtvboxd.aug: remote/libvirtd.aug.in
 		$< > $@
 
 vbox/test_virtvboxd.aug: remote/test_libvirtd.aug.in \
-		vbox/virtvboxd.conf $(AUG_GENTEST)
+		vbox/virtvboxd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) vbox/virtvboxd.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
diff --git a/src/vz/Makefile.inc.am b/src/vz/Makefile.inc.am
index f56fceb8f7..fa7b8b79dd 100644
--- a/src/vz/Makefile.inc.am
+++ b/src/vz/Makefile.inc.am
@@ -91,7 +91,7 @@ vz/virtvzd.aug: remote/libvirtd.aug.in
 		$< > $@
 
 vz/test_virtvzd.aug: remote/test_libvirtd.aug.in \
-		vz/virtvzd.conf $(AUG_GENTEST)
+		vz/virtvzd.conf $(AUG_GENTEST_SCRIPT)
 	$(AM_V_GEN)$(AUG_GENTEST) vz/virtvzd.conf \
 		$(srcdir)/remote/test_libvirtd.aug.in | \
 		$(SED) \
-- 
2.21.0




More information about the libvir-list mailing list