mirror of
https://github.com/libvirt/libvirt.git
synced 2025-01-08 07:03:19 -06:00
build-aux: rewrite duplicate header checker in Python
As part of an goal to eliminate Perl from libvirt build tools, rewrite the prohibit-duplicate-header.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. Reviewed-by: Ján Tomko <jtomko@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
parent
2816fe2e84
commit
7909359d60
@ -50,7 +50,7 @@ EXTRA_DIST = \
|
||||
build-aux/header-ifdef.pl \
|
||||
scripts/minimize-po.py \
|
||||
build-aux/mock-noinline.pl \
|
||||
build-aux/prohibit-duplicate-header.pl \
|
||||
scripts/prohibit-duplicate-header.py \
|
||||
build-aux/syntax-check.mk \
|
||||
build-aux/useless-if-before-free \
|
||||
build-aux/vc-list-files \
|
||||
|
@ -1,26 +0,0 @@
|
||||
#!/usr/bin/env perl
|
||||
|
||||
use strict;
|
||||
|
||||
my $file = " ";
|
||||
my $ret = 0;
|
||||
my %includes = ( );
|
||||
my $lineno = 0;
|
||||
|
||||
while (<>) {
|
||||
if (not $file eq $ARGV) {
|
||||
%includes = ( );
|
||||
$file = $ARGV;
|
||||
$lineno = 0;
|
||||
}
|
||||
$lineno++;
|
||||
if (/^# *include *[<"]([^>"]*\.h)[">]/) {
|
||||
$includes{$1}++;
|
||||
if ($includes{$1} == 2) {
|
||||
$ret = 1;
|
||||
print STDERR "$ARGV:$lineno: $_";
|
||||
print STDERR "Do not include a header more than once per file\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
exit $ret;
|
@ -2166,8 +2166,8 @@ endif
|
||||
|
||||
# Don't include duplicate header in the source (either *.c or *.h)
|
||||
prohibit-duplicate-header:
|
||||
$(AM_V_GEN)$(VC_LIST_EXCEPT) | $(GREP) '\.[chx]$$' | xargs \
|
||||
$(PERL) -W $(top_srcdir)/build-aux/prohibit-duplicate-header.pl
|
||||
$(AM_V_GEN)$(VC_LIST_EXCEPT) | $(GREP) '\.[chx]$$' | $(RUNUTF8) xargs \
|
||||
$(PYTHON) $(top_srcdir)/scripts/prohibit-duplicate-header.py
|
||||
|
||||
spacing-check:
|
||||
$(AM_V_GEN)$(VC_LIST) | $(GREP) '\.c$$' | xargs \
|
||||
|
56
scripts/prohibit-duplicate-header.py
Normal file
56
scripts/prohibit-duplicate-header.py
Normal file
@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016-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 re
|
||||
import sys
|
||||
|
||||
|
||||
def check_file(filename):
|
||||
includes = {}
|
||||
lineno = 0
|
||||
errs = False
|
||||
with open(filename, "r") as fh:
|
||||
for line in fh:
|
||||
lineno = lineno + 1
|
||||
|
||||
headermatch = re.search(r'''^# *include *[<"]([^>"]*\.h)[">]''', line)
|
||||
if headermatch is not None:
|
||||
inc = headermatch.group(1)
|
||||
|
||||
if inc in includes:
|
||||
print("%s:%d: %s" % (filename, lineno, inc),
|
||||
file=sys.stderr)
|
||||
errs = True
|
||||
else:
|
||||
includes[inc] = True
|
||||
|
||||
return errs
|
||||
|
||||
|
||||
ret = 0
|
||||
|
||||
for filename in sys.argv[1:]:
|
||||
if check_file(filename):
|
||||
ret = 1
|
||||
|
||||
if ret == 1:
|
||||
print("Do not include a header more than once per file", file=sys.stderr)
|
||||
|
||||
sys.exit(ret)
|
Loading…
Reference in New Issue
Block a user