Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operation for executing python code on remote? #1079

Open
stone-w4tch3r opened this issue Mar 24, 2024 · 1 comment
Open

Operation for executing python code on remote? #1079

stone-w4tch3r opened this issue Mar 24, 2024 · 1 comment
Labels
Operations Issues with operations.

Comments

@stone-w4tch3r
Copy link
Contributor

stone-w4tch3r commented Mar 24, 2024

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 🤗

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 😊

@Fizzadar
Copy link
Member

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 Fizzadar assigned Fizzadar and unassigned Fizzadar May 26, 2024
@Fizzadar Fizzadar added the Operations Issues with operations. label May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Operations Issues with operations.
Projects
None yet
Development

No branches or pull requests

2 participants