grpc: added args to execute script to reflect prior gui functionality, updated new and old gui to use the same exact logic for executing scripts for consistency

This commit is contained in:
Blake Harnden 2021-11-18 09:02:42 -08:00
parent f98594e927
commit 9da64af79b
7 changed files with 30 additions and 22 deletions

View file

@ -978,14 +978,15 @@ class CoreGrpcClient:
response = self.stub.GetEmaneEventChannel(request)
return wrappers.EmaneEventChannel.from_proto(response)
def execute_script(self, script: str) -> Optional[int]:
def execute_script(self, script: str, args: str) -> Optional[int]:
"""
Executes a python script given context of the current CoreEmu object.
:param script: script to execute
:param args: arguments to provide to script
:return: create session id for script executed
"""
request = ExecuteScriptRequest(script=script)
request = ExecuteScriptRequest(script=script, args=args)
response = self.stub.ExecuteScript(request)
return response.session_id if response.session_id else None

View file

@ -3,7 +3,6 @@ import logging
import os
import re
import tempfile
import threading
import time
from concurrent import futures
from pathlib import Path
@ -1236,13 +1235,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
def ExecuteScript(self, request, context):
existing_sessions = set(self.coreemu.sessions.keys())
file_path = Path(request.script)
thread = threading.Thread(
target=utils.execute_file,
args=(file_path, {"coreemu": self.coreemu}),
daemon=True,
)
thread.start()
thread.join()
utils.execute_script(self.coreemu, file_path, request.args)
current_sessions = set(self.coreemu.sessions.keys())
new_sessions = list(current_sessions.difference(existing_sessions))
new_session = -1

View file

@ -3,7 +3,6 @@ socket server request handlers leveraged by core servers.
"""
import logging
import shlex
import shutil
import socketserver
import sys
@ -935,9 +934,9 @@ class CoreHandler(socketserver.BaseRequestHandler):
if execute_server:
try:
logger.info("executing: %s", execute_server)
old_session_ids = set()
if message.flags & MessageFlags.STRING.value:
old_session_ids = set(self.coreemu.sessions.keys())
sys.argv = shlex.split(execute_server)
file_path = Path(sys.argv[0])
if file_path.suffix == ".xml":
session = self.coreemu.create_session()
@ -947,13 +946,7 @@ class CoreHandler(socketserver.BaseRequestHandler):
self.coreemu.delete_session(session.id)
raise
else:
thread = threading.Thread(
target=utils.execute_file,
args=(file_path, {"coreemu": self.coreemu}),
daemon=True,
)
thread.start()
thread.join()
utils.execute_script(self.coreemu, file_path, execute_server)
if message.flags & MessageFlags.STRING.value:
new_session_ids = set(self.coreemu.sessions.keys())

View file

@ -801,8 +801,8 @@ class CoreClient:
)
return config
def execute_script(self, script) -> None:
session_id = self.client.execute_script(script)
def execute_script(self, script: str, options: str) -> None:
session_id = self.client.execute_script(script, options)
logger.info("execute python script %s", session_id)
if session_id != -1:
self.join_session(session_id)

View file

@ -86,5 +86,5 @@ class ExecutePythonDialog(Dialog):
file = self.file_entry.get()
options = self.option_entry.get()
logger.info("Execute %s with options %s", file, options)
self.app.core.execute_script(file)
self.app.core.execute_script(file, options)
self.destroy()

View file

@ -15,6 +15,7 @@ import random
import shlex
import shutil
import sys
import threading
from pathlib import Path
from subprocess import PIPE, STDOUT, Popen
from typing import (
@ -39,6 +40,7 @@ from core.errors import CoreCommandError, CoreError
logger = logging.getLogger(__name__)
if TYPE_CHECKING:
from core.emulator.coreemu import CoreEmu
from core.emulator.session import Session
from core.nodes.base import CoreNode
T = TypeVar("T")
@ -47,6 +49,24 @@ DEVNULL = open(os.devnull, "wb")
IFACE_CONFIG_FACTOR: int = 1000
def execute_script(coreemu: "CoreEmu", file_path: Path, args: str) -> None:
"""
Provides utility function to execute a python script in context of the
provide coreemu instance.
:param coreemu: coreemu to provide to script
:param file_path: python script to execute
:param args: args to provide script
:return: nothing
"""
sys.argv = shlex.split(args)
thread = threading.Thread(
target=execute_file, args=(file_path, {"coreemu": coreemu}), daemon=True
)
thread.start()
thread.join()
def execute_file(
path: Path, exec_globals: Dict[str, str] = None, exec_locals: Dict[str, str] = None
) -> None:

View file

@ -466,6 +466,7 @@ message GetInterfacesResponse {
message ExecuteScriptRequest {
string script = 1;
string args = 2;
}
message ExecuteScriptResponse {