Update tools.py
Browse files
tools.py
CHANGED
|
@@ -149,16 +149,50 @@ def read_excel_file(file_path: str) -> Dict[str, Any]:
|
|
| 149 |
except Exception as e:
|
| 150 |
return {"error": f"Failed to read Excel file: {str(e)}"}
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
def execute_python_code(file_path: str, timeout: int = 30) -> str:
|
| 153 |
"""Execute Python code safely with subprocess"""
|
| 154 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
# Run in isolated subprocess with timeout
|
| 156 |
result = subprocess.run(
|
| 157 |
['python', file_path],
|
| 158 |
capture_output=True,
|
| 159 |
text=True,
|
| 160 |
timeout=timeout,
|
| 161 |
-
cwd=
|
| 162 |
)
|
| 163 |
|
| 164 |
if result.returncode == 0:
|
|
|
|
| 149 |
except Exception as e:
|
| 150 |
return {"error": f"Failed to read Excel file: {str(e)}"}
|
| 151 |
|
| 152 |
+
# def execute_python_code(file_path: str, timeout: int = 30) -> str:
|
| 153 |
+
# """Execute Python code safely with subprocess"""
|
| 154 |
+
# try:
|
| 155 |
+
# # Run in isolated subprocess with timeout
|
| 156 |
+
# result = subprocess.run(
|
| 157 |
+
# ['python', file_path],
|
| 158 |
+
# capture_output=True,
|
| 159 |
+
# text=True,
|
| 160 |
+
# timeout=timeout,
|
| 161 |
+
# cwd=tempfile.gettempdir() # Run in temp directory
|
| 162 |
+
# )
|
| 163 |
+
|
| 164 |
+
# if result.returncode == 0:
|
| 165 |
+
# return result.stdout.strip()
|
| 166 |
+
# else:
|
| 167 |
+
# return f"Error: {result.stderr}"
|
| 168 |
+
|
| 169 |
+
# except subprocess.TimeoutExpired:
|
| 170 |
+
# return "Error: Code execution timed out"
|
| 171 |
+
# except Exception as e:
|
| 172 |
+
# return f"Error executing code: {str(e)}"
|
| 173 |
+
|
| 174 |
def execute_python_code(file_path: str, timeout: int = 30) -> str:
|
| 175 |
"""Execute Python code safely with subprocess"""
|
| 176 |
try:
|
| 177 |
+
# Check if file exists, if not try common locations
|
| 178 |
+
if not os.path.exists(file_path):
|
| 179 |
+
# Try in downloads directory
|
| 180 |
+
alt_path = os.path.join("downloads", os.path.basename(file_path))
|
| 181 |
+
if os.path.exists(alt_path):
|
| 182 |
+
file_path = alt_path
|
| 183 |
+
else:
|
| 184 |
+
return f"Error: File not found at {file_path} or {alt_path}"
|
| 185 |
+
|
| 186 |
+
# Ensure we have absolute path
|
| 187 |
+
file_path = os.path.abspath(file_path)
|
| 188 |
+
|
| 189 |
# Run in isolated subprocess with timeout
|
| 190 |
result = subprocess.run(
|
| 191 |
['python', file_path],
|
| 192 |
capture_output=True,
|
| 193 |
text=True,
|
| 194 |
timeout=timeout,
|
| 195 |
+
cwd=os.path.dirname(file_path) # Run in the file's directory
|
| 196 |
)
|
| 197 |
|
| 198 |
if result.returncode == 0:
|