Fix and consolidate command workspace resolution

This commit is contained in:
Reinier van der Leer
2023-04-16 18:52:22 +02:00
committed by Pi
parent 35175fc19b
commit 11620cc571
5 changed files with 59 additions and 53 deletions

39
autogpt/workspace.py Normal file
View File

@@ -0,0 +1,39 @@
import os
from pathlib import Path
# Set a dedicated folder for file I/O
WORKSPACE_PATH = Path(os.getcwd()) / "auto_gpt_workspace"
# Create the directory if it doesn't exist
if not os.path.exists(WORKSPACE_PATH):
os.makedirs(WORKSPACE_PATH)
def path_in_workspace(relative_path: str | Path) -> Path:
"""Get full path for item in workspace
Parameters:
relative_path (str | Path): Path to translate into the workspace
Returns:
Path: Absolute path for the given path in the workspace
"""
return safe_path_join(WORKSPACE_PATH, relative_path)
def safe_path_join(base: Path, *paths: str | Path) -> Path:
"""Join one or more path components, asserting the resulting path is within the workspace.
Args:
base (Path): The base path
*paths (str): The paths to join to the base path
Returns:
Path: The joined path
"""
joined_path = base.joinpath(*paths).resolve()
if not joined_path.is_relative_to(base):
raise ValueError(f"Attempted to access path '{joined_path}' outside of working directory '{base}'.")
return joined_path