From 49badd72dbe634f17875bd075bb5cc3532e8ca2d Mon Sep 17 00:00:00 2001 From: CakeCN Date: Sun, 14 Sep 2025 17:13:00 +0800 Subject: [PATCH] sudo cat\tee --- .../projects/algoriAgent/agent_manager.py | 1 - Html/apps/services/backboard_service.py | 7 ++-- Html/apps/utils/sudoUtil.py | 34 +++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 Html/apps/utils/sudoUtil.py diff --git a/AlgoriAgent/projects/algoriAgent/agent_manager.py b/AlgoriAgent/projects/algoriAgent/agent_manager.py index 6a5a665..d40ab33 100644 --- a/AlgoriAgent/projects/algoriAgent/agent_manager.py +++ b/AlgoriAgent/projects/algoriAgent/agent_manager.py @@ -261,7 +261,6 @@ class AgentManager: agent.change_language(language) def sample_judge(self, id, bb): - agent = self.agents[id] with self.app.app_context(): try: diff --git a/Html/apps/services/backboard_service.py b/Html/apps/services/backboard_service.py index ca36924..6a458da 100644 --- a/Html/apps/services/backboard_service.py +++ b/Html/apps/services/backboard_service.py @@ -2,6 +2,7 @@ import os from flask import current_app from backboardManager import Backboard # 你已有的类 +from ..utils.sudoUtil import sudo_open def _to_wsl_path_if_needed(path: str) -> str: cfg = current_app.config["VSCODE_WEB_PATH"] @@ -32,7 +33,7 @@ def realtime_response(config: dict, realtime_action: dict) -> None: elif rtype == "activeFile": file_path = realtime_action.get("filePath") 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_path = file_path @@ -42,14 +43,14 @@ def realtime_response(config: dict, realtime_action: dict) -> None: bb.pasted_file_path = file_path bb.pasted_content = realtime_action.get("content") 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() elif rtype == "fileEdit": file_path = realtime_action.get("filePath") assert isinstance(file_path, str) 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() diff --git a/Html/apps/utils/sudoUtil.py b/Html/apps/utils/sudoUtil.py new file mode 100644 index 0000000..f32e043 --- /dev/null +++ b/Html/apps/utils/sudoUtil.py @@ -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 \ No newline at end of file