: libxl python binding
RFC PATCH 1/1 : libxl python binding
Attached patch is the first-cut of a libxl python binding. Presently only list_domains, domid_to_name, domain_shutdown and domain_destroy are implemented. The code takes advantage of Ian Campbells libxl IDL work to make 178 lines of python generate 4,460 lines of boilerplate.
All that remains is:
- support marshalling of other than integer types
- call auto-generated C destructors when python objects are free'd
- flatten out unions, structs etc. for libxl-type marshalling
- manually implement a xen.lowlevel.xl.ctx method for each libxl function
Here's an example of what current code can do:
$ sudo python
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
from xen.lowlevel import xl
xl
<module 'xen.lowlevel.xl' from '/usr/lib/python2.6/dist-packages/xen/lowlevel/xl.so'>
Quoted 1 line
Quoted 1 line
Quoted 1 line
dir(xl)
['Error', '__doc__', '__file__', '__name__', '__package__', 'ctx', 'device_console', 'device_disk', 'device_model_info', 'device_net2', 'device_nic', 'device_pci', 'device_vfb', 'device_vkb', 'diskinfo', 'domain_build_info', 'domain_build_state', 'domain_create_info', 'dominfo', 'file_reference', 'net2info', 'nicinfo', 'physinfo', 'poolinfo', 'sched_credit', 'vcpuinfo', 'version_info', 'vminfo']
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
ctx =
xl.ctx()
dir(ctx)
['domain_destroy', 'domain_shutdown', 'domid_to_name', 'list_domains']
Quoted 1 line
Quoted 1 line
Quoted 1 line
ctx.list_domains()
[<xen.lowlevel.xl.dominfo object at 0x7f6765492a08>, <xen.lowlevel.xl.dominfo object at 0x7f6765492a50>, <xen.lowlevel.xl.dominfo object at 0x7f6765492a98>]
Quoted 1 line
Quoted 1 line
Quoted 1 line
map(lambda x:x.domid, ctx.list_domains())
[0L, 133L, 136L]
Quoted 1 line
Quoted 1 line
Quoted 1 line
map(ctx.domid_to_name, map(lambda x:x.domid, ctx.list_domains()))
['Domain-0', 'netbsd', 'lenny']
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
ctx.domain_destroy(136)map(ctx.domid_to_name, map(lambda x:x.domid, ctx.list_domains()))
['Domain-0', 'netbsd']
-----
tools/python/genwrap.py | 178 +++++++++++++++++++++++ tools/python/xen/lowlevel/xl/xl.c | 269 ++++++++++++++++++++++++++++++++++++ tools/libxl/libxl.idl | 10 - tools/libxl/libxltypes.py | 23 ++- tools/python/Makefile | 1 tools/python/setup.py | 19 ++ 6 files changed, 486 insertions(+), 14 deletions(-)
----8<-----------------------------------------------------------------
tools/libxl/libxl.idl +5 -5
@@ -6,11 +6,11 @@ libxl_ctx = Builtin("ctx") libxl_uuid = Builtin("uuid") libxl_mac = Builtin("mac") -libxl_qemu_machine_type = Builtin("qemu_machine_type") -libxl_console_consback = Builtin("console_consback") -libxl_console_constype = Builtin("console_constype") -libxl_disk_phystype = Builtin("disk_phystype") -libxl_nic_type = Builtin("nic_type") +libxl_qemu_machine_type = Number("qemu_machine_type", namespace="libxl_") +libxl_console_consback = Number("console_consback", namespace="libxl_") +libxl_console_constype = Number("console_constype", namespace="libxl_") +libxl_disk_phystype = Number("disk_phystype", namespace="libxl_") +libxl_nic_type = Number("nic_type", namespace="libxl_") libxl_string_list = Builtin("string_list", destructor_fn="libxl_string_list_destroy", passby=PASS_BY_REFERENCE) libxl_key_value_list = Builtin("key_value_list", destructor_fn="libxl_key_value_list_destroy", passby=PASS_BY_REFERENCE)
tools/libxl/libxltypes.py +16 -7
@@ -14,10 +14,13 @@ class Type(object): if typename is None: # Anonymous type self.typename = None + self.rawname = None elif self.namespace is None: # e.g. system provided types self.typename = typename + self.rawname = typename else: self.typename = self.namespace + typename + self.rawname = typename if self.typename is not None: self.destructor_fn = kwargs.setdefault('destructor_fn', self.typename + "_destroy")
@@ -32,11 +35,17 @@ class Builtin(Type): kwargs.setdefault('destructor_fn', None) Type.__init__(self, typename, **kwargs) -class UInt(Type): +class Number(Builtin): + def __init__(self, ctype, **kwargs): + kwargs.setdefault('namespace', None) + kwargs.setdefault('destructor_fn', None) + Builtin.__init__(self, ctype, **kwargs) + +class UInt(Number): def __init__(self, w, **kwargs): kwargs.setdefault('namespace', None) kwargs.setdefault('destructor_fn', None) - Type.__init__(self, "uint%d_t" % w, **kwargs) + Number.__init__(self, "uint%d_t" % w, **kwargs) self.width = w
@@ -128,12 +137,12 @@ class Reference(Type): void = Builtin("void *", namespace = None) bool = Builtin("bool", namespace = None) -size_t = Builtin("size_t", namespace = None) +size_t = Number("size_t", namespace = None) -integer = Builtin("int", namespace = None) -unsigned_integer = Builtin("unsigned int", namespace = None) -unsigned = Builtin("unsigned int", namespace = None) -unsigned_long = Builtin("unsigned long", namespace = None) +integer = Number("int", namespace = None) +unsigned_integer = Number("unsigned int", namespace = None) +unsigned = Number("unsigned int", namespace = None) +unsigned_long = Number("unsigned long", namespace = None) uint8 = UInt(8) uint16 = UInt(16)
tools/python/Makefile +1 -0
@@ -59,6 +59,7 @@ refresh-po: $(POTFILE) .PHONY: install install: install-messages install-dtd + PYTHONPATH=$(XEN_ROOT)/tools/libxl $(PYTHON) genwrap.py $(XEN_ROOT)/tools/libxl/libxl.idl __pyxl_types.h CC="$(CC)" CFLAGS="$(CFLAGS)" $(PYTHON) setup.py install \ $(PYTHON_PREFIX_ARG) --root="$(DESTDIR)" --force
tools/python/genwrap.py new file +178 -0
178 lines not on this page · raw message
tools/python/setup.py +17 -2
@@ -9,14 +9,23 @@ extra_compile_args = [ "-fno-strict-ali include_dirs = [ XEN_ROOT + "/tools/libxc", XEN_ROOT + "/tools/xenstore", XEN_ROOT + "/tools/include", + XEN_ROOT + "/tools/libxl", ] library_dirs = [ XEN_ROOT + "/tools/libxc", XEN_ROOT + "/tools/xenstore", + XEN_ROOT + "/tools/libxl", + XEN_ROOT + "/tools/blktap2/control", ] libraries = [ "xenctrl", "xenguest", "xenstore" ] +plat = os.uname()[0] +if plat == 'Linux': + uuid_libs = ["uuid"] +else: + uuid_libs = [] + xc = Extension("xc", extra_compile_args = extra_compile_args, include_dirs = include_dirs + [ "xen/lowlevel/xc" ],
@@ -83,8 +92,14 @@ netlink = Extension("netlink", sources = [ "xen/lowlevel/netlink/netlink.c", "xen/lowlevel/netlink/libnetlink.c"]) -modules = [ xc, xs, ptsname, acm, flask ] -plat = os.uname()[0] +xl = Extension("xl", + extra_compile_args = extra_compile_args, + include_dirs = include_dirs + [ "xen/lowlevel/xl" ], + library_dirs = library_dirs, + libraries = libraries + ["xenlight", "blktapctl" ] + uuid_libs, + sources = [ "xen/lowlevel/xl/xl.c" ]) + +modules = [ xc, xs, ptsname, acm, flask, xl ] if plat == 'SunOS': modules.extend([ scf, process ]) if plat == 'Linux':
tools/python/xen/lowlevel/xl/xl.c new file +269 -0
269 lines not on this page · raw message
Quoted 4 lines · Gianni Tedesco
Attached patch is the first-cut of a libxl python binding. Presently only
list_domains,domid_to_name,domain_shutdownanddomain_destroyare implemented. The code takes advantage of Ian Campbells libxl IDL work to make 178 lines of python generate 4,460 lines of boilerplate.
I should also say that this patch will crash python without:
- xl: Fix NULL deref in libxl_ctx_free()
And conflicts with/re-implements:
- xl: cleanups for IDL code
Gianni