diff --git a/daemon/core/api/grpc/client.py b/daemon/core/api/grpc/client.py index e3aab86a..783e80c0 100644 --- a/daemon/core/api/grpc/client.py +++ b/daemon/core/api/grpc/client.py @@ -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 diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index d8bb8078..a792c6ea 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -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 diff --git a/daemon/core/api/tlv/corehandlers.py b/daemon/core/api/tlv/corehandlers.py index 73a42af6..4efb22f6 100644 --- a/daemon/core/api/tlv/corehandlers.py +++ b/daemon/core/api/tlv/corehandlers.py @@ -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()) diff --git a/daemon/core/gui/coreclient.py b/daemon/core/gui/coreclient.py index 41753e5d..4905ac8a 100644 --- a/daemon/core/gui/coreclient.py +++ b/daemon/core/gui/coreclient.py @@ -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) diff --git a/daemon/core/gui/dialogs/executepython.py b/daemon/core/gui/dialogs/executepython.py index 8fe44616..8c9b31ba 100644 --- a/daemon/core/gui/dialogs/executepython.py +++ b/daemon/core/gui/dialogs/executepython.py @@ -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() diff --git a/daemon/core/utils.py b/daemon/core/utils.py index b7ef1e5c..fcc37842 100644 --- a/daemon/core/utils.py +++ b/daemon/core/utils.py @@ -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: diff --git a/daemon/proto/core/api/grpc/core.proto b/daemon/proto/core/api/grpc/core.proto index df66254c..1986dcef 100644 --- a/daemon/proto/core/api/grpc/core.proto +++ b/daemon/proto/core/api/grpc/core.proto @@ -466,6 +466,7 @@ message GetInterfacesResponse { message ExecuteScriptRequest { string script = 1; + string args = 2; } message ExecuteScriptResponse {