From 3872cf8a7555774d3093bbf324a9c478be837359 Mon Sep 17 00:00:00 2001 From: Vincent Jardin Date: Thu, 16 Apr 2026 12:24:21 +0200 Subject: [PATCH] package/python-libyang: new package Python CFFI bindings for the libyang YANG library, providing the 'libyang' Python module for YANG data modeling operations. This package is used by higher-level tooling such as python-sysrepo. https://github.com/CESNET/libyang-python Signed-off-by: Vincent Jardin Signed-off-by: Vincent Cruz Signed-off-by: Thomas Petazzoni --- DEVELOPERS | 1 + package/Config.in | 1 + package/python-libyang/Config.in | 18 ++++++++ package/python-libyang/python-libyang.hash | 5 ++ package/python-libyang/python-libyang.mk | 21 +++++++++ .../tests/package/sample_python_libyang.py | 46 +++++++++++++++++++ .../tests/package/test_python_libyang.py | 12 +++++ 7 files changed, 104 insertions(+) create mode 100644 package/python-libyang/Config.in create mode 100644 package/python-libyang/python-libyang.hash create mode 100644 package/python-libyang/python-libyang.mk create mode 100644 support/testing/tests/package/sample_python_libyang.py create mode 100644 support/testing/tests/package/test_python_libyang.py diff --git a/DEVELOPERS b/DEVELOPERS index 7f99e8cb53..76d4df4a29 100644 --- a/DEVELOPERS +++ b/DEVELOPERS @@ -3368,6 +3368,7 @@ F: package/dpdk/ F: package/libecoli/ F: package/libnss-ato/ F: package/libyang-cpp/ +F: package/python-libyang/ F: package/sysrepo-cpp/ N: Vincent Prince diff --git a/package/Config.in b/package/Config.in index 19c4ac2221..c70a64e968 100644 --- a/package/Config.in +++ b/package/Config.in @@ -1211,6 +1211,7 @@ menu "External python modules" source "package/python-libevdev/Config.in" source "package/python-librt/Config.in" source "package/python-libusb1/Config.in" + source "package/python-libyang/Config.in" source "package/python-lmdb/Config.in" source "package/python-lockfile/Config.in" source "package/python-log-rate-limit/Config.in" diff --git a/package/python-libyang/Config.in b/package/python-libyang/Config.in new file mode 100644 index 0000000000..0442bb2e90 --- /dev/null +++ b/package/python-libyang/Config.in @@ -0,0 +1,18 @@ +config BR2_PACKAGE_PYTHON_LIBYANG + bool "python-libyang" + depends on BR2_TOOLCHAIN_HAS_SYNC_4 # libyang + depends on BR2_TOOLCHAIN_HAS_THREADS # libyang + depends on !BR2_STATIC_LIBS # libyang + select BR2_PACKAGE_LIBYANG + select BR2_PACKAGE_PYTHON_CFFI # runtime + help + Python CFFI bindings for the libyang YANG library. + + This package provides the 'libyang' Python module, used by + higher-level tooling such as python-sysrepo. + + https://github.com/CESNET/libyang-python + +comment "python-libyang needs a toolchain w/ threads, dynamic library" + depends on !BR2_TOOLCHAIN_HAS_SYNC_4 + depends on !BR2_TOOLCHAIN_HAS_THREADS || BR2_STATIC_LIBS diff --git a/package/python-libyang/python-libyang.hash b/package/python-libyang/python-libyang.hash new file mode 100644 index 0000000000..077d97cdd8 --- /dev/null +++ b/package/python-libyang/python-libyang.hash @@ -0,0 +1,5 @@ +# From https://pypi.org/pypi/libyang/3.3.0/json +sha256 97da2c908a8a3607ac834f2acec224ea53a4b9e9770c13e5598513641decf637 libyang-3.3.0.tar.gz + +# Locally computed +sha256 c8d554f918d32b1467f35829e1cfb753764dd683c6edcecc877a4d7e9f05b2a7 LICENSE diff --git a/package/python-libyang/python-libyang.mk b/package/python-libyang/python-libyang.mk new file mode 100644 index 0000000000..2e4d1f3b90 --- /dev/null +++ b/package/python-libyang/python-libyang.mk @@ -0,0 +1,21 @@ +################################################################################ +# +# python-libyang +# +################################################################################ + +PYTHON_LIBYANG_VERSION = 3.3.0 +PYTHON_LIBYANG_SOURCE = libyang-$(PYTHON_LIBYANG_VERSION).tar.gz +PYTHON_LIBYANG_SITE = https://files.pythonhosted.org/packages/source/l/libyang + +PYTHON_LIBYANG_LICENSE = MIT +PYTHON_LIBYANG_LICENSE_FILES = LICENSE + +# pyproject.toml (PEP517) +PYTHON_LIBYANG_SETUP_TYPE = pep517 + +PYTHON_LIBYANG_DEPENDENCIES = \ + libyang \ + host-python-cffi + +$(eval $(python-package)) diff --git a/support/testing/tests/package/sample_python_libyang.py b/support/testing/tests/package/sample_python_libyang.py new file mode 100644 index 0000000000..29b2c6cadd --- /dev/null +++ b/support/testing/tests/package/sample_python_libyang.py @@ -0,0 +1,46 @@ +# test from https://github.com/CESNET/libyang-python/blob/master/README.rst + +import libyang +from xml.etree import ElementTree + +ctx = libyang.Context() + +module = ctx.parse_module_str(''' +module example { + namespace "urn:example"; + prefix "ex"; + container data { + list interface { + key name; + leaf name { + type string; + } + leaf address { + type string; + } + } + leaf hostname { + type string; + } + } +} +''') + +node = module.parse_data_dict({ + 'data': { + 'hostname': 'foobar', + 'interface': [ + {'name': 'eth0', 'address': '1.2.3.4/24'}, + {'name': 'lo', 'address': '127.0.0.1'}, + ], + }, +}) + +xml = node.print_mem('xml', pretty=True) +tree = ElementTree.fromstringlist(xml) +assert tree.tag == '{urn:example}data' +assert tree.find('.//{*}hostname').text == 'foobar' +assert len(tree.find('.//{*}interface')) == 2 + +node.free() +ctx.destroy() diff --git a/support/testing/tests/package/test_python_libyang.py b/support/testing/tests/package/test_python_libyang.py new file mode 100644 index 0000000000..ea31b0bc5d --- /dev/null +++ b/support/testing/tests/package/test_python_libyang.py @@ -0,0 +1,12 @@ +from tests.package.test_python import TestPythonPackageBase + + +class TestPythonLibyang(TestPythonPackageBase): + __test__ = True + config = TestPythonPackageBase.config + \ + """ + BR2_PACKAGE_PYTHON3=y + BR2_PACKAGE_PYTHON3_PYEXPAT=y + BR2_PACKAGE_PYTHON_LIBYANG=y + """ + sample_scripts = ["tests/package/sample_python_libyang.py"]