Skip to content

Enhancement: Python 3.x Execution enablement? #38

@SomeGuru

Description

@SomeGuru

Hi Ryan,

Any chance you are able to incorporate Python execution into the mix on the plugin? I ask because I have a large library of python code that usually I install to the machines manually, but would love to be able to use your plugin to execute the python code directly via input or upload of the python script file.

Any chance you could do this for me?

Thanks,
SomeGuru

Some random code like stuff I was thinking on, but couldn't implement. I also wrote for Ubuntu, but need it universal for all OSes like Windows, Apple, Linux (distros RHEL, Debian, etc).

import os
import subprocess

def executePythonScript(mesh, params):
  # Check if Python is installed on the agent system
  pythonCheck = subprocess.run(["python", "--version"], stdout=subprocess.PIPE)
  if pythonCheck.returncode != 0:
    # Python is not installed on the agent system, so we will install it
    print("Python is not installed on the agent system. Installing...")
    subprocess.run(["apt-get", "install", "-y", "python3"])
  
  # Check if the required Python dependencies are present
  # Replace "dependency1" and "dependency2" with the actual dependencies needed
  dependencyCheck = subprocess.run(["pip3", "show", "dependency1"], stdout=subprocess.PIPE)
  if dependencyCheck.returncode != 0:
    # Dependency1 is not installed, so we will install it
    print("Dependency1 is not installed. Installing...")
    subprocess.run(["pip3", "install", "dependency1"])
  dependencyCheck = subprocess.run(["pip3", "show", "dependency2"], stdout=subprocess.PIPE)
  if dependencyCheck.returncode != 0:
    # Dependency2 is not installed, so we will install it
    print("Dependency2 is not installed. Installing...")
    subprocess.run(["pip3", "install", "dependency2"])
  
  # Check if a Python file or text input was provided
  if "pythonFile" in params:
    # A Python file was provided, so we will execute it
    print("Executing Python file...")
    subprocess.run(["python3", params["pythonFile"]])
  else:
    # No Python file was provided, so we will accept text input in a console window
    print("Enter Python code to be executed:")
    code = input()
    print("Executing Python code...")
    subprocess.run(["python3", "-c", code])

with a registration of the plugin via:

mesh.registerRpcMethod("executePythonScript", executePythonScript)

To use this plugin, the user can call the "executePythonScript" RPC method and pass in the parameters "pythonFile" (optional) and "text" (optional). If "pythonFile" is provided, the plugin will execute the specified Python file. If "text" is provided, the plugin will execute the provided Python code. If neither is provided, the plugin will accept text input in a console window.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions