From 39282b1a38e7fe9c73eaa76aa9a11294c02586e7 Mon Sep 17 00:00:00 2001 From: Kiri Date: Mon, 15 Jul 2024 07:39:58 -0700 Subject: [PATCH] After-stream commit. --- SomePythonThingy/requirements.txt | 5 +++ SomePythonThingy/some_utility_script.py | 10 ++++++ TestPythonInExport.gd | 28 +++++++++++++--- .../KiriPythonBuildWrangler.gd | 1 + .../KiriPythonWrapperInstance.gd | 33 +++++++++++++++---- addons/KiriPythonRPCWrapper/TODO.md | 8 +++-- 6 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 SomePythonThingy/requirements.txt create mode 100644 SomePythonThingy/some_utility_script.py diff --git a/SomePythonThingy/requirements.txt b/SomePythonThingy/requirements.txt new file mode 100644 index 0000000..d1576dd --- /dev/null +++ b/SomePythonThingy/requirements.txt @@ -0,0 +1,5 @@ +mediapipe == 0.10.14 +numpy == 1.26.0 +psutil == 5.9.7 +cv2-enumerate-cameras == 1.1.10 + diff --git a/SomePythonThingy/some_utility_script.py b/SomePythonThingy/some_utility_script.py new file mode 100644 index 0000000..fd19c1e --- /dev/null +++ b/SomePythonThingy/some_utility_script.py @@ -0,0 +1,10 @@ +#!/usr/bin/python3 + +import time + +a = 10 +while a > 0: + print("asdf: ", a) + a -= 1 + time.sleep(1) + diff --git a/TestPythonInExport.gd b/TestPythonInExport.gd index 3383fc5..8815a4d 100644 --- a/TestPythonInExport.gd +++ b/TestPythonInExport.gd @@ -16,14 +16,32 @@ func _ready(): #asdfblah.emit() #pw.stop_process() # - - - - + pw = KiriPythonWrapperInstance.new( "res://SomePythonThingy/test_a_thing.py") pw.setup_python() - pw.start_process() + + #print("running command...") + #var utility_script_path = pw.convert_cache_item_to_real_path( + #"res://SomePythonThingy/some_utility_script.py") + #var r1 = pw.run_python_command([utility_script_path], [], true) + #print("done running command... ", r1) + + + print("running command...") + var requirements_txt_path = pw.convert_cache_item_to_real_path( + "res://SomePythonThingy/requirements.txt") + var output_array = [] + var r1 = pw.run_python_command( + ["-m", "pip", "install", "-r", requirements_txt_path], + output_array, true, true) + print("done running command... ", r1) + print("OUTPUT WAS:", output_array[0]) + + + + + pw.start_process(true) print("Call thingy...") var ret = await pw.call_rpc_async("some_function_to_call", []) print("Done thingy...") diff --git a/addons/KiriPythonRPCWrapper/KiriPythonBuildWrangler.gd b/addons/KiriPythonRPCWrapper/KiriPythonBuildWrangler.gd index d43b44c..f71e207 100644 --- a/addons/KiriPythonRPCWrapper/KiriPythonBuildWrangler.gd +++ b/addons/KiriPythonRPCWrapper/KiriPythonBuildWrangler.gd @@ -211,6 +211,7 @@ func unpack_python(overwrite : bool = false): var cache_path_godot : String = _get_cache_path_godot() + # FIXME: !!! THIS CAN END UP WITH PARTIAL INSTALLS !!! # Check to see if the Python executable already exists. If it does, we might # just skip unpacking. var python_executable_expected_path : String = \ diff --git a/addons/KiriPythonRPCWrapper/KiriPythonWrapperInstance.gd b/addons/KiriPythonRPCWrapper/KiriPythonWrapperInstance.gd index 2f139e0..670fe63 100644 --- a/addons/KiriPythonRPCWrapper/KiriPythonWrapperInstance.gd +++ b/addons/KiriPythonRPCWrapper/KiriPythonWrapperInstance.gd @@ -92,21 +92,40 @@ func get_status(): return KiriPythonWrapperStatus.STATUS_RUNNING -func start_process(open_terminal : bool = false): +func run_python_command( + args : PackedStringArray, + output : Array = [], + read_stderr : bool = false, + open_console : bool = false): - # FIXME: Make sure we don't have one running. - - var open_port = 9500 + var python_exe_path : String = _get_python_executable() - # Convert Python script path into a real path on the system. - var real_python_script_path = python_script_path + # Do a little switcheroo on Linux to open a console. + if open_console: + if OS.get_name() == "Linux": + args = PackedStringArray(["-e", python_exe_path]) + args + python_exe_path = "xterm" + + return OS.execute(python_exe_path, args, output, read_stderr, open_console) + +func convert_cache_item_to_real_path(path : String): + var real_python_script_path = path if real_python_script_path.begins_with("res://"): real_python_script_path = _build_wrangler._get_script_cache_path_system().path_join( real_python_script_path.substr(len("res://"))) else: real_python_script_path = ProjectSettings.globalize_path( real_python_script_path) - print("REAL PATH: ", real_python_script_path) + return real_python_script_path + +func start_process(open_terminal : bool = false): + + # FIXME: Make sure we don't have one running. + + var open_port = 9500 + + var real_python_script_path = convert_cache_item_to_real_path( + python_script_path) assert(not _server_packet_socket) _server_packet_socket = KiriPacketSocket.new() diff --git a/addons/KiriPythonRPCWrapper/TODO.md b/addons/KiriPythonRPCWrapper/TODO.md index a1bccd5..9c9ee56 100644 --- a/addons/KiriPythonRPCWrapper/TODO.md +++ b/addons/KiriPythonRPCWrapper/TODO.md @@ -9,11 +9,15 @@ Done: x Fix whatever this is: x example Python module from OUTSIDE the addon x Remove xterm dependency, or make it like a debug-only thing. + x Test on WINE/Windows. + x First-time setup of requirements (pip, etc). The big ones: - - First-time setup of requirements (pip, etc). - - Test on WINE/Windows. + - Deal with interrupted setup operations + - We check for the python.exe file in a given setup location to see if + we need to unpack stuff, but what if that exists but the setup was + interrupted and we're missing files? - Documentation. - how to use .kiri_export_python