Skip to content

Commit

Permalink
Accomodate running in container
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-XT committed Nov 13, 2023
1 parent 12bc539 commit 343ebe5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ You can pass an entire message from a langauge model into the `code` field and i
from safeexecute import execute_python_code

code = "print('Hello, World!')"

result = execute_python_code(code=code)
print(result)
```
7 changes: 5 additions & 2 deletions safeexecute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def install_docker_image():
def execute_python_code(code: str, working_directory: str = None) -> str:
if working_directory is None:
working_directory = os.path.join(os.getcwd(), "WORKSPACE")
docker_working_dir = working_directory
if os.environ.get("DOCKER_CONTAINER", False):
docker_working_dir = os.environ.get("WORKING_DIRECTORY", working_directory)
if not os.path.exists(working_directory):
os.makedirs(working_directory)
# Check if there are any package requirements in the code to install
Expand All @@ -43,7 +46,7 @@ def execute_python_code(code: str, working_directory: str = None) -> str:
IMAGE_NAME,
f"pip install {package}",
volumes={
os.path.abspath(working_directory): {
os.path.abspath(docker_working_dir): {
"bind": "/workspace",
"mode": "rw",
}
Expand All @@ -61,7 +64,7 @@ def execute_python_code(code: str, working_directory: str = None) -> str:
IMAGE_NAME,
f"python /workspace/temp.py",
volumes={
os.path.abspath(working_directory): {
os.path.abspath(docker_working_dir): {
"bind": "/workspace",
"mode": "rw",
}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="safeexecute",
version="0.0.8",
version="0.0.9",
description="Safe way to execute Python code with containerization.",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 343ebe5

Please sign in to comment.