mirror of
https://gitlab.com/libvirt/libvirt-python.git
synced 2025-08-02 04:21:59 +03:00
Re-arrange python generator to make it clear what's auto-generated
* README: New file describing what each file is used for * livvirt-override.c, libvirt-override.py, libvirt-override-api.xml, libvirt-override-virConnect.py: Manually written code overriding the generator * typewrappers.c, typewrappers.h: Data type wrappers * generator.py: Automatically pre-prend contents of libvirt-override.py to generated libvirt.py. Output into libvirt.py directly instead of libvirtclass.py. Don't generate libvirtclass.txt at all. Write C files into libvirt.c/.h directly * Makefile.am: Remove rule for creating libvirt.py from libvirt-override.py and libvirtclass.py, since generator.py does it directly
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@ -7,9 +7,7 @@ Makefile.in
|
|||||||
*.loT
|
*.loT
|
||||||
libvirt.py
|
libvirt.py
|
||||||
libvirt-export.c
|
libvirt-export.c
|
||||||
libvirtclass.txt
|
libvirt.c
|
||||||
libvirt-py.[ch]
|
libvirt.h
|
||||||
libvirtclass.py
|
|
||||||
gen_prog
|
|
||||||
*.pyc
|
*.pyc
|
||||||
generated.stamp
|
generated.stamp
|
||||||
|
34
Makefile.am
34
Makefile.am
@ -13,16 +13,15 @@ DOCS_DIR = $(datadir)/doc/libvirt-python-$(LIBVIRT_VERSION)
|
|||||||
|
|
||||||
DOCS = ${srcdir}/TODO
|
DOCS = ${srcdir}/TODO
|
||||||
|
|
||||||
CLASSES_EXTRA = virConnect.py
|
CLASSES_EXTRA = libvirt-override-virConnect.py
|
||||||
|
|
||||||
EXTRA_DIST = \
|
EXTRA_DIST = \
|
||||||
libvir.c \
|
generator.py \
|
||||||
types.c \
|
typewrappers.c \
|
||||||
generator.py \
|
typewrappers.h \
|
||||||
libvirt_wrap.h \
|
libvirt-override.c \
|
||||||
libvirt.py \
|
libvirt-override.py \
|
||||||
libvir.py \
|
libvirt-override-api.xml \
|
||||||
libvirt-python-api.xml \
|
|
||||||
$(CLASSES_EXTRA) \
|
$(CLASSES_EXTRA) \
|
||||||
$(DOCS)
|
$(DOCS)
|
||||||
|
|
||||||
@ -33,7 +32,7 @@ all-local: libvirt.py
|
|||||||
|
|
||||||
python_LTLIBRARIES = libvirtmod.la
|
python_LTLIBRARIES = libvirtmod.la
|
||||||
|
|
||||||
libvirtmod_la_SOURCES = libvir.c types.c libvirt-py.c libvirt-py.h
|
libvirtmod_la_SOURCES = libvirt-override.c typewrappers.c libvirt.c libvirt.h
|
||||||
# Python <= 2.4 header files contain a redundant decl, hence we
|
# Python <= 2.4 header files contain a redundant decl, hence we
|
||||||
# need extra flags here
|
# need extra flags here
|
||||||
libvirtmod_la_CFLAGS = @WARN_PYTHON_CFLAGS@
|
libvirtmod_la_CFLAGS = @WARN_PYTHON_CFLAGS@
|
||||||
@ -44,12 +43,11 @@ libvirtmod_la_LIBADD = $(mylibs) \
|
|||||||
@CYGWIN_EXTRA_LIBADD@ @CYGWIN_EXTRA_PYTHON_LIBADD@
|
@CYGWIN_EXTRA_LIBADD@ @CYGWIN_EXTRA_PYTHON_LIBADD@
|
||||||
|
|
||||||
GENERATE = generator.py
|
GENERATE = generator.py
|
||||||
API_DESC = $(top_srcdir)/docs/libvirt-api.xml $(srcdir)/libvirt-python-api.xml
|
API_DESC = $(top_srcdir)/docs/libvirt-api.xml $(srcdir)/libvirt-override-api.xml
|
||||||
GENERATED= libvirt-export.c \
|
GENERATED= libvirt-export.c \
|
||||||
libvirtclass.txt \
|
libvirt.c \
|
||||||
libvirt-py.c \
|
libvirt.h \
|
||||||
libvirt-py.h \
|
libvirt.py
|
||||||
libvirtclass.py
|
|
||||||
|
|
||||||
generated.stamp: $(srcdir)/$(GENERATE) $(API_DESC)
|
generated.stamp: $(srcdir)/$(GENERATE) $(API_DESC)
|
||||||
$(PYTHON) $(srcdir)/$(GENERATE) $(srcdir)
|
$(PYTHON) $(srcdir)/$(GENERATE) $(srcdir)
|
||||||
@ -57,10 +55,6 @@ generated.stamp: $(srcdir)/$(GENERATE) $(API_DESC)
|
|||||||
|
|
||||||
$(GENERATED): generated.stamp
|
$(GENERATED): generated.stamp
|
||||||
|
|
||||||
libvirt.py: $(srcdir)/libvir.py $(GENERATED) $(CLASSES_EXTRA)
|
|
||||||
cat $(srcdir)/libvir.py libvirtclass.py > $@-t
|
|
||||||
mv $@-t $@
|
|
||||||
|
|
||||||
$(libvirtmod_la_OBJECTS): $(GENERATED)
|
$(libvirtmod_la_OBJECTS): $(GENERATED)
|
||||||
|
|
||||||
install-data-local:
|
install-data-local:
|
||||||
@ -73,7 +67,7 @@ install-data-local:
|
|||||||
uninstall-local:
|
uninstall-local:
|
||||||
rm -f $(DESTDIR)$(pythondir)/libvirt.py
|
rm -f $(DESTDIR)$(pythondir)/libvirt.py
|
||||||
|
|
||||||
CLEANFILES= $(GENERATED) generated.stamp libvirt.py
|
CLEANFILES= $(GENERATED) generated.stamp
|
||||||
|
|
||||||
else
|
else
|
||||||
all:
|
all:
|
||||||
|
27
README
Normal file
27
README
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
libvirt Python Bindings README
|
||||||
|
==============================
|
||||||
|
|
||||||
|
Most of the libvirt python binding code is automatically generated
|
||||||
|
using the script generator.py, and the API description from
|
||||||
|
docs/libvirt-api.xml
|
||||||
|
|
||||||
|
|
||||||
|
Manually written files:
|
||||||
|
|
||||||
|
- libvirt-override.c: methods where the C binding needs to be hand crafted
|
||||||
|
- libvirt-override.py: global methods where the C and python bindings have different args
|
||||||
|
- libvirt-override-api.xml: methods where the auto-extracted API docs are not
|
||||||
|
suitable for python auto-generator. Overriding this if the method is going
|
||||||
|
into libvirt-override.c, but we still want auto-generated libvirt-override.py
|
||||||
|
- libvirt-override-virConnect.py: virConnect class methods
|
||||||
|
- typewrappers.h,.c: Python object wrappers for each libvirt C object
|
||||||
|
|
||||||
|
|
||||||
|
Auto-generated files:
|
||||||
|
|
||||||
|
- libvirt.py: The main python binding. Comprises auto-generated code, along
|
||||||
|
with contents from libvirt-override.py and libvirt-override-virConnect.py
|
||||||
|
- libvirt.c, libvirt.h: The C glue layer for the python binding. Comprises
|
||||||
|
auto-generated code, along with libvirt-override.c
|
||||||
|
- libvirt-export.c: List of auto-generated C methods, included into
|
||||||
|
the libvirt-override.c method table
|
62
generator.py
62
generator.py
@ -287,8 +287,8 @@ foreign_encoding_args = (
|
|||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
#
|
#
|
||||||
# This part writes the C <-> Python stubs libvirt2-py.[ch] and
|
# This part writes the C <-> Python stubs libvirt.[ch] and
|
||||||
# the table libxml2-export.c to add when registrering the Python module
|
# the table libvirt-export.c to add when registrering the Python module
|
||||||
#
|
#
|
||||||
#######################################################################
|
#######################################################################
|
||||||
|
|
||||||
@ -555,7 +555,7 @@ def buildStubs():
|
|||||||
|
|
||||||
py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
|
py_types['pythonObject'] = ('O', "pythonObject", "pythonObject", "pythonObject")
|
||||||
try:
|
try:
|
||||||
f = open(os.path.join(srcPref,"libvirt-python-api.xml"))
|
f = open(os.path.join(srcPref,"libvirt-override-api.xml"))
|
||||||
data = f.read()
|
data = f.read()
|
||||||
(parser, target) = getparser()
|
(parser, target) = getparser()
|
||||||
parser.feed(data)
|
parser.feed(data)
|
||||||
@ -564,22 +564,22 @@ def buildStubs():
|
|||||||
print file, ":", msg
|
print file, ":", msg
|
||||||
|
|
||||||
|
|
||||||
print "Found %d functions in libvirt-python-api.xml" % (
|
print "Found %d functions in libvirt-override-api.xml" % (
|
||||||
len(functions.keys()) - n)
|
len(functions.keys()) - n)
|
||||||
nb_wrap = 0
|
nb_wrap = 0
|
||||||
failed = 0
|
failed = 0
|
||||||
skipped = 0
|
skipped = 0
|
||||||
|
|
||||||
include = open("libvirt-py.h", "w")
|
include = open("libvirt.h", "w")
|
||||||
include.write("/* Generated */\n\n")
|
include.write("/* Generated */\n\n")
|
||||||
export = open("libvirt-export.c", "w")
|
export = open("libvirt-export.c", "w")
|
||||||
export.write("/* Generated */\n\n")
|
export.write("/* Generated */\n\n")
|
||||||
wrapper = open("libvirt-py.c", "w")
|
wrapper = open("libvirt.c", "w")
|
||||||
wrapper.write("/* Generated */\n\n")
|
wrapper.write("/* Generated */\n\n")
|
||||||
wrapper.write("#include <Python.h>\n")
|
wrapper.write("#include <Python.h>\n")
|
||||||
wrapper.write("#include <libvirt/libvirt.h>\n")
|
wrapper.write("#include <libvirt/libvirt.h>\n")
|
||||||
wrapper.write("#include \"libvirt_wrap.h\"\n")
|
wrapper.write("#include \"typewrappers.h\"\n")
|
||||||
wrapper.write("#include \"libvirt-py.h\"\n\n")
|
wrapper.write("#include \"libvirt.h\"\n\n")
|
||||||
for function in functions.keys():
|
for function in functions.keys():
|
||||||
ret = print_function_wrapper(function, wrapper, export, include)
|
ret = print_function_wrapper(function, wrapper, export, include)
|
||||||
if ret < 0:
|
if ret < 0:
|
||||||
@ -931,12 +931,30 @@ def buildWrappers():
|
|||||||
info = (0, func, name, ret, args, file)
|
info = (0, func, name, ret, args, file)
|
||||||
function_classes['None'].append(info)
|
function_classes['None'].append(info)
|
||||||
|
|
||||||
classes = open("libvirtclass.py", "w")
|
classes = open("libvirt.py", "w")
|
||||||
|
|
||||||
txt = open("libvirtclass.txt", "w")
|
extra = open(os.path.join(srcPref,"libvirt-override.py"), "r")
|
||||||
txt.write(" Generated Classes for libvir-python\n\n")
|
classes.write("#!/usr/bin/python -i\n")
|
||||||
|
classes.write("#\n")
|
||||||
|
classes.write("# WARNING WARNING WARNING WARNING\n")
|
||||||
|
classes.write("#\n")
|
||||||
|
classes.write("# This file is automatically written by generator.py. Any changes\n")
|
||||||
|
classes.write("# made here will be lost.\n")
|
||||||
|
classes.write("#\n")
|
||||||
|
classes.write("# To change the manually written methods edit libvirt-override.py\n")
|
||||||
|
classes.write("# To change the automatically written methods edit generator.py\n")
|
||||||
|
classes.write("#\n")
|
||||||
|
classes.write("# WARNING WARNING WARNING WARNING\n")
|
||||||
|
classes.write("#\n")
|
||||||
|
classes.writelines(extra.readlines())
|
||||||
|
classes.write("#\n")
|
||||||
|
classes.write("# WARNING WARNING WARNING WARNING\n")
|
||||||
|
classes.write("#\n")
|
||||||
|
classes.write("# Automatically written part of python bindings for libvirt\n")
|
||||||
|
classes.write("#\n")
|
||||||
|
classes.write("# WARNING WARNING WARNING WARNING\n")
|
||||||
|
extra.close()
|
||||||
|
|
||||||
txt.write("#\n# Global functions of the module\n#\n\n")
|
|
||||||
if function_classes.has_key("None"):
|
if function_classes.has_key("None"):
|
||||||
flist = function_classes["None"]
|
flist = function_classes["None"]
|
||||||
flist.sort(functionCompare)
|
flist.sort(functionCompare)
|
||||||
@ -945,10 +963,8 @@ def buildWrappers():
|
|||||||
(index, func, name, ret, args, file) = info
|
(index, func, name, ret, args, file) = info
|
||||||
if file != oldfile:
|
if file != oldfile:
|
||||||
classes.write("#\n# Functions from module %s\n#\n\n" % file)
|
classes.write("#\n# Functions from module %s\n#\n\n" % file)
|
||||||
txt.write("\n# functions from module %s\n" % file)
|
|
||||||
oldfile = file
|
oldfile = file
|
||||||
classes.write("def %s(" % func)
|
classes.write("def %s(" % func)
|
||||||
txt.write("%s()\n" % func);
|
|
||||||
n = 0
|
n = 0
|
||||||
for arg in args:
|
for arg in args:
|
||||||
if n != 0:
|
if n != 0:
|
||||||
@ -1025,14 +1041,11 @@ def buildWrappers():
|
|||||||
|
|
||||||
classes.write("\n");
|
classes.write("\n");
|
||||||
|
|
||||||
txt.write("\n\n#\n# Set of classes of the module\n#\n\n")
|
|
||||||
for classname in classes_list:
|
for classname in classes_list:
|
||||||
if classname == "None":
|
if classname == "None":
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if classes_ancestor.has_key(classname):
|
if classes_ancestor.has_key(classname):
|
||||||
txt.write("\n\nClass %s(%s)\n" % (classname,
|
|
||||||
classes_ancestor[classname]))
|
|
||||||
classes.write("class %s(%s):\n" % (classname,
|
classes.write("class %s(%s):\n" % (classname,
|
||||||
classes_ancestor[classname]))
|
classes_ancestor[classname]))
|
||||||
classes.write(" def __init__(self, _obj=None):\n")
|
classes.write(" def __init__(self, _obj=None):\n")
|
||||||
@ -1044,7 +1057,6 @@ def buildWrappers():
|
|||||||
classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
|
classes.write(" %s.__init__(self, _obj=_obj)\n\n" % (
|
||||||
classes_ancestor[classname]))
|
classes_ancestor[classname]))
|
||||||
else:
|
else:
|
||||||
txt.write("Class %s()\n" % (classname))
|
|
||||||
classes.write("class %s:\n" % (classname))
|
classes.write("class %s:\n" % (classname))
|
||||||
if classname in [ "virDomain", "virNetwork", "virInterface", "virStoragePool", "virStorageVol", "virNodeDevice", "virSecret" ]:
|
if classname in [ "virDomain", "virNetwork", "virInterface", "virStoragePool", "virStorageVol", "virNodeDevice", "virSecret" ]:
|
||||||
classes.write(" def __init__(self, conn, _obj=None):\n")
|
classes.write(" def __init__(self, conn, _obj=None):\n")
|
||||||
@ -1084,16 +1096,13 @@ def buildWrappers():
|
|||||||
if file != oldfile:
|
if file != oldfile:
|
||||||
if file == "python_accessor":
|
if file == "python_accessor":
|
||||||
classes.write(" # accessors for %s\n" % (classname))
|
classes.write(" # accessors for %s\n" % (classname))
|
||||||
txt.write(" # accessors\n")
|
|
||||||
else:
|
else:
|
||||||
classes.write(" #\n")
|
classes.write(" #\n")
|
||||||
classes.write(" # %s functions from module %s\n" % (
|
classes.write(" # %s functions from module %s\n" % (
|
||||||
classname, file))
|
classname, file))
|
||||||
txt.write("\n # functions from module %s\n" % file)
|
|
||||||
classes.write(" #\n\n")
|
classes.write(" #\n\n")
|
||||||
oldfile = file
|
oldfile = file
|
||||||
classes.write(" def %s(self" % func)
|
classes.write(" def %s(self" % func)
|
||||||
txt.write(" %s()\n" % func);
|
|
||||||
n = 0
|
n = 0
|
||||||
for arg in args:
|
for arg in args:
|
||||||
if n != index:
|
if n != index:
|
||||||
@ -1316,7 +1325,7 @@ def buildWrappers():
|
|||||||
classes.write("\n");
|
classes.write("\n");
|
||||||
# Append "<classname>.py" to class def, iff it exists
|
# Append "<classname>.py" to class def, iff it exists
|
||||||
try:
|
try:
|
||||||
extra = open(classname + ".py", "r")
|
extra = open(os.path.join(srcPref,"libvirt-override-" + classname + ".py"), "r")
|
||||||
classes.write (" #\n")
|
classes.write (" #\n")
|
||||||
classes.write (" # %s methods from %s.py (hand coded)\n" % (classname,classname))
|
classes.write (" # %s methods from %s.py (hand coded)\n" % (classname,classname))
|
||||||
classes.write (" #\n")
|
classes.write (" #\n")
|
||||||
@ -1336,15 +1345,6 @@ def buildWrappers():
|
|||||||
classes.write("%s = %s\n" % (name,value))
|
classes.write("%s = %s\n" % (name,value))
|
||||||
classes.write("\n");
|
classes.write("\n");
|
||||||
|
|
||||||
if len(functions_skipped) != 0:
|
|
||||||
txt.write("\nFunctions skipped:\n")
|
|
||||||
for function in functions_skipped:
|
|
||||||
txt.write(" %s\n" % function)
|
|
||||||
if len(functions_failed) != 0:
|
|
||||||
txt.write("\nFunctions failed:\n")
|
|
||||||
for function in functions_failed:
|
|
||||||
txt.write(" %s\n" % function)
|
|
||||||
txt.close()
|
|
||||||
classes.close()
|
classes.close()
|
||||||
|
|
||||||
if buildStubs() < 0:
|
if buildStubs() < 0:
|
||||||
|
@ -19,8 +19,8 @@
|
|||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
#include "libvirt/libvirt.h"
|
#include "libvirt/libvirt.h"
|
||||||
#include "libvirt/virterror.h"
|
#include "libvirt/virterror.h"
|
||||||
#include "libvirt_wrap.h"
|
#include "typewrappers.h"
|
||||||
#include "libvirt-py.h"
|
#include "libvirt.h"
|
||||||
|
|
||||||
#ifndef __CYGWIN__
|
#ifndef __CYGWIN__
|
||||||
extern void initlibvirtmod(void);
|
extern void initlibvirtmod(void);
|
@ -1,8 +1,5 @@
|
|||||||
#!/usr/bin/python -u
|
|
||||||
#
|
#
|
||||||
# Those are the autogenerated Python bindings for libvirt.
|
# Manually written part of python bindings for libvirt
|
||||||
# Check python/generator.py in the source distribution of libvir
|
|
||||||
# to find out more about the generation process
|
|
||||||
#
|
#
|
||||||
|
|
||||||
# On cygwin, the DLL is called cygvirtmod.dll
|
# On cygwin, the DLL is called cygvirtmod.dll
|
||||||
@ -145,10 +142,3 @@ def eventInvokeTimeoutCallback (timer, callback, opaque):
|
|||||||
libvirtmod.virEventInvokeTimeoutCallback(timer, callback, opaque);
|
libvirtmod.virEventInvokeTimeoutCallback(timer, callback, opaque);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
|
|
||||||
#
|
|
||||||
# Everything before this line comes from libvir.py
|
|
||||||
# Everything after this line is automatically generated
|
|
||||||
#
|
|
||||||
# WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
|
|
@ -14,7 +14,7 @@
|
|||||||
* which has over 180 autoconf-style HAVE_* definitions. Shame on them. */
|
* which has over 180 autoconf-style HAVE_* definitions. Shame on them. */
|
||||||
#undef HAVE_PTHREAD_H
|
#undef HAVE_PTHREAD_H
|
||||||
|
|
||||||
#include "libvirt_wrap.h"
|
#include "typewrappers.h"
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
libvirt_intWrap(int val)
|
libvirt_intWrap(int val)
|
Reference in New Issue
Block a user