sudo cat\tee

This commit is contained in:
CakeCN
2025-09-14 17:13:00 +08:00
parent 18c28edd4f
commit 49badd72db
3 changed files with 38 additions and 4 deletions

View File

@@ -261,7 +261,6 @@ class AgentManager:
agent.change_language(language) agent.change_language(language)
def sample_judge(self, id, bb): def sample_judge(self, id, bb):
agent = self.agents[id]
with self.app.app_context(): with self.app.app_context():
try: try:

View File

@@ -2,6 +2,7 @@
import os import os
from flask import current_app from flask import current_app
from backboardManager import Backboard # 你已有的类 from backboardManager import Backboard # 你已有的类
from ..utils.sudoUtil import sudo_open
def _to_wsl_path_if_needed(path: str) -> str: def _to_wsl_path_if_needed(path: str) -> str:
cfg = current_app.config["VSCODE_WEB_PATH"] cfg = current_app.config["VSCODE_WEB_PATH"]
@@ -32,7 +33,7 @@ def realtime_response(config: dict, realtime_action: dict) -> None:
elif rtype == "activeFile": elif rtype == "activeFile":
file_path = realtime_action.get("filePath") file_path = realtime_action.get("filePath")
assert isinstance(file_path, str) assert isinstance(file_path, str)
with open(file_path, "r", encoding="utf-8") as f: with sudo_open(file_path, "r", encoding="utf-8") as f:
bb.active_file_content = f.read() bb.active_file_content = f.read()
bb.active_file_path = file_path bb.active_file_path = file_path
@@ -42,14 +43,14 @@ def realtime_response(config: dict, realtime_action: dict) -> None:
bb.pasted_file_path = file_path bb.pasted_file_path = file_path
bb.pasted_content = realtime_action.get("content") bb.pasted_content = realtime_action.get("content")
bb.active_file_path = file_path bb.active_file_path = file_path
with open(file_path, "r", encoding="utf-8") as f: with sudo_open(file_path, "r", encoding="utf-8") as f:
bb.active_file_content = f.read() bb.active_file_content = f.read()
elif rtype == "fileEdit": elif rtype == "fileEdit":
file_path = realtime_action.get("filePath") file_path = realtime_action.get("filePath")
assert isinstance(file_path, str) assert isinstance(file_path, str)
bb.active_file_path = file_path bb.active_file_path = file_path
with open(file_path, "r", encoding="utf-8") as f: with sudo_open(file_path, "r", encoding="utf-8") as f:
bb.active_file_content = f.read() bb.active_file_content = f.read()

View File

@@ -0,0 +1,34 @@
from contextlib import contextmanager
import subprocess
@contextmanager
def sudo_open(file_path, mode='r', encoding='utf-8'):
"""
上下文管理器,以 sudo 权限打开文件,支持读取和写入模式。
:param file_path: 文件路径
:param mode: 打开文件的模式 ('r''w')
:param encoding: 文件编码
"""
try:
# 检查打开模式,读取或写入
if mode == 'r':
# 使用 sudo 以读取文件
result = subprocess.run(["sudo", "cat", file_path], capture_output=True, text=True, encoding=encoding)
if result.returncode != 0:
raise PermissionError(f"无法读取文件: {file_path}")
content = result.stdout
yield content
elif mode == 'w':
# 使用 sudo 打开文件并写入内容
def write_content(content):
result = subprocess.run(["sudo", "tee", file_path], input=content, text=True, capture_output=True, encoding=encoding)
if result.returncode != 0:
raise PermissionError(f"无法写入文件: {file_path}")
return result.stdout
yield write_content
else:
raise ValueError("只支持 'r''w' 模式")
except Exception as e:
raise e
finally:
pass