Skip to content

Commit

Permalink
netplan/cli/utils: move the iter ABI check in the wrapper class
Browse files Browse the repository at this point in the history
This makes it possible to use the wrapper class in autonomy, and remove
the implicit dependency between the netplan_get_ids_for_devtype tests
and the _NetdefIdIterator tests. The latter would crash if run before
the former, as the ctypes bindings wouldn't have been initialized.
  • Loading branch information
schopin-pro committed Oct 25, 2021
1 parent 412645b commit 1fb7f1f
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions netplan/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,34 @@ def netplan_get_filename_by_id(netdef_id, rootdir):


class _NetdefIdIterator:
_abi_checked = False

@classmethod
def c_abi_sanity_check(cls):
if cls._abi_checked:
return

if not hasattr(lib, '_netplan_iter_defs_per_devtype_init'): # pragma: nocover (hard to unit-test against the WRONG lib)
raise LibNetplanException('''
The current version of libnetplan does not allow iterating by devtype.
Please ensure that both the netplan CLI package and its library are up to date.
''')
lib._netplan_iter_defs_per_devtype_init.argtypes = [ctypes.c_char_p]
lib._netplan_iter_defs_per_devtype_init.restype = ctypes.c_void_p

lib._netplan_iter_defs_per_devtype_next.argtypes = [ctypes.c_void_p]
lib._netplan_iter_defs_per_devtype_next.restype = ctypes.c_void_p

lib._netplan_iter_defs_per_devtype_free.argtypes = [ctypes.c_void_p]
lib._netplan_iter_defs_per_devtype_free.restype = None

lib._netplan_netdef_id.argtypes = [ctypes.c_void_p]
lib._netplan_netdef_id.restype = ctypes.c_char_p

cls._abi_checked = True

def __init__(self, devtype):
self.c_abi_sanity_check()
self.iterator = lib._netplan_iter_defs_per_devtype_init(devtype.encode('utf-8'))

def __del__(self):
Expand All @@ -83,23 +110,6 @@ def __next__(self):


def netplan_get_ids_for_devtype(devtype, rootdir):
if not hasattr(lib, '_netplan_iter_defs_per_devtype_init'): # pragma: nocover (hard to unit-test against the WRONG lib)
raise LibNetplanException('''
The current version of libnetplan does not allow iterating by devtype.
Please ensure that both the netplan CLI package and its library are up to date.
''')
lib._netplan_iter_defs_per_devtype_init.argtypes = [ctypes.c_char_p]
lib._netplan_iter_defs_per_devtype_init.restype = ctypes.c_void_p

lib._netplan_iter_defs_per_devtype_next.argtypes = [ctypes.c_void_p]
lib._netplan_iter_defs_per_devtype_next.restype = ctypes.c_void_p

lib._netplan_iter_defs_per_devtype_free.argtypes = [ctypes.c_void_p]
lib._netplan_iter_defs_per_devtype_free.restype = None

lib._netplan_netdef_id.argtypes = [ctypes.c_void_p]
lib._netplan_netdef_id.restype = ctypes.c_char_p

err = ctypes.POINTER(_GError)()
lib.netplan_clear_netdefs()
lib.process_yaml_hierarchy(rootdir.encode('utf-8'))
Expand Down

0 comments on commit 1fb7f1f

Please sign in to comment.