Merge pull request #333 from coreemu/feature/imn-to-xml
added imn-to-xml script
This commit is contained in:
commit
e9caea5a6c
2 changed files with 72 additions and 2 deletions
|
@ -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):
|
||||
|
|
70
daemon/scripts/core-imn-to-xml
Executable file
70
daemon/scripts/core-imn-to-xml
Executable file
|
@ -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)
|
Loading…
Reference in a new issue