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) response = self.stub.GetEmaneEventChannel(request)
return wrappers.EmaneEventChannel.from_proto(response) 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. Executes a python script given context of the current CoreEmu object.
:param script: script to execute :param script: script to execute
:param args: arguments to provide to script
:return: create session id for script executed :return: create session id for script executed
""" """
request = ExecuteScriptRequest(script=script) request = ExecuteScriptRequest(script=script, args=args)
response = self.stub.ExecuteScript(request) response = self.stub.ExecuteScript(request)
return response.session_id if response.session_id else None return response.session_id if response.session_id else None

View file

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

View file

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

View file

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

View file

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

View file

@ -15,6 +15,7 @@ import random
import shlex import shlex
import shutil import shutil
import sys import sys
import threading
from pathlib import Path from pathlib import Path
from subprocess import PIPE, STDOUT, Popen from subprocess import PIPE, STDOUT, Popen
from typing import ( from typing import (
@ -39,6 +40,7 @@ from core.errors import CoreCommandError, CoreError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
if TYPE_CHECKING: if TYPE_CHECKING:
from core.emulator.coreemu import CoreEmu
from core.emulator.session import Session from core.emulator.session import Session
from core.nodes.base import CoreNode from core.nodes.base import CoreNode
T = TypeVar("T") T = TypeVar("T")
@ -47,6 +49,24 @@ DEVNULL = open(os.devnull, "wb")
IFACE_CONFIG_FACTOR: int = 1000 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( def execute_file(
path: Path, exec_globals: Dict[str, str] = None, exec_locals: Dict[str, str] = None path: Path, exec_globals: Dict[str, str] = None, exec_locals: Dict[str, str] = None
) -> None: ) -> None:

View file

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