File size: 2,683 Bytes
017531a
 
 
f531709
 
 
017531a
f531709
 
7429e74
 
 
 
 
 
 
 
 
 
017531a
 
 
f531709
 
 
7429e74
 
f531709
8a0a337
 
 
 
017531a
8a0a337
7429e74
8a0a337
 
 
7429e74
 
 
 
8a0a337
 
 
 
 
 
7429e74
8a0a337
7429e74
8a0a337
 
 
 
017531a
7429e74
8a0a337
 
 
f531709
7429e74
8a0a337
 
 
7429e74
f531709
8a0a337
f531709
8a0a337
7429e74
f531709
017531a
8a0a337
 
017531a
8a0a337
 
 
 
7429e74
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import subprocess
import sys
import os
import tempfile
from fastapi import FastAPI
from pydantic import BaseModel, Field

# Defines the structure of the request body
class ExecutionRequest(BaseModel):
    package_name: str = Field(
        ...,
        example="numpy requests beautifulsoup4",
        description="One or more pip packages to install (space or comma separated)."
    )
    script_content: str = Field(
        ...,
        example="import numpy as np; print(np.array([1, 2, 3]))",
        description="The Python script content to execute."
    )

app = FastAPI()

@app.post("/execute/")
def install_and_execute(request: ExecutionRequest):
    """
    Installs one or more packages into a temporary directory and executes a Python script.
    Supports multiple packages separated by spaces or commas.
    """
    with tempfile.TemporaryDirectory() as temp_dir:
        pip_output = ""
        script_output = ""
        error = ""

        try:
            # 1. Create the install directory
            install_path = os.path.join(temp_dir, "packages")
            os.makedirs(install_path)

            # 2. Split package names by space or comma
            packages = [pkg.strip() for pkg in request.package_name.replace(",", " ").split() if pkg.strip()]

            # 3. Install the packages to the temporary directory
            pip_output = subprocess.check_output(
                [
                    sys.executable,
                    "-m",
                    "pip",
                    "install",
                    *packages,  # multiple packages supported
                    "--target",
                    install_path,
                ],
                stderr=subprocess.STDOUT,
                text=True
            )

            # 4. Create a temporary script file
            script_path = os.path.join(temp_dir, "script.py")
            with open(script_path, 'w') as temp_script:
                temp_script.write(request.script_content)

            # 5. Prepare the environment with custom PYTHONPATH
            exec_env = os.environ.copy()
            exec_env["PYTHONPATH"] = install_path + os.pathsep + exec_env.get("PYTHONPATH", "")

            # 6. Execute the Python script
            script_output = subprocess.check_output(
                [sys.executable, script_path],
                stderr=subprocess.STDOUT,
                text=True,
                env=exec_env
            )

        except subprocess.CalledProcessError as e:
            error = e.output

        return {
            "pip_install_output": pip_output,
            "script_execution_output": script_output,
            "error_output": error
        }