Skip to content

Commit

Permalink
feat: change "package" to "modulepath" and include it in module execu…
Browse files Browse the repository at this point in the history
…tion resolution
  • Loading branch information
WinPlay02 committed Nov 28, 2023
1 parent 0f5e849 commit b82df25
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/safeds_runner/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def ws_main(ws: simple_websocket.Server) -> None:
code = request_data["code"]
msg_main = request_data["main"]
# This should only be called from the extension as it is a security risk
execute_pipeline(code, msg_main["package"], msg_main["module"], msg_main["pipeline"], execution_id)
execute_pipeline(code, msg_main["modulepath"], msg_main["module"], msg_main["pipeline"], execution_id)
case "placeholder_query":
valid, invalid_message = messages.validate_placeholder_query_message(request_data)
if not valid:
Expand Down
2 changes: 1 addition & 1 deletion src/safeds_runner/server/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def validate_program_message(message_data: dict[str, typing.Any] | str) -> tuple
return False, "No 'main' parameter given"
if (
not isinstance(message_data["main"], dict)
or "package" not in message_data["main"]
or "modulepath" not in message_data["main"]
or "module" not in message_data["main"]
or "pipeline" not in message_data["main"]
):
Expand Down
17 changes: 9 additions & 8 deletions src/safeds_runner/server/pipeline_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class PipelineProcess:
def __init__(
self,
code: dict[str, dict[str, str]],
sdspackage: str,
modulepath: str,
sdsmodule: str,
sdspipeline: str,
execution_id: str,
Expand All @@ -89,15 +89,15 @@ def __init__(
Create a new process which will execute the given pipeline, when started.
:param code: A dictionary containing the code to be executed, in a virtual filesystem
:param sdspackage: Safe-DS package name
:param modulepath: Relative path to the main module
:param sdsmodule: Safe-DS module name
:param sdspipeline: Safe-DS main pipeline name
:param execution_id: Unique ID to identify this process
:param messages_queue: A queue to write outgoing messages to
:param placeholder_map: A map to save calculated placeholders in
"""
self.code = code
self.sdspackage = sdspackage
self.modulepath = modulepath
self.sdsmodule = sdsmodule
self.sdspipeline = sdspipeline
self.id = execution_id
Expand All @@ -124,14 +124,15 @@ def save_placeholder(self, placeholder_name: str, value: typing.Any) -> None:
self._send_message("placeholder_type", create_placeholder_description(placeholder_name, placeholder_type))

def _execute(self) -> None:
logging.info("Executing %s.%s.%s...", self.sdspackage, self.sdsmodule, self.sdspipeline)
logging.info("Executing %s.%s.%s...", self.modulepath, self.sdsmodule, self.sdspipeline)
pipeline_finder = InMemoryFinder(self.code)
pipeline_finder.attach()
main_module = f"gen_{self.sdsmodule}_{self.sdspipeline}"
global current_pipeline # noqa: PLW0603
current_pipeline = self
try:
runpy.run_module(main_module, run_name="__main__") # TODO Is the Safe-DS-Package relevant here?
runpy.run_module(main_module if len(self.modulepath) == 0 else f"{self.modulepath}.{main_module}",
run_name="__main__")
self._send_message("progress", create_runtime_progress_done())
except BaseException as error: # noqa: BLE001
self._send_exception(error)
Expand Down Expand Up @@ -177,7 +178,7 @@ def get_backtrace_info(error: BaseException) -> list[dict[str, typing.Any]]:

def execute_pipeline(
code: dict[str, dict[str, str]],
sdspackage: str,
modulepath: str,
sdsmodule: str,
sdspipeline: str,
exec_id: str,
Expand All @@ -186,7 +187,7 @@ def execute_pipeline(
Run a Safe-DS pipeline.
:param code: A dictionary containing the code to be executed, in a virtual filesystem
:param sdspackage: Safe-DS package name
:param modulepath: Relative path to the main module
:param sdsmodule: Safe-DS module name
:param sdspipeline: Safe-DS main pipeline name
:param exec_id: Unique ID to identify this execution
Expand All @@ -196,7 +197,7 @@ def execute_pipeline(
global_placeholder_map[exec_id] = multiprocessing_manager.dict()
process = PipelineProcess(
code,
sdspackage,
modulepath,
sdsmodule,
sdspipeline,
exec_id,
Expand Down
24 changes: 12 additions & 12 deletions tests/safeds_runner/server/test_websocket_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@ def test_websocket_no_json() -> None:
({"type": "program", "id": "1234", "data": "a"}, "Message data is not a JSON object"),
({"type": "placeholder_query", "id": "123", "data": {"a": "v"}}, "Message data is not a string"),
(
{"type": "program", "id": "1234", "data": {"main": {"package": "1", "module": "2", "pipeline": "3"}}},
{"type": "program", "id": "1234", "data": {"main": {"modulepath": "1", "module": "2", "pipeline": "3"}}},
"No 'code' parameter given",
),
({"type": "program", "id": "1234", "data": {"code": {"": {"entry": ""}}}}, "No 'main' parameter given"),
(
{
"type": "program",
"id": "1234",
"data": {"code": {"": {"entry": ""}}, "main": {"package": "1", "module": "2"}},
"data": {"code": {"": {"entry": ""}}, "main": {"modulepath": "1", "module": "2"}},
},
"Invalid 'main' parameter given",
),
(
{
"type": "program",
"id": "1234",
"data": {"code": {"": {"entry": ""}}, "main": {"package": "1", "pipeline": "3"}},
"data": {"code": {"": {"entry": ""}}, "main": {"modulepath": "1", "pipeline": "3"}},
},
"Invalid 'main' parameter given",
),
Expand All @@ -90,7 +90,7 @@ def test_websocket_no_json() -> None:
"id": "1234",
"data": {
"code": {"": {"entry": ""}},
"main": {"package": "1", "module": "2", "pipeline": "3", "other": "4"},
"main": {"modulepath": "1", "module": "2", "pipeline": "3", "other": "4"},
},
},
"Invalid 'main' parameter given",
Expand All @@ -101,7 +101,7 @@ def test_websocket_no_json() -> None:
"id": "1234",
"data": {
"code": {"": {"entry": ""}},
"main": {"package": "1", "module": "2", "pipeline": "3", "other": {"4": "a"}},
"main": {"modulepath": "1", "module": "2", "pipeline": "3", "other": {"4": "a"}},
},
},
"Invalid 'main' parameter given",
Expand All @@ -110,23 +110,23 @@ def test_websocket_no_json() -> None:
{
"type": "program",
"id": "1234",
"data": {"code": "a", "main": {"package": "1", "module": "2", "pipeline": "3"}},
"data": {"code": "a", "main": {"modulepath": "1", "module": "2", "pipeline": "3"}},
},
"Invalid 'code' parameter given",
),
(
{
"type": "program",
"id": "1234",
"data": {"code": {"": "a"}, "main": {"package": "1", "module": "2", "pipeline": "3"}},
"data": {"code": {"": "a"}, "main": {"modulepath": "1", "module": "2", "pipeline": "3"}},
},
"Invalid 'code' parameter given",
),
(
{
"type": "program",
"id": "1234",
"data": {"code": {"": {"a": {"b": "c"}}}, "main": {"package": "1", "module": "2", "pipeline": "3"}},
"data": {"code": {"": {"a": {"b": "c"}}}, "main": {"modulepath": "1", "module": "2", "pipeline": "3"}},
},
"Invalid 'code' parameter given",
),
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_websocket_progress_message_done() -> None:
"a": {"stub": "def u():\n\treturn 1"},
"v.u.s": {"testing": "import a.stub;\n\ndef add1(v1, v2):\n\treturn v1 + v2 + a.stub.u()\n"},
},
"main": {"package": "", "module": "b", "pipeline": "c"},
"main": {"modulepath": "", "module": "b", "pipeline": "c"},
},
}
mock_connection = MockWebsocketConnection([json.dumps(code_message)])
Expand All @@ -215,7 +215,7 @@ def test_websocket_progress_message_done() -> None:
)
def test_websocket_exception_message() -> None:
setup_pipeline_execution()
code_id = "abcdefg"
code_id = "abcdefgh"
code_message = {
"type": "program",
"id": code_id,
Expand All @@ -226,7 +226,7 @@ def test_websocket_exception_message() -> None:
"gen_test_a_pipe": "from gen_test_a import pipe\n\nif __name__ == '__main__':\n\tpipe()",
},
},
"main": {"package": "", "module": "test_a", "pipeline": "pipe"},
"main": {"modulepath": "", "module": "test_a", "pipeline": "pipe"},
},
}
mock_connection = MockWebsocketConnection([json.dumps(code_message)])
Expand Down Expand Up @@ -272,7 +272,7 @@ def test_websocket_placeholder_valid() -> None:
"gen_test_a_pipe": "from gen_test_a import pipe\n\nif __name__ == '__main__':\n\tpipe()",
},
},
"main": {"package": "", "module": "test_a", "pipeline": "pipe"},
"main": {"modulepath": "", "module": "test_a", "pipeline": "pipe"},
},
}
mock_connection = MockWebsocketConnection([json.dumps(code_message)])
Expand Down

0 comments on commit b82df25

Please sign in to comment.