Skip to content

Commit f7e2b42

Browse files
authored
Merge pull request #5395 from jestabro/diff-functor
T9169: update for vyos1x-config refactor
2 parents bbe4079 + 54d419d commit f7e2b42

2 files changed

Lines changed: 93 additions & 37 deletions

File tree

libvyosconfig/lib/bindings.ml

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ open Commitd_client
77

88
module VT = Vytree
99
module CT = Config_tree
10-
module CD = Config_diff
1110
module RT = Reference_tree
1211
module TA = Tree_alg
13-
module CM = Commit
14-
module VC = Vycall_client
1512
module CDict = Config_dict
13+
module D = Diff
14+
module DT = Diff_tree
15+
module DC = Diff_compare
16+
module DS = Diff_show
17+
module UN = Union
18+
module M = Mask
1619

1720
module I = Internal.Make(Config_tree)
1821
module IR = Internal.Make(Reference_tree)
@@ -416,57 +419,68 @@ let copy_node c_ptr old_path new_path =
416419
| Vytree.Insert_error s -> error_message := s; 1
417420

418421
let diff_tree path c_ptr_l c_ptr_r =
419-
(* alert exn CD.diff_tree:
420-
[Config_diff.Incommensurable] caught
421-
[Config_diff.Empty_comparison] caught
422+
(* alert exn DT.diff_tree:
423+
[Diff.Incommensurable] caught
424+
[Diff.Empty_comparison] caught
422425
*)
423426
let path = split_on_whitespace path in
424427
let ct_l = Root.get c_ptr_l in
425428
let ct_r = Root.get c_ptr_r in
426429
try
427-
let ct_ret = (CD.diff_tree[@alert "-exn"]) path ct_l ct_r in
430+
let ct_ret = (DT.diff_tree[@alert "-exn"]) path ct_l ct_r in
428431
Ctypes.Root.create ct_ret
429432
with
430-
| CD.Incommensurable -> error_message := "Incommensurable"; Ctypes.null
431-
| CD.Empty_comparison -> error_message := "Empty comparison"; Ctypes.null
433+
| D.Incommensurable -> error_message := "Incommensurable"; Ctypes.null
434+
| D.Empty_comparison -> error_message := "Empty comparison"; Ctypes.null
432435

433436
let diff_compare cmds path c_ptr_l c_ptr_r =
434-
(* alert exn CD.show_diff:
435-
[Config_diff.Incommensurable] caught
436-
[Config_diff.Empty_comparison] caught
437+
(* alert exn DC.diff_compare:
438+
[Diff.Incommensurable] caught
439+
[Diff.Empty_comparison] caught
437440
*)
438441
let path = split_on_whitespace path in
439442
let ct_l = Root.get c_ptr_l in
440443
let ct_r = Root.get c_ptr_r in
441444
try
442-
(CD.diff_compare[@alert "-exn"]) ~cmds:cmds path ct_l ct_r
445+
(DC.diff_compare[@alert "-exn"]) ~cmds:cmds path ct_l ct_r
443446
with
444-
| CD.Incommensurable -> error_message := "Incommensurable"; "#1@"
445-
| CD.Empty_comparison -> error_message := "Empty comparison"; "#1@"
447+
| D.Incommensurable -> error_message := "Incommensurable"; "#1@"
448+
| D.Empty_comparison -> error_message := "Empty comparison"; "#1@"
449+
450+
let diff_show r_ptr c_ptr_l c_ptr_r path =
451+
let path = split_on_whitespace path in
452+
let rt = Root.get r_ptr in
453+
let ct_l = Root.get c_ptr_l in
454+
let ct_r = Root.get c_ptr_r in
455+
try
456+
(DS.diff_show[@alert "-exn"]) rt path ct_l ct_r
457+
with
458+
| D.Incommensurable -> error_message := "Incommensurable"; "#1@"
459+
| D.Empty_comparison -> error_message := "Empty comparison"; "#1@"
446460

447461
let tree_union c_ptr_l c_ptr_r =
448-
(* alert exn CD.tree_union:
462+
(* alert exn UN.tree_union:
449463
[Tree_alg.Incompatible_union] caught
450464
[Tree_alg.Nonexistent_child] caught
451465
*)
452466
let ct_l = Root.get c_ptr_l in
453467
let ct_r = Root.get c_ptr_r in
454468
try
455-
let ct_ret = (CD.tree_union[@alert "-exn"]) ct_l ct_r in
469+
let ct_ret = (UN.tree_union[@alert "-exn"]) ct_l ct_r in
456470
Ctypes.Root.create ct_ret
457471
with
458472
| TA.Nonexistent_child -> error_message := "Nonexistent child"; Ctypes.null
459473
| TA.Incompatible_union -> error_message := "Trees must have equivalent root"; Ctypes.null
460474

461475
let tree_merge destructive c_ptr_l c_ptr_r =
462-
(* alert exn CD.tree_merge:
476+
(* alert exn UN.tree_merge:
463477
[Tree_alg.Incompatible_union] caught
464478
[Tree_alg.Nonexistent_child] caught
465479
*)
466480
let ct_l = Root.get c_ptr_l in
467481
let ct_r = Root.get c_ptr_r in
468482
try
469-
let ct_ret = (CD.tree_merge[@alert "-exn"]) ~destructive:destructive ct_l ct_r in
483+
let ct_ret = (UN.tree_merge[@alert "-exn"]) ~destructive:destructive ct_l ct_r in
470484
Ctypes.Root.create ct_ret
471485
with
472486
| TA.Nonexistent_child -> error_message := "Nonexistent child"; Ctypes.null
@@ -488,19 +502,33 @@ let reference_tree_to_json internal_cache from_dir to_file =
488502
let s = Printf.sprintf "Write_error \'%s\'" msg in
489503
error_message := s; 1
490504

491-
let mask_tree c_ptr_l c_ptr_r exclusive =
492-
(* alert exn CD.mask_tree:
493-
[Config_diff.Incommensurable] caught
494-
[Config_diff.Empty_comparison] caught
505+
let mask_inclusive c_ptr_l c_ptr_r =
506+
(* alert exn M.mask_inclusive:
507+
[Diff.Incommensurable] caught
508+
[Diff.Empty_comparison] caught
509+
*)
510+
let ct_l = Root.get c_ptr_l in
511+
let ct_r = Root.get c_ptr_r in
512+
try
513+
let ct_ret = (M.mask_inclusive[@alert "-exn"]) ct_l ct_r in
514+
Ctypes.Root.create ct_ret
515+
with
516+
| D.Incommensurable -> error_message := "Incommensurable"; Ctypes.null
517+
| D.Empty_comparison -> error_message := "Empty comparison"; Ctypes.null
518+
519+
let mask_exclusive c_ptr_l c_ptr_r =
520+
(* alert exn M.mask_exclusive:
521+
[Diff.Incommensurable] caught
522+
[Diff.Empty_comparison] caught
495523
*)
496524
let ct_l = Root.get c_ptr_l in
497525
let ct_r = Root.get c_ptr_r in
498526
try
499-
let ct_ret = (CD.mask_tree[@alert "-exn"]) ~exclusive:exclusive ct_l ct_r in
527+
let ct_ret = (M.mask_exclusive[@alert "-exn"]) ct_l ct_r in
500528
Ctypes.Root.create ct_ret
501529
with
502-
| CD.Incommensurable -> error_message := "Incommensurable"; Ctypes.null
503-
| CD.Empty_comparison -> error_message := "Empty comparison"; Ctypes.null
530+
| D.Incommensurable -> error_message := "Incommensurable"; Ctypes.null
531+
| D.Empty_comparison -> error_message := "Empty comparison"; Ctypes.null
504532

505533
let subtree_from_partial r_ptr c_ptr i_ptr path =
506534
let rt = Root.get r_ptr in
@@ -509,10 +537,10 @@ let subtree_from_partial r_ptr c_ptr i_ptr path =
509537
let path = split_on_whitespace path in
510538
try
511539
error_message := "";
512-
let ct_ret = (CD.subtree_from_partial[@alert "-exn"]) rt ct input path in
540+
let ct_ret = (Derived.subtree_from_partial[@alert "-exn"]) rt ct input path in
513541
Ctypes.Root.create ct_ret
514542
with
515-
CD.Malformed_path s ->
543+
Derived.Malformed_path s ->
516544
error_message := s; Ctypes.null
517545

518546
let validate_tree_filter c_ptr rt_cache_path validator_dir =
@@ -578,10 +606,12 @@ struct
578606
let () = I.internal "return_values" ((ptr void) @-> string @-> returning string) return_values
579607
let () = I.internal "diff_tree" (string @-> (ptr void) @-> (ptr void) @-> returning (ptr void)) diff_tree
580608
let () = I.internal "diff_compare" (bool @-> string @-> (ptr void) @-> (ptr void) @-> returning string) diff_compare
609+
let () = I.internal "diff_show" ((ptr void) @-> (ptr void) @-> (ptr void) @-> string @-> returning string) diff_show
581610
let () = I.internal "tree_union" ((ptr void) @-> (ptr void) @-> returning (ptr void)) tree_union
582611
let () = I.internal "tree_merge" (bool @-> (ptr void) @-> (ptr void) @-> returning (ptr void)) tree_merge
583612
let () = I.internal "reference_tree_to_json" (string @-> string @-> string @-> returning int) reference_tree_to_json
584-
let () = I.internal "mask_tree" ((ptr void) @-> (ptr void) @-> bool @-> returning (ptr void)) mask_tree
613+
let () = I.internal "mask_inclusive" ((ptr void) @-> (ptr void) @-> returning (ptr void)) mask_inclusive
614+
let () = I.internal "mask_exclusive" ((ptr void) @-> (ptr void) @-> returning (ptr void)) mask_exclusive
585615
let () = I.internal "subtree_from_partial" ((ptr void) @-> (ptr void) @-> (ptr void) @-> string @-> returning (ptr void)) subtree_from_partial
586616
let () = I.internal "validate_tree_filter" ((ptr void) @-> string @-> string @-> returning (ptr void)) validate_tree_filter
587617
let () = I.internal "config_dict" ((ptr void) @-> (ptr void) @-> (ptr void) @-> string @-> bool @-> bool @-> returning string) config_dict

python/vyos/configtree.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,32 @@ def diff_compare(left, right, path=None, commands=False, libpath=LIBPATH):
579579
return res
580580

581581

582+
def diff_show(rt, left, right, path=None, libpath=LIBPATH):
583+
if not (isinstance(left, ConfigTree) and isinstance(right, ConfigTree)):
584+
raise TypeError('Arguments must be instances of ConfigTree')
585+
path = path or []
586+
587+
check_path(path)
588+
path_str = ' '.join(map(str, path)).encode()
589+
590+
__lib = cdll.LoadLibrary(libpath)
591+
__diff_show = __lib.diff_show
592+
__diff_show.argtypes = [c_void_p, c_void_p, c_void_p, c_char_p]
593+
__diff_show.restype = c_char_p
594+
__get_error = __lib.get_error
595+
__get_error.argtypes = []
596+
__get_error.restype = c_char_p
597+
598+
res = __diff_show(rt.get_tree(), left.get_tree(), right.get_tree(), path_str)
599+
res = res.decode()
600+
if res == '#1@':
601+
msg = __get_error().decode()
602+
raise ConfigTreeError(msg)
603+
604+
res = unescape_backslash(res)
605+
return res
606+
607+
582608
def union(left, right, libpath=LIBPATH):
583609
if left is None:
584610
left = ConfigTree(config_string='\n')
@@ -630,14 +656,14 @@ def mask_inclusive(left, right, libpath=LIBPATH):
630656

631657
try:
632658
__lib = cdll.LoadLibrary(libpath)
633-
__mask_tree = __lib.mask_tree
634-
__mask_tree.argtypes = [c_void_p, c_void_p, c_bool]
635-
__mask_tree.restype = c_void_p
659+
__mask_inclusive = __lib.mask_inclusive
660+
__mask_inclusive.argtypes = [c_void_p, c_void_p]
661+
__mask_inclusive.restype = c_void_p
636662
__get_error = __lib.get_error
637663
__get_error.argtypes = []
638664
__get_error.restype = c_char_p
639665

640-
res = __mask_tree(left.get_tree(), right.get_tree(), False)
666+
res = __mask_inclusive(left.get_tree(), right.get_tree())
641667
except Exception as e:
642668
raise ConfigTreeError(e)
643669
if not res:
@@ -656,14 +682,14 @@ def mask_exclusive(left, right, libpath=LIBPATH):
656682

657683
try:
658684
__lib = cdll.LoadLibrary(libpath)
659-
__mask_tree = __lib.mask_tree
660-
__mask_tree.argtypes = [c_void_p, c_void_p, c_bool]
661-
__mask_tree.restype = c_void_p
685+
__mask_exclusive = __lib.mask_exclusive
686+
__mask_exclusive.argtypes = [c_void_p, c_void_p]
687+
__mask_exclusive.restype = c_void_p
662688
__get_error = __lib.get_error
663689
__get_error.argtypes = []
664690
__get_error.restype = c_char_p
665691

666-
res = __mask_tree(left.get_tree(), right.get_tree(), True)
692+
res = __mask_exclusive(left.get_tree(), right.get_tree())
667693
except Exception as e:
668694
raise ConfigTreeError(e)
669695
if not res:

0 commit comments

Comments
 (0)