<commit_before>#!/usr/bin/python3

# Copyright 2019 VyOS maintainers and contributors <<EMAIL>>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import subprocess
import vyos.defaults
import vyos.remote
from vyos.config import Config
from vyos.configtree import ConfigTree


if (len(sys.argv) < 2):
    print("Need config file name to merge.")
    print("Usage: merge <config file> [config path]")
    sys.exit(0)

file_name = sys.argv[1]

configdir = vyos.defaults.directories['config']

protocols = ['scp', 'sftp', 'http', 'https', 'ftp', 'tftp']

if any(x in file_name for x in protocols):
    config_file = vyos.remote.get_remote_config(file_name)
    if not config_file:
        sys.exit("No config file by that name.")
else:
    canonical_path = "{0}/{1}".format(configdir, file_name)
    first_err = None
    try:
        with open(canonical_path, 'r') as f:
            config_file = f.read()
    except Exception as err:
        first_err = err
        try:
            with open(file_name, 'r') as f:
                config_file = f.read()
        except Exception as err:
            print(first_err)
            print(err)
            sys.exit(1)

path = None
if (len(sys.argv) > 2):
    path = " ".join(sys.argv[2:])

merge_config_tree = ConfigTree(config_file)

effective_config = Config()

output_effective_config = effective_config.show_config()
effective_config_tree = ConfigTree(output_effective_config)

effective_cmds = effective_config_tree.to_commands()
merge_cmds = merge_config_tree.to_commands()

effective_cmd_list = effective_cmds.splitlines()
merge_cmd_list =  merge_cmds.splitlines()

effective_cmd_set = set(effective_cmd_list)<commit_msg>T1397: escape backslashes in output passed to configtree<commit_after>#!/usr/bin/python3

# Copyright 2019 VyOS maintainers and contributors <<EMAIL>>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import subprocess
import vyos.defaults
import vyos.remote
from vyos.config import Config
from vyos.configtree import ConfigTree


if (len(sys.argv) < 2):
    print("Need config file name to merge.")
    print("Usage: merge <config file> [config path]")
    sys.exit(0)

file_name = sys.argv[1]

configdir = vyos.defaults.directories['config']

protocols = ['scp', 'sftp', 'http', 'https', 'ftp', 'tftp']

if any(x in file_name for x in protocols):
    config_file = vyos.remote.get_remote_config(file_name)
    if not config_file:
        sys.exit("No config file by that name.")
else:
    canonical_path = "{0}/{1}".format(configdir, file_name)
    first_err = None
    try:
        with open(canonical_path, 'r') as f:
            config_file = f.read()
    except Exception as err:
        first_err = err
        try:
            with open(file_name, 'r') as f:
                config_file = f.read()
        except Exception as err:
            print(first_err)
            print(err)
            sys.exit(1)

path = None
if (len(sys.argv) > 2):
    path = " ".join(sys.argv[2:])

merge_config_tree = ConfigTree(config_file)

effective_config = Config()

output_effective_config = effective_config.show_config()
# showConfig (called by config.show_config() does not escape
# backslashes, which configtree expects; cf. T1001.
output_effective_config = output_effective_config.replace("\\", "\\\\")

effective_config_tree = ConfigTree(output_effective_config)

effective_cmds = effective_config_tree.to_commands()
merge_cmds = merge_config_tree.to_commands()

effective_cmd_list = effective_cmds.splitlines()
merge_cmd_list =  merge_cmds.splitlines()

effective_cmd_set = set(effective_cmd_list)
{'fraction_non_alphanumeric': 0.09646036017387705, 'fraction_numerical': 0.007865866280273236, 'mean_word_length': 3.774703557312253, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2865300', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1441, 'n_words': 489}