mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-12-10 16:24:37 +03:00
src: rewrite driver impl checker in Python
As part of a goal to eliminate Perl from libvirt build tools, rewrite the check-driverimpls.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. Tested-by: Cole Robinson <crobinso@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
103
scripts/check-driverimpls.py
Executable file
103
scripts/check-driverimpls.py
Executable file
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2013-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 checkdriverimpls(filename):
|
||||
intable = False
|
||||
mainprefix = None
|
||||
|
||||
errs = False
|
||||
with open(filename, "r") as fh:
|
||||
lineno = 0
|
||||
for line in fh:
|
||||
lineno = lineno + 1
|
||||
if intable:
|
||||
if line.find("}") != -1:
|
||||
intable = False
|
||||
mainprefix = None
|
||||
continue
|
||||
|
||||
m = re.search(r'''\.(\w+)\s*=\s*(\w+),?''', line)
|
||||
if m is not None:
|
||||
api = m.group(1)
|
||||
impl = m.group(2)
|
||||
|
||||
if api in ["no", "name"]:
|
||||
continue
|
||||
if impl in ["NULL"]:
|
||||
continue
|
||||
|
||||
suffix = impl
|
||||
prefix = re.sub(r'''^([a-z]+)(.*?)$''', r'''\1''', impl)
|
||||
|
||||
if mainprefix is not None:
|
||||
if mainprefix != prefix:
|
||||
print(("%s:%d Bad prefix '%s' for API '%s', " +
|
||||
"expecting '%s'") %
|
||||
(filename, lineno, prefix, api, mainprefix),
|
||||
file=sys.stderr)
|
||||
errs = True
|
||||
else:
|
||||
mainprefix = prefix
|
||||
|
||||
if not api.startswith(mainprefix):
|
||||
suffix = re.sub(r'''^[a-z]+''', "", suffix)
|
||||
suffix = re.sub(r'''^([A-Z]+)''',
|
||||
lambda m: m.group(1).lower(), suffix)
|
||||
|
||||
if api != suffix:
|
||||
want = api
|
||||
if want.startswith("nwf"):
|
||||
want = "NWF" + want[3:]
|
||||
|
||||
if not api.startswith(mainprefix):
|
||||
want = re.sub(r'''^([a-z])''',
|
||||
lambda m: m.group(1).upper(), want)
|
||||
want = mainprefix + want
|
||||
|
||||
print(("%s:%d Bad impl name '%s' for API " +
|
||||
"'%s', expecting '%s'") %
|
||||
(filename, lineno, impl, api, want),
|
||||
file=sys.stderr)
|
||||
errs = True
|
||||
else:
|
||||
m = re.search(r'''^(?:static\s+)?(vir(?:\w+)?Driver)''' +
|
||||
r'''\s+(?!.*;)''', line)
|
||||
if m is not None:
|
||||
drv = m.group(1)
|
||||
if drv in [
|
||||
"virNWFilterCallbackDriver",
|
||||
"virNWFilterTechDriver",
|
||||
"virConnectDriver"]:
|
||||
continue
|
||||
intable = True
|
||||
|
||||
return errs
|
||||
|
||||
|
||||
status = 0
|
||||
for filename in sys.argv[1:]:
|
||||
if checkdriverimpls(filename):
|
||||
status = 1
|
||||
sys.exit(status)
|
||||
Reference in New Issue
Block a user