-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.py
More file actions
257 lines (214 loc) · 12.8 KB
/
Copy pathexecutor.py
File metadata and controls
257 lines (214 loc) · 12.8 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import os
import shutil
class CodeExecutor:
def __init__(self, db):
self.db = db
def execute_session(self, project_id, root_dir, instructions, log_callback=None):
exec_id = self.db.create_execution(project_id)
project_files = {rel: is_ro for rel, is_ro in self.db.get_project_files(project_id)}
overall_success = True
abs_root = os.path.abspath(root_dir)
for item in instructions:
rel_path = item['path']
abs_path = os.path.normpath(os.path.join(abs_root, rel_path))
# Защита от Path Traversal: не даем выйти за пределы корневой директории проекта через ../
if not abs_path.startswith(abs_root + os.sep) and abs_path != abs_root:
err = f"Ошибка: Запрещено. Попытка выхода за пределы папки проекта ({rel_path})."
if log_callback: log_callback(err)
self.db.add_change(exec_id, rel_path, item['type'], item.get('search', ''), item.get('replace', ''),
None, 'Error', err)
overall_success = False
continue
# Проверка прав: существующие файлы можно менять ТОЛЬКО если они явно отмечены как Writable (0)
if os.path.exists(abs_path):
if project_files.get(rel_path) != 0:
err = f"Ошибка: Запись запрещена. Файл {rel_path} не отмечен как [WRITABLE] в проекте."
if log_callback: log_callback(err)
self.db.add_change(exec_id, rel_path, item['type'], item.get('search', ''), item.get('replace', ''),
None, 'Error', err)
overall_success = False
continue
if item['type'] == 'create':
if os.path.exists(abs_path):
err = f"Ошибка: Нельзя создать {rel_path}, файл уже существует."
if log_callback: log_callback(err)
self.db.add_change(exec_id, rel_path, 'create', '', item['content'], None, 'Error', err)
overall_success = False
else:
try:
os.makedirs(os.path.dirname(abs_path), exist_ok=True)
with open(abs_path, 'w', encoding='utf-8') as f:
f.write(item['content'])
self.db.add_change(exec_id, rel_path, 'create', '', item['content'], None, 'Success')
# Автоматически прописываем новый файл в базу проекта как Writable
project_files[rel_path] = 0
current_list = self.db.get_project_files(project_id)
current_list.append((rel_path, 0))
self.db.set_project_files(project_id, current_list)
if log_callback: log_callback(f"Создан файл: {rel_path}")
except Exception as e:
err = f"Ошибка создания {rel_path}: {str(e)}"
if log_callback: log_callback(err)
self.db.add_change(exec_id, rel_path, 'create', '', item['content'], None, 'Error', err)
overall_success = False
elif item['type'] == 'edit':
if not os.path.exists(abs_path):
err = f"Ошибка: Файл {rel_path} не найден для изменения."
if log_callback: log_callback(err)
self.db.add_change(exec_id, rel_path, 'edit', item['search'], item['replace'], None, 'Error', err)
overall_success = False
continue
try:
with open(abs_path, 'r', encoding='utf-8') as f:
original_content = f.read()
new_content = None
if item['replace_all']:
new_content = item['replace']
else:
# Поиск с допуском пробелов
if item['search'] in original_content:
new_content = original_content.replace(item['search'], item['replace'])
else:
# Пробуем без лишних концевых пробелов
s_clean = "\n".join([line.rstrip() for line in item['search'].splitlines()])
o_clean_lines = [line.rstrip() for line in original_content.splitlines()]
o_clean_text = "\n".join(o_clean_lines)
if s_clean in o_clean_text:
new_content = o_clean_text.replace(s_clean, item['replace'])
if new_content is not None:
with open(abs_path, 'w', encoding='utf-8') as f:
f.write(new_content)
self.db.add_change(exec_id, rel_path, 'edit', item['search'], item['replace'], original_content,
'Success')
if log_callback: log_callback(f"Изменен файл: {rel_path}")
else:
err = f"Ошибка: Блок поиска не найден в файле {rel_path}"
if log_callback: log_callback(err)
self.db.add_change(exec_id, rel_path, 'edit', item['search'], item['replace'], original_content,
'Error', err)
overall_success = False
except Exception as e:
err = f"Ошибка изменения {rel_path}: {str(e)}"
if log_callback: log_callback(err)
self.db.add_change(exec_id, rel_path, 'edit', item['search'], item['replace'], None, 'Error', err)
overall_success = False
status_str = "Success" if overall_success else "Error"
self.db.update_execution_status(exec_id, status_str)
return overall_success
# --- Добавить внутрь класса CodeExecutor в executor.py ---
def is_modified_manually(self, change_id, root_dir):
cursor = self.db.conn.execute(
"SELECT file_path, change_type, search_content, replace_content, original_snapshot FROM changes WHERE id=?",
(change_id,))
row = cursor.fetchone()
if not row:
return False
file_path, c_type, s_c, r_c, snapshot = row
abs_path = os.path.normpath(os.path.join(root_dir, file_path))
if not os.path.exists(abs_path):
return False
try:
with open(abs_path, 'r', encoding='utf-8') as f:
current_content = f.read()
if c_type == 'create':
return current_content != r_c
elif c_type == 'edit':
if snapshot is None:
return False
# Ожидаемое содержимое файла после успешной заправки ИИ
if s_c and ('<all />' in s_c or '<all/>' in s_c):
expected = r_c
else:
expected = snapshot.replace(s_c, r_c)
return current_content != expected
except Exception:
return False
return False
def rollback_change(self, change_id, root_dir):
# Получаем данные изменения из БД напрямую
cursor = self.db.conn.execute(
"SELECT file_path, change_type, original_snapshot, status FROM changes WHERE id=?", (change_id,))
row = cursor.fetchone()
if not row: return False, "Запись не найдена"
file_path, c_type, snapshot, status = row
abs_root = os.path.abspath(root_dir)
abs_path = os.path.normpath(os.path.join(abs_root, file_path))
# Защита от выхода за границы директории
if not abs_path.startswith(abs_root + os.sep) and abs_path != abs_root:
return False, "Запрещено: попытка выхода за пределы корневой папки проекта"
try:
if c_type == 'create':
if os.path.exists(abs_path):
os.remove(abs_path)
elif c_type == 'edit':
if snapshot is not None:
with open(abs_path, 'w', encoding='utf-8') as f:
f.write(snapshot)
self.db.update_change_status(change_id, 'RolledBack')
return True, "Откачено успешно"
except Exception as e:
return False, str(e)
def reapply_change(self, change_id, root_dir):
cursor = self.db.conn.execute(
"SELECT file_path, change_type, search_content, replace_content FROM changes WHERE id=?", (change_id,))
row = cursor.fetchone()
if not row: return False, "Запись не найдена"
file_path, c_type, s_c, r_c = row
abs_root = os.path.abspath(root_dir)
abs_path = os.path.normpath(os.path.join(abs_root, file_path))
# Защита от выхода за границы директории
if not abs_path.startswith(abs_root + os.sep) and abs_path != abs_root:
return False, "Запрещено: попытка выхода за пределы корневой папки проекта"
try:
if c_type == 'create':
os.makedirs(os.path.dirname(abs_path), exist_ok=True)
with open(abs_path, 'w', encoding='utf-8') as f:
f.write(r_c)
elif c_type == 'edit':
with open(abs_path, 'r', encoding='utf-8') as f:
content = f.read()
if s_c and ('<all />' in s_c or '<all/>' in s_c):
new_content = r_c
else:
new_content = content.replace(s_c, r_c)
with open(abs_path, 'w', encoding='utf-8') as f:
f.write(new_content)
self.db.update_change_status(change_id, 'Success')
return True, "Применено повторно"
except Exception as e:
err_msg = str(e)
self.db.update_change_status(change_id, 'Error', err_msg)
return False, err_msg
def generate_dump(self, project_id, root_dir):
files = self.db.get_project_files(project_id)
dump_path = os.path.join(root_dir, "context_dump.txt")
try:
with open(dump_path, 'w', encoding='utf-8') as out:
for rel_path, is_ro in files:
abs_path = os.path.normpath(os.path.join(root_dir, rel_path))
# 1. Пропускаем пути, которые являются папками, а не файлами
if os.path.isdir(abs_path):
continue
out.write(f"{rel_path}\n")
if is_ro == 1:
out.write("[READ_ONLY]\n")
if os.path.exists(abs_path):
try:
# 2. Быстрая проверка на бинарный файл (картинка, exe, pyc и т.д.)
with open(abs_path, 'rb') as f_bin:
chunk = f_bin.read(1024)
if b'\x00' in chunk: # Нулевой байт — верный признак бинарника
out.write("# [БИНАРНЫЙ ФАЙЛ ПРОПУЩЕН]\n")
out.write("\n----------\n\n")
continue
# 3. Безопасное чтение текста с защитой от сбоев кодировки
with open(abs_path, 'r', encoding='utf-8', errors='replace') as f:
out.write(f.read())
except Exception as read_err:
out.write(f"# ОШИБКА ЧТЕНИЯ ФАЙЛА: {str(read_err)}\n")
else:
out.write("# ФАЙЛ НЕ НАЙДЕН НА ДИСКЕ\n")
out.write("\n----------\n\n")
return True, dump_path
except Exception as e:
return False, str(e)