As part of an goal to eliminate Perl from libvirt build tools,
rewrite the check-symsorting.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é <berrange(a)redhat.com>
---
src/Makefile.am | 6 +--
src/check-symsorting.pl | 106 -------------------------------------
src/check-symsorting.py | 112 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 115 insertions(+), 109 deletions(-)
delete mode 100755 src/check-symsorting.pl
create mode 100755 src/check-symsorting.py
diff --git a/src/Makefile.am b/src/Makefile.am
index 2eb4387231..f30b085d07 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -285,12 +285,12 @@ check-symfile:
check-admin-symfile:
endif ! WITH_LINUX
check-symsorting:
- $(AM_V_GEN)$(PERL) $(srcdir)/check-symsorting.pl \
+ $(AM_V_GEN)$(PYTHON) $(srcdir)/check-symsorting.py \
$(srcdir) $(SYM_FILES)
check-admin-symsorting:
- $(AM_V_GEN)$(PERL) $(srcdir)/check-symsorting.pl \
+ $(AM_V_GEN)$(PYTHON) $(srcdir)/check-symsorting.py \
$(srcdir) $(ADMIN_SYM_FILES)
-EXTRA_DIST += check-symfile.pl check-symsorting.pl
+EXTRA_DIST += check-symfile.pl check-symsorting.py
# Keep this list synced with RPC_PROBE_FILES
PROTOCOL_STRUCTS = \
diff --git a/src/check-symsorting.pl b/src/check-symsorting.pl
deleted file mode 100755
index 51e38bdedb..0000000000
--- a/src/check-symsorting.pl
+++ /dev/null
@@ -1,106 +0,0 @@
-#!/usr/bin/env perl
-
-# Copyright (C) 2012-2013 Red Hat, Inc.
-#
-# 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 SRCDIR SYMFILE..." unless int(@ARGV) >= 2;
-
-my $ret = 0;
-my $srcdir = shift;
-my $lastgroup = undef;
-foreach my $symfile (@ARGV) {
- open SYMFILE, $symfile or die "cannot read $symfile: $!";
-
- my $line = 0;
- my $groupfile = "";
- my @group;
-
- while (<SYMFILE>) {
- chomp;
-
- if (/^#\s*((\w+\/)*(\w+\.h))\s*$/) {
- $groupfile = $1;
- } elsif (/^#/) {
- # Ignore comments
- } elsif (/^\s*$/) {
- if (@group) {
- &check_sorting(\@group, $symfile, $line, $groupfile);
- }
- @group = ();
- $line = $.;
- } else {
- $_ =~ s/;//;
- push @group, $_;
- }
- }
-
- close SYMFILE;
- if (@group) {
- &check_sorting(\@group, $symfile, $line, $groupfile);
- }
- $lastgroup = undef;
-}
-
-sub check_sorting {
- my $group = shift;
- my $symfile = shift;
- my $line = shift;
- my $groupfile = shift;
-
- my @group = @{$group};
- my @sorted = sort { lc $a cmp lc $b } @group;
- my $sorted = 1;
- my $first;
- my $last;
-
- # Check that groups are in order and groupfile exists
- if (defined $lastgroup && lc $lastgroup ge lc $groupfile) {
- print "Symbol block at $symfile:$line: block not sorted\n";
- print "Move $groupfile block before $lastgroup block\n";
- print "\n";
- $ret = 1;
- }
- if (! -e "$srcdir/$groupfile") {
- print "Symbol block at $symfile:$line: $groupfile not found\n";
- print "\n";
- $ret = 1;
- }
- $lastgroup = $groupfile;
-
- # Check that symbols within a group are in order
- for (my $i = 0 ; $i <= $#sorted ; $i++) {
- if ($sorted[$i] ne $group[$i]) {
- $first = $i unless defined $first;
- $last = $i;
- $sorted = 0;
- }
- }
- if (!$sorted) {
- @group = splice @group, $first, ($last-$first+1);
- @sorted = splice @sorted, $first, ($last-$first+1);
- print "Symbol block at $symfile:$line: symbols not sorted\n";
- print map { " " . $_ . "\n" } @group;
- print "Correct ordering\n";
- print map { " " . $_ . "\n" } @sorted;
- print "\n";
- $ret = 1;
- }
-}
-
-exit $ret;
diff --git a/src/check-symsorting.py b/src/check-symsorting.py
new file mode 100755
index 0000000000..a1d16a4c43
--- /dev/null
+++ b/src/check-symsorting.py
@@ -0,0 +1,112 @@
+#!/usr/bin/env python
+
+# Copyright (C) 2012-2019 Red Hat, Inc.
+#
+# 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 os.path
+import re
+import sys
+
+if len(sys.argv) < 3:
+ print("syntax: %s SRCDIR SYMFILE..." % sys.argv[0], file=sys.stderr)
+ sys.exit(1)
+
+def check_sorting(group, symfile, line, groupfile, lastgroup):
+ sortedgroup = sorted(group, key=str.lower)
+ issorted = True
+ first = None
+ last = None
+
+ err = False
+ # Check that groups are in order and groupfile exists
+ if lastgroup is not None and lastgroup.lower() > groupfile.lower():
+ print("Symbol block at %s:%s: block not sorted" % (symfile, line),
file=sys.stderr)
+ print("Move %s block before %s block" % (groupfile, lastgroup),
file=sys.stderr)
+ print("", file=sys.stderr)
+ err = True
+
+ if not os.path.exists(os.path.join(srcdir, groupfile)):
+ print("Symbol block at %s:%s: %s not found" % (symfile, line,
groupfile), file=sys.stderr)
+ print("", file=sys.stderr)
+ err = True
+
+ # Check that symbols within a group are in order
+ for i in range(len(group)):
+ if sortedgroup[i] != group[i]:
+ if first is None:
+ first = i
+
+ last = i
+ issorted = False
+
+ if not issorted:
+ actual = group[first:(last-first+1)]
+ expect = sortedgroup[first:(last-first+1)]
+ print("Symbol block at %s:%s: symbols not sorted" % (symfile, line),
file=sys.stderr)
+ for g in actual:
+ print(" %s" % g, file=sys.stderr)
+ print("Correct ordering", file=sys.stderr)
+ for g in expect:
+ print(" %s" % g, file=sys.stderr)
+ print("", file=sys.stderr)
+ err = True
+
+ return err
+
+
+ret = 0
+srcdir = sys.argv[1]
+lastgroup = None
+for symfile in sys.argv[2:]:
+ with open(symfile, "r") as fh:
+ lineno = 0
+ groupfile = ""
+ group = []
+ thisline = 0
+
+ filenameprog =
re.compile(r'''^#\s*((\w+\/)*(\w+\.h))\s*$''')
+
+ for line in fh:
+ thisline = thisline + 1
+ line = line.strip()
+
+ filenamematch = filenameprog.match(line)
+ if filenamematch is not None:
+ groupfile = filenamematch.group(1)
+ elif line == "":
+ if len(group) > 0:
+ if check_sorting(group, symfile, lineno, groupfile, lastgroup):
+ ret = 1
+
+ group = []
+ lineno = thisline
+ lastgroup = groupfile
+ elif line[0] == '#':
+ # Ignore comments
+ pass
+ else:
+ line = line.strip(";")
+ group.append(line)
+
+ if len(group) > 0:
+ if check_sorting(group, symfile, lineno, groupfile, lastgroup):
+ ret = 1
+
+ lastgroup = None
+
+sys.exit(ret)
--
2.21.0