Skip to content

Commit

Permalink
fix: Add input validation and error handling for paths
Browse files Browse the repository at this point in the history
  • Loading branch information
lxobr committed Nov 19, 2024
1 parent b1f3a3f commit 3f44546
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions cognee/tasks/repo_processor/local_script_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,29 @@ async def get_local_script_dependencies(script_path: str, repo_path: Optional[st
"""
Extract and return a list of unique module paths that the script depends on.
"""
if repo_path:
repo_path_resolved = str(Path(repo_path).resolve())
with add_sys_path(repo_path_resolved):
dependencies = await _extract_dependencies(script_path)
dependencies = [path for path in dependencies if path.startswith(repo_path_resolved)]
else:
try:
script_path = Path(script_path).resolve(strict=True)
except (FileNotFoundError, PermissionError) as e:
logger.error(f"Error resolving script path: {e}")
return []

if not repo_path:
return await _extract_dependencies(script_path)

try:
repo_path = Path(repo_path).resolve(strict=True)
except (FileNotFoundError, PermissionError) as e:
logger.warning(f"Error resolving repo path: {e}. Proceeding without repo_path.")
return await _extract_dependencies(script_path)

if not script_path.is_relative_to(repo_path):
logger.warning(f"Script {script_path} not in repo {repo_path}. Proceeding without repo_path.")
return await _extract_dependencies(script_path)

with add_sys_path(str(repo_path)):
dependencies = await _extract_dependencies(script_path)
return dependencies

return [path for path in dependencies if path.startswith(str(repo_path))]


if __name__ == "__main__":
Expand Down

0 comments on commit 3f44546

Please sign in to comment.