Operation for executing python code on remote?
Is your feature request related to a problem? Please describe
When writing new operations, it's not easy to put complex logic into basic shell commands. If abstraction level is high, StringCommand becomes a complete bash script.
Consider creating json operation, smth like jq. No-dependency solution is either fetch file from remote, edit in-python and send back, or use horrible bash code within Command.
Describe the solution you'd like
It would be nice to execute arbitrary code on remote, python is pre-installed on almost all modern OS.
Ideas required
I am trying to implement such operation, but suggestions are needed :hugs:
Two approaches come in mind:
- Just pass
.pyfile and arguments to python CLI Easy, straightforward, but not very convenient/readable from client code - Use
inspectto get source code of passed lambda, write lambda call to file and execute More readable, but more dangerous too. Using variables from outer scope will throw at runtime, for example
Examples: 1.
import subprocess
def execute_remotely(py_file: str, args: list[str]):
subprocess.run(f"python3 {py_file} {' '.join(args)}", shell=True)
execute_remotely("script_to_run.py", ["Hello", "world"])
2:
import inspect
from typing import Callable
def prepare_executable_file(function: Callable, *args, **kwargs) -> str:
source_code = inspect.getsource(function)
return source_code + f"\n\n{function.__name__}(*{args}, **{kwargs})\n"
def test(a: int, b: int, c: int = 3) -> None:
print(a, b, c)
source = prepare_executable_file(test, 1, 2, c=4)
print(source)
with open("test_file.py", "w") as f:
f.write(source)
If you have vision of this operation or any concerns, please share 😊
Oh this is interesting, never thought about doing this! How would it be different to using the server.script op to upload and execute a python file?