Skip to content

Commit 2aac2eb

Browse files
committed
Updates
1 parent 300a6c3 commit 2aac2eb

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

safeexecute/__init__.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import os
22
import re
33
import logging
4-
import subprocess
54
import docker
6-
from docker.errors import ImageNotFound
75

86
IMAGE_NAME = "joshxt/safeexecute:latest"
97

@@ -13,16 +11,9 @@ def install_docker_image():
1311
try:
1412
client.images.get(IMAGE_NAME)
1513
logging.info(f"Image '{IMAGE_NAME}' found locally")
16-
except ImageNotFound:
14+
except:
1715
logging.info(f"Installing docker image '{IMAGE_NAME}' from Docker Hub")
18-
low_level_client = docker.APIClient()
19-
for line in low_level_client.pull(IMAGE_NAME, stream=True, decode=True):
20-
status = line.get("status")
21-
progress = line.get("progress")
22-
if status and progress:
23-
logging.info(f"{status}: {progress}")
24-
elif status:
25-
logging.info(status)
16+
client.images.pull(IMAGE_NAME)
2617
logging.info(f"Image '{IMAGE_NAME}' installed")
2718
return client
2819

@@ -33,13 +24,6 @@ async def execute_python_code(code: str, working_directory: str) -> str:
3324
os.makedirs(working_directory)
3425
# Check if there are any package requirements in the code to install
3526
package_requirements = re.findall(r"pip install (.*)", code)
36-
if package_requirements:
37-
# Install the required packages
38-
for package in package_requirements:
39-
try:
40-
subprocess.check_output(["pip", "install", package])
41-
except:
42-
pass
4327
if "```python" in code:
4428
code = code.split("```python")[1].split("```")[0]
4529
# Create a temporary Python file in the WORKSPACE directory
@@ -48,6 +32,29 @@ async def execute_python_code(code: str, working_directory: str) -> str:
4832
f.write(code)
4933
try:
5034
client = install_docker_image()
35+
if package_requirements:
36+
# Install the required packages in the container
37+
for package in package_requirements:
38+
try:
39+
logging.info(f"Installing package '{package}' in container")
40+
client.containers.run(
41+
IMAGE_NAME,
42+
f"pip install {package}",
43+
volumes={
44+
os.path.abspath(working_directory): {
45+
"bind": "/workspace",
46+
"mode": "ro",
47+
}
48+
},
49+
working_dir="/workspace",
50+
stderr=True,
51+
stdout=True,
52+
detach=True,
53+
)
54+
except Exception as e:
55+
logging.error(f"Error installing package '{package}': {str(e)}")
56+
return f"Error: {str(e)}"
57+
# Run the Python code in the container
5158
container = client.containers.run(
5259
IMAGE_NAME,
5360
f"python {temp_file}",
@@ -66,8 +73,10 @@ async def execute_python_code(code: str, working_directory: str) -> str:
6673
logs = container.logs().decode("utf-8")
6774
container.remove()
6875
os.remove(temp_file)
76+
logging.info(f"Python code executed successfully. Logs: {logs}")
6977
return logs
7078
except Exception as e:
79+
logging.error(f"Error executing Python code: {str(e)}")
7180
return f"Error: {str(e)}"
7281

7382

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setup(
1010
name="safeexecute",
11-
version="0.0.2",
11+
version="0.0.3",
1212
description="Safe way to execute Python code with containerization.",
1313
long_description=long_description,
1414
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)