1
1
import os
2
2
import re
3
3
import logging
4
- import subprocess
5
4
import docker
6
- from docker .errors import ImageNotFound
7
5
8
6
IMAGE_NAME = "joshxt/safeexecute:latest"
9
7
@@ -13,16 +11,9 @@ def install_docker_image():
13
11
try :
14
12
client .images .get (IMAGE_NAME )
15
13
logging .info (f"Image '{ IMAGE_NAME } ' found locally" )
16
- except ImageNotFound :
14
+ except :
17
15
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 )
26
17
logging .info (f"Image '{ IMAGE_NAME } ' installed" )
27
18
return client
28
19
@@ -33,13 +24,6 @@ async def execute_python_code(code: str, working_directory: str) -> str:
33
24
os .makedirs (working_directory )
34
25
# Check if there are any package requirements in the code to install
35
26
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
43
27
if "```python" in code :
44
28
code = code .split ("```python" )[1 ].split ("```" )[0 ]
45
29
# Create a temporary Python file in the WORKSPACE directory
@@ -48,6 +32,29 @@ async def execute_python_code(code: str, working_directory: str) -> str:
48
32
f .write (code )
49
33
try :
50
34
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
51
58
container = client .containers .run (
52
59
IMAGE_NAME ,
53
60
f"python { temp_file } " ,
@@ -66,8 +73,10 @@ async def execute_python_code(code: str, working_directory: str) -> str:
66
73
logs = container .logs ().decode ("utf-8" )
67
74
container .remove ()
68
75
os .remove (temp_file )
76
+ logging .info (f"Python code executed successfully. Logs: { logs } " )
69
77
return logs
70
78
except Exception as e :
79
+ logging .error (f"Error executing Python code: { str (e )} " )
71
80
return f"Error: { str (e )} "
72
81
73
82
0 commit comments