Skip to content

Commit

Permalink
libyang python APIs (#10)
Browse files Browse the repository at this point in the history
* libyang python APIs
- libyang python wrapper: class sonic_yang
- setup.py to build sonic_yang_mgmt-1.0-py2-none-any.whl
- pytest for the libyang python class: sonic_yang

* Add python wheel package dependency on debs/stretch targets in slave.mk

* Add sonic-yang-mgmt debian package to sonic-broadcom.bin and install libyang python 2 and sonic-yang-mgmt on switch

* Python APIs for libyang
- Add exceptions
- specify data_xpath and schema_xpath
- Add more test cases

* Add error messges for libyang Python APIs
  • Loading branch information
li-pingmao authored and zhenggen-xu committed Jan 9, 2020
1 parent 32d4683 commit c6aa34f
Show file tree
Hide file tree
Showing 9 changed files with 1,274 additions and 12 deletions.
9 changes: 9 additions & 0 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ sudo cp {{swsssdk_py2_wheel_path}} $FILESYSTEM_ROOT/$SWSSSDK_PY2_WHEEL_NAME
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install $SWSSSDK_PY2_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$SWSSSDK_PY2_WHEEL_NAME

# Install sonic-yang-mgmt Python 2 package, install dependencies
sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/libyang_*.deb
sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/libyang-cpp_*.deb
sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/python2-yang_*.deb
SONIC_YANG_MGMT_PY2_WHEEL_NAME=$(basename {{sonic_yang_mgmt_py2_wheel_path}})
sudo cp {{sonic_yang_mgmt_py2_wheel_path}} $FILESYSTEM_ROOT/$SONIC_YANG_MGMT_PY2_WHEEL_NAME
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install $SONIC_YANG_MGMT_PY2_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$SONIC_YANG_MGMT_PY2_WHEEL_NAME

# Install sonic-platform-common Python 2 package
PLATFORM_COMMON_PY2_WHEEL_NAME=$(basename {{platform_common_py2_wheel_path}})
sudo cp {{platform_common_py2_wheel_path}} $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY2_WHEEL_NAME
Expand Down
2 changes: 2 additions & 0 deletions rules/sonic-yang-mgmt-py2.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
SONIC_YANG_MGMT_PY2 = sonic_yang_mgmt-1.0-py2-none-any.whl
$(SONIC_YANG_MGMT_PY2)_SRC_PATH = $(SRC_PATH)/sonic-yang-mgmt
$(SONIC_YANG_MGMT_PY2)_PYTHON_VERSION = 2
$(SONIC_YANG_MGMT_PY2)_DEBS_DEPENDS = $(LIBYANG)

SONIC_PYTHON_WHEELS += $(SONIC_YANG_MGMT_PY2)
7 changes: 5 additions & 2 deletions slave.mk
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ SONIC_TARGET_LIST += $(addprefix $(PYTHON_DEBS_PATH)/, $(SONIC_PYTHON_STDEB_DEBS
# $(SOME_NEW_WHL)_PYTHON_VERSION = 2 (or 3)
# $(SOME_NEW_WHL)_DEPENDS = $(SOME_OTHER_WHL1) $(SOME_OTHER_WHL2) ...
# SONIC_PYTHON_WHEELS += $(SOME_NEW_WHL)
$(addprefix $(PYTHON_WHEELS_PATH)/, $(SONIC_PYTHON_WHEELS)) : $(PYTHON_WHEELS_PATH)/% : .platform $$(addsuffix -install,$$(addprefix $(PYTHON_WHEELS_PATH)/,$$($$*_DEPENDS)))
$(addprefix $(PYTHON_WHEELS_PATH)/, $(SONIC_PYTHON_WHEELS)) : $(PYTHON_WHEELS_PATH)/% : .platform $$(addsuffix -install,$$(addprefix $(PYTHON_WHEELS_PATH)/,$$($$*_DEPENDS))) \
$$(addsuffix -install,$$(addprefix $(DEBS_PATH)/,$$($$*_DEBS_DEPENDS)))
$(HEADER)
pushd $($*_SRC_PATH) $(LOG)
# apply series of patches if exist
Expand Down Expand Up @@ -624,7 +625,8 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_CONFIG_ENGINE)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_COMMON_PY2)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(REDIS_DUMP_LOAD_PY2)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_API_PY2))
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_API_PY2)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_YANG_MGMT_PY2))
$(HEADER)
# Pass initramfs and linux kernel explicitly. They are used for all platforms
export debs_path="$(STRETCH_DEBS_PATH)"
Expand All @@ -649,6 +651,7 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
export platform_common_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_COMMON_PY2))"
export redis_dump_load_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(REDIS_DUMP_LOAD_PY2))"
export install_debug_image="$(INSTALL_DEBUG_TOOLS)"
export sonic_yang_mgmt_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_YANG_MGMT_PY2))"

$(foreach docker, $($*_DOCKERS),\
export docker_image="$(docker)"
Expand Down
27 changes: 17 additions & 10 deletions src/sonic-yang-mgmt/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
from setuptools.command.build_py import build_py
from os import system
from sys import exit
import pytest
import os

# important reuirements parameters
build_requirements = ['../../target/debs/stretch/libyang_1.0.73_amd64.deb',
'../../target/debs/stretch/libyang-cpp_1.0.73_amd64.deb',
'../../target/debs/stretch/python2-yang_1.0.73_amd64.deb']
'../../target/debs/stretch/libyang-cpp_1.0.73_amd64.deb',
'../../target/debs/stretch/python2-yang_1.0.73_amd64.deb',]

install_requirements = []
setup_requirements = ['pytest-runner']

setup_requirements = ['pytest-runner',]

test_requirements = ['pytest>=3', ]
test_requirements = ['pytest>=3']

# read me
with open('README.rst') as readme_file:
Expand All @@ -39,9 +39,10 @@ def run (self):
if 'target/debs'in req:
pkg_install_cmd = "sudo dpkg -i {}".format(req)
if (system(pkg_install_cmd)):
print("{} installed".format(req))
print("{} installation failed".format(req))
exit(1)
else:
print("{} installtion failed".format(req))
print("{} installed".format(req))

# run tests for yang models
test_yang_cmd = "python {} -f {} -y {}".format(yang_test_py, test_yangJson_file, yang_model_dir)
Expand All @@ -53,8 +54,13 @@ def run (self):
print("YANG Tests passed\n")

# Continue usual build steps
build_py.run(self)
# run pytest for libyang python APIs
self.pytest_args = []
errno = pytest.main(self.pytest_args)
if (errno):
exit(errno)

build_py.run(self)

setup(
cmdclass={
Expand All @@ -77,12 +83,13 @@ def run (self):
'Programming Language :: Python :: 3.8',
],
description="Package contains YANG models for sonic.",
install_requires=install_requirements,
tests_require = test_requirements,
license="GNU General Public License v3",
long_description=readme + '\n\n',
include_package_data=True,
keywords='sonic_yang_mgmt',
name='sonic_yang_mgmt',
py_modules=['sonic_yang'],
packages=find_packages(),
setup_requires=setup_requirements,
version='1.0',
Expand Down
Loading

0 comments on commit c6aa34f

Please sign in to comment.