From 848d4acb8fafd0c5419e847d13eb8029f2499d12 Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Tue, 7 Jan 2020 15:29:36 -0800 Subject: [PATCH] added imn-to-xml script, fixed bug introduced when reading xml files with custom service files --- daemon/core/xml/corexml.py | 4 +- daemon/scripts/core-imn-to-xml | 70 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) create mode 100755 daemon/scripts/core-imn-to-xml diff --git a/daemon/core/xml/corexml.py b/daemon/core/xml/corexml.py index db6d8bd7..0e79137c 100644 --- a/daemon/core/xml/corexml.py +++ b/daemon/core/xml/corexml.py @@ -664,12 +664,12 @@ class CoreXmlReader: file_elements = service_configuration.find("files") if file_elements is not None: - files = [] + files = set(service.configs) for file_element in file_elements.iterchildren(): name = file_element.get("name") data = file_element.text service.config_data[name] = data - files.append(name) + files.add(name) service.configs = tuple(files) def read_emane_configs(self): diff --git a/daemon/scripts/core-imn-to-xml b/daemon/scripts/core-imn-to-xml new file mode 100755 index 00000000..725d7119 --- /dev/null +++ b/daemon/scripts/core-imn-to-xml @@ -0,0 +1,70 @@ +#!/usr/bin/env python +import argparse +import re +import sys +from pathlib import Path + +from core import utils +from core.api.grpc.client import CoreGrpcClient +from core.errors import CoreCommandError + +if __name__ == "__main__": + # parse flags + parser = argparse.ArgumentParser(description="Converts CORE imn files to xml") + parser.add_argument("-f", "--file", dest="file", help="imn file to convert") + parser.add_argument( + "-d", "--dest", dest="dest", default=None, help="destination for xml file, defaults to same location as imn" + ) + args = parser.parse_args() + + # validate provided file exists + imn_file = Path(args.file) + if not imn_file.exists(): + print(f"{args.file} does not exist") + sys.exit(1) + + # validate destination + if args.dest is not None: + dest = Path(args.dest) + if not dest.exists() or not dest.is_dir(): + print(f"{dest.resolve()} does not exist or is not a directory") + sys.exit(1) + xml_file = Path(dest, imn_file.with_suffix(".xml").name) + else: + xml_file = Path(imn_file.with_suffix(".xml").name) + + # validate xml file + if xml_file.exists(): + print(f"{xml_file.resolve()} already exists") + sys.exit(1) + + # run provided imn using core-gui batch mode + try: + print(f"running {imn_file.resolve()} in batch mode") + output = utils.cmd(f"core-gui --batch {imn_file.resolve()}") + last_line = output.split("\n")[-1].strip() + + # check for active session + if last_line == "Another session is active.": + print("need to restart core-daemon or shutdown previous batch session") + sys.exit(1) + + # parse session id + m = re.search(r"Session id is (\d+)\.", last_line) + if not m: + print(f"failed to find session id: {output}") + sys.exit(1) + session_id = int(m.group(1)) + print(f"created session {session_id}") + + # save xml and delete session + client = CoreGrpcClient() + with client.context_connect(): + print(f"saving xml {xml_file.resolve()}") + client.save_xml(session_id, xml_file) + + print(f"deleting session {session_id}") + client.delete_session(session_id) + except CoreCommandError as e: + print(f"core-gui batch failed for {imn_file.resolve()}: {e}") + sys.exit(1)