summaryrefslogtreecommitdiff
path: root/python/vyos/configtree.py
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2023-02-15 19:05:24 +0100
committerGitHub <noreply@github.com>2023-02-15 19:05:24 +0100
commita48940a943d237b0b2e8fcf8f9066c416d175e8d (patch)
tree8e76bfb1bd77b35d4f4a4a39898b2c5941871f2a /python/vyos/configtree.py
parent63dfe01db5fb60031cf5b4b017a2f172166fe4b7 (diff)
parent694096f108c3421d4ff11ad75eec3bfb1cde562b (diff)
downloadvyos-1x-a48940a943d237b0b2e8fcf8f9066c416d175e8d.tar.gz
vyos-1x-a48940a943d237b0b2e8fcf8f9066c416d175e8d.zip
Merge pull request #1811 from jestabro/udiff
config_mgmt: T4991: use configtree.show_diff instead of Python difflib
Diffstat (limited to 'python/vyos/configtree.py')
-rw-r--r--python/vyos/configtree.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py
index f2358ee4f..c0b3ebd78 100644
--- a/python/vyos/configtree.py
+++ b/python/vyos/configtree.py
@@ -16,7 +16,7 @@ import os
import re
import json
-from ctypes import cdll, c_char_p, c_void_p, c_int
+from ctypes import cdll, c_char_p, c_void_p, c_int, c_bool
LIBPATH = '/usr/lib/libvyosconfig.so.0'
@@ -322,6 +322,36 @@ class ConfigTree(object):
subt = ConfigTree(address=res)
return subt
+def show_diff(left, right, path=[], commands=False, libpath=LIBPATH):
+ if left is None:
+ left = ConfigTree(config_string='\n')
+ if right is None:
+ right = ConfigTree(config_string='\n')
+ if not (isinstance(left, ConfigTree) and isinstance(right, ConfigTree)):
+ raise TypeError("Arguments must be instances of ConfigTree")
+ if path:
+ if (not left.exists(path)) and (not right.exists(path)):
+ raise ConfigTreeError(f"Path {path} doesn't exist")
+
+ check_path(path)
+ path_str = " ".join(map(str, path)).encode()
+
+ __lib = cdll.LoadLibrary(libpath)
+ __show_diff = __lib.show_diff
+ __show_diff.argtypes = [c_bool, c_char_p, c_void_p, c_void_p]
+ __show_diff.restype = c_char_p
+ __get_error = __lib.get_error
+ __get_error.argtypes = []
+ __get_error.restype = c_char_p
+
+ res = __show_diff(commands, path_str, left._get_config(), right._get_config())
+ res = res.decode()
+ if res == "#1@":
+ msg = __get_error().decode()
+ raise ConfigTreeError(msg)
+
+ return res
+
class DiffTree:
def __init__(self, left, right, path=[], libpath=LIBPATH):
if left is None: