-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_system_tools.py
More file actions
127 lines (107 loc) · 2.97 KB
/
Copy pathfile_system_tools.py
File metadata and controls
127 lines (107 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Custom tools for file system operations
"""
import os
import subprocess
from typing import List
from .tools import register_tool
@register_tool
def run_shell_command(command: str) -> str:
"""
Executes a shell command.
Args:
command: The command to execute.
Returns:
The output of the command.
"""
try:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.returncode == 0:
return f"Command executed successfully:\n{result.stdout}"
else:
return f"Error executing command:\n{result.stderr}"
except Exception as e:
return f"Error executing command: {e}"
@register_tool
def write_file(path: str, content: str) -> str:
"""
Writes content to a file.
Args:
path: The path to the file.
content: The content to write.
Returns:
A confirmation message.
"""
try:
with open(path, 'w') as f:
f.write(content)
return f"File '{path}' written successfully."
except Exception as e:
return f"Error writing file: {e}"
@register_tool
def read_file(path: str) -> str:
"""
Reads the content of a file.
Args:
path: The path to the file.
Returns:
The content of the file, or an error message.
"""
try:
with open(path, 'r') as f:
return f.read()
except Exception as e:
return f"Error reading file: {e}"
@register_tool
def replace_text(path: str, old_text: str, new_text: str) -> str:
"""
Replaces text in a file.
Args:
path: The path to the file.
old_text: The text to replace.
new_text: The new text.
Returns:
A confirmation message.
"""
try:
with open(path, 'r') as f:
content = f.read()
content = content.replace(old_text, new_text)
with open(path, 'w') as f:
f.write(content)
return f"Text in '{path}' replaced successfully."
except Exception as e:
return f"Error replacing text: {e}"
@register_tool
def list_files_recursive(path: str) -> List[str]:
"""
Lists all files in a directory and its subdirectories.
Args:
path: The path to the directory.
Returns:
A list of file paths.
"""
try:
file_list = []
for root, _, files in os.walk(path):
for file in files:
file_list.append(os.path.join(root, file))
return file_list
except Exception as e:
return [f"Error listing files: {e}"]
@register_tool
def create_directory(path: str) -> str:
"""
Creates a new directory.
Args:
path: The path to the directory to create.
Returns:
A confirmation message.
"""
try:
os.makedirs(path, exist_ok=True)
return f"Directory '{path}' created successfully."
except Exception as e:
return f"Error creating directory: {e}"