diff --git a/python/libknot/control.py b/python/libknot/control.py index 43737031b3a0f891b2c0e3f41c44c9d26e144f4b..8ec7a178fedecf278c13d3a60234cf2fe567ea27 100755 --- a/python/libknot/control.py +++ b/python/libknot/control.py @@ -1,6 +1,9 @@ """Libknot server control interface wrapper. Example: + import json + from libknot.control import * + ctl = KnotCtl() ctl.connect("/var/run/knot/knot.sock") @@ -25,35 +28,56 @@ Example: from ctypes import cdll, c_void_p, c_int, c_char_p, c_uint, byref from enum import IntEnum -LIB = cdll.LoadLibrary('libknot.so') +CTL_ALLOC = None +CTL_FREE = None +CTL_SET_TIMEOUT = None +CTL_CONNECT = None +CTL_CLOSE = None +CTL_SEND = None +CTL_RECEIVE = None +CTL_ERROR = None + + +def load_lib(path="libknot.so"): + """Loads the libknot library.""" + + LIB = cdll.LoadLibrary(path) -CTL_ALLOC = LIB.knot_ctl_alloc -CTL_ALLOC.restype = c_void_p + global CTL_ALLOC + CTL_ALLOC = LIB.knot_ctl_alloc + CTL_ALLOC.restype = c_void_p -CTL_FREE = LIB.knot_ctl_free -CTL_FREE.argtypes = [c_void_p] + global CTL_FREE + CTL_FREE = LIB.knot_ctl_free + CTL_FREE.argtypes = [c_void_p] -CTL_SET_TIMEOUT = LIB.knot_ctl_set_timeout -CTL_SET_TIMEOUT.argtypes = [c_void_p, c_int] + global CTL_SET_TIMEOUT + CTL_SET_TIMEOUT = LIB.knot_ctl_set_timeout + CTL_SET_TIMEOUT.argtypes = [c_void_p, c_int] -CTL_CONNECT = LIB.knot_ctl_connect -CTL_CONNECT.restype = c_int -CTL_CONNECT.argtypes = [c_void_p, c_char_p] + global CTL_CONNECT + CTL_CONNECT = LIB.knot_ctl_connect + CTL_CONNECT.restype = c_int + CTL_CONNECT.argtypes = [c_void_p, c_char_p] -CTL_CLOSE = LIB.knot_ctl_close -CTL_CLOSE.argtypes = [c_void_p] + global CTL_CLOSE + CTL_CLOSE = LIB.knot_ctl_close + CTL_CLOSE.argtypes = [c_void_p] -CTL_SEND = LIB.knot_ctl_send -CTL_SEND.restype = c_int -CTL_SEND.argtypes = [c_void_p, c_uint, c_void_p] + global CTL_SEND + CTL_SEND = LIB.knot_ctl_send + CTL_SEND.restype = c_int + CTL_SEND.argtypes = [c_void_p, c_uint, c_void_p] -CTL_RECEIVE = LIB.knot_ctl_receive -CTL_RECEIVE.restype = c_int -CTL_RECEIVE.argtypes = [c_void_p, c_void_p, c_void_p] + global CTL_RECEIVE + CTL_RECEIVE = LIB.knot_ctl_receive + CTL_RECEIVE.restype = c_int + CTL_RECEIVE.argtypes = [c_void_p, c_void_p, c_void_p] -CTL_ERROR = LIB.knot_strerror -CTL_ERROR.restype = c_char_p -CTL_ERROR.argtypes = [c_int] + global CTL_ERROR + CTL_ERROR = LIB.knot_strerror + CTL_ERROR.restype = c_char_p + CTL_ERROR.argtypes = [c_int] class KnotCtlType(IntEnum): @@ -114,6 +138,8 @@ class KnotCtl(object): """Libknot server control interface.""" def __init__(self): + if not CTL_ALLOC: + load_lib() self.obj = CTL_ALLOC() def __del__(self): diff --git a/tests-extra/tools/dnstest/libknot.py b/tests-extra/tools/dnstest/libknot.py new file mode 100644 index 0000000000000000000000000000000000000000..418b5a8491cda2465879821806f5ea6a59b6d2b2 --- /dev/null +++ b/tests-extra/tools/dnstest/libknot.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +import sys + +from dnstest.utils import Skip +import dnstest.params as params + +try: + sys.path.append(params.repo_binary("python")) + import libknot.control + libknot.control.load_lib(params.libknot_lib) +except: + raise Skip("libknot not available") diff --git a/tests-extra/tools/dnstest/params.py b/tests-extra/tools/dnstest/params.py index f1c1de898816c876c2c0099bacb4e186551e2cc7..50f7ccb0c67e237874ebbfa5efd53a7c9c506241 100644 --- a/tests-extra/tools/dnstest/params.py +++ b/tests-extra/tools/dnstest/params.py @@ -50,6 +50,8 @@ gdb_bin = get_binary("KNOT_TEST_GDB", "gdb") vgdb_bin = get_binary("KNOT_TEST_VGDB", "vgdb") # KNOT_TEST_LIBTOOL - libtool script. libtool_bin = get_binary("KNOT_TEST_LIBTOOL", repo_binary("libtool")) +# KNOT_TEST_LIBKNOT - libknot library. +libknot_lib = get_binary("KNOT_TEST_LIBKNOT", repo_binary("src/.libs/libknot.so")) # KNOT_TEST_KNOT - Knot binary. knot_bin = get_binary("KNOT_TEST_KNOT", repo_binary("src/knotd")) # KNOT_TEST_KNOTC - Knot control binary.