pyinfra icon indicating copy to clipboard operation
pyinfra copied to clipboard

Operation for executing python code on remote?

Open stone-w4tch3r opened this issue 2 years ago • 1 comments

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:

  1. Just pass .py file and arguments to python CLI Easy, straightforward, but not very convenient/readable from client code
  2. Use inspect to 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 😊

stone-w4tch3r avatar Mar 24 '24 18:03 stone-w4tch3r

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?

Fizzadar avatar May 26 '24 08:05 Fizzadar