From 9aec42029aff0d81c9f27a6d6df6eae401446c76 Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Tue, 5 Jun 2018 20:20:30 +0200 Subject: T684: add bindings for the commands formatter and scripts for converting configs to commands. --- python/vyos/configtree.py | 7 +++++++ src/helpers/commands-pipe.py | 30 ++++++++++++++++++++++++++++++ src/utils/vyos-config-to-commands | 29 +++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100755 src/helpers/commands-pipe.py create mode 100755 src/utils/vyos-config-to-commands diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py index e0d46260f..4b46a1fb3 100644 --- a/python/vyos/configtree.py +++ b/python/vyos/configtree.py @@ -96,6 +96,10 @@ class ConfigTree(object): self.__to_string.argtypes = [c_void_p] self.__to_string.restype = c_char_p + self.__to_commands = self.__lib.to_commands + self.__to_commands.argtypes = [c_void_p] + self.__to_commands.restype = c_char_p + self.__set_add_value = self.__lib.set_add_value self.__set_add_value.argtypes = [c_void_p, c_char_p, c_char_p] self.__set_add_value.restype = c_int @@ -162,6 +166,9 @@ class ConfigTree(object): config_string = "{0}\n{1}".format(config_string, self.__comments) return config_string + def to_commands(self): + return self.__to_commands(self.__config).decode() + def set(self, path, value=None, replace=True): check_path(path) path_str = " ".join(map(str, path)).encode() diff --git a/src/helpers/commands-pipe.py b/src/helpers/commands-pipe.py new file mode 100755 index 000000000..3ffc64545 --- /dev/null +++ b/src/helpers/commands-pipe.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +import sys +import re + +from signal import signal, SIGPIPE, SIG_DFL +from vyos.configtree import ConfigTree + +signal(SIGPIPE,SIG_DFL) + + +config_string = sys.stdin.read().strip() + +if not config_string: + sys.exit(0) + +# When used in conf mode pipe, the config given to the script is likely incomplete +# and breaks the "all top level nodes are neither tag nor leaf" +# invariant, so we wrap it into a fake node. +# Since nodes don't normally start with an underscore, +# __root__ is hygienic enough. +config_string = "__root__ {{ {0} \n }}".format(config_string) + +config_re = re.compile("(set|comment)\s+__root__\s+(.*)") + +config = ConfigTree(config_string) +commands = config.to_commands() +commands = config_re.sub("\\1 \\2", commands) + +print(commands) diff --git a/src/utils/vyos-config-to-commands b/src/utils/vyos-config-to-commands new file mode 100755 index 000000000..8b50f7c5d --- /dev/null +++ b/src/utils/vyos-config-to-commands @@ -0,0 +1,29 @@ +#!/usr/bin/python3 + +import sys + +from signal import signal, SIGPIPE, SIG_DFL +from vyos.configtree import ConfigTree + +signal(SIGPIPE,SIG_DFL) + +config_string = None +if (len(sys.argv) == 1): + # If no argument given, act as a pipe + config_string = sys.stdin.read() +else: + file_name = sys.argv[1] + try: + with open(file_name, 'r') as f: + config_string = f.read() + except OSError as e: + print("Could not read config file {0}: {1}".format(file_name, e), file=sys.stderr) + +try: + config = ConfigTree(config_string) + commands = config.to_commands() +except ValueError as e: + print("Could not parse the config file: {0}".format(e), file=sys.stderr) + sys.exit(1) + +print(commands) -- cgit v1.2.3