File size: 3,779 Bytes
6a27ac3 | 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | """
=====================================
Shulker Code — Code Executor Plugin
Safely run generated code snippets
Developed by @kopeedev / CyeroX
=====================================
Plugin that executes Python code in an isolated subprocess
with a timeout, so you can test generated code on the fly.
"""
import subprocess
import sys
import os
import tempfile
import textwrap
from typing import Tuple, Optional
class CodeExecutorPlugin:
"""
Execute Python code snippets safely in a subprocess.
Captures stdout, stderr, and return code.
Enforces a time limit to prevent infinite loops.
"""
def __init__(self, timeout_seconds: int = 10, max_output_chars: int = 4096):
self.timeout = timeout_seconds
self.max_output = max_output_chars
def run_python(self, code: str) -> Tuple[bool, str, str]:
"""
Execute Python code and return (success, stdout, stderr).
Args:
code: Python source code to execute
Returns:
(success, stdout, stderr)
"""
# Write code to a temporary file
with tempfile.NamedTemporaryFile(
mode="w",
suffix=".py",
delete=False,
encoding="utf-8",
) as f:
f.write(textwrap.dedent(code))
temp_path = f.name
try:
result = subprocess.run(
[sys.executable, temp_path],
capture_output=True,
text=True,
timeout=self.timeout,
)
stdout = result.stdout[:self.max_output]
stderr = result.stderr[:self.max_output]
success = result.returncode == 0
return success, stdout, stderr
except subprocess.TimeoutExpired:
return False, "", f"⏱️ Execution timed out after {self.timeout}s"
except Exception as e:
return False, "", f"❌ Execution error: {str(e)}"
finally:
os.unlink(temp_path)
def format_result(self, success: bool, stdout: str, stderr: str) -> str:
"""Format execution result for display."""
lines = []
if success:
lines.append("✅ Code executed successfully")
if stdout.strip():
lines.append("\n📤 Output:")
lines.append(stdout)
else:
lines.append("❌ Execution failed")
if stderr.strip():
lines.append("\n🔴 Error:")
lines.append(stderr)
if stdout.strip():
lines.append("\n📤 Partial output:")
lines.append(stdout)
return "\n".join(lines)
class SyntaxChecker:
"""
Check Python code for syntax errors without executing it.
Uses Python's built-in `compile()` function.
"""
def check(self, code: str) -> Tuple[bool, Optional[str]]:
"""
Check code syntax.
Returns:
(is_valid, error_message_or_None)
"""
try:
compile(code, "<string>", "exec")
return True, None
except SyntaxError as e:
msg = f"SyntaxError at line {e.lineno}: {e.msg}"
if e.text:
msg += f"\n {e.text.rstrip()}"
if e.offset:
msg += f"\n {' ' * (e.offset - 1)}^"
return False, msg
except Exception as e:
return False, str(e)
# Plugin registry
PLUGINS = {
"code_executor": CodeExecutorPlugin,
"syntax_checker": SyntaxChecker,
}
def load_plugin(name: str, **kwargs):
"""Load a plugin by name."""
if name not in PLUGINS:
raise ValueError(f"Unknown plugin: {name}. Available: {list(PLUGINS.keys())}")
return PLUGINS[name](**kwargs)
|