✨ feat: 完成env重新设计
This commit is contained in:
1
aide-program/aide/aide.sh
Symbolic link
1
aide-program/aide/aide.sh
Symbolic link
@@ -0,0 +1 @@
|
||||
./aide.sh
|
||||
@@ -13,8 +13,9 @@ from aide.core import output
|
||||
DEFAULT_CONFIG = """# Aide 默认配置(由 aide init 生成)
|
||||
# runtime: aide 自身运行要求
|
||||
# task: 任务文档路径
|
||||
# env: 虚拟环境与依赖配置
|
||||
# env: 环境模块配置
|
||||
# flow: 环节名称列表,供流程校验使用
|
||||
|
||||
[runtime]
|
||||
python_min = "3.11"
|
||||
use_uv = true
|
||||
@@ -24,8 +25,20 @@ source = "task-now.md"
|
||||
spec = "task-spec.md"
|
||||
|
||||
[env]
|
||||
venv = ".venv"
|
||||
requirements = "requirements.txt"
|
||||
# 启用的模块列表
|
||||
modules = ["python", "uv", "venv", "requirements"]
|
||||
|
||||
# Python 版本要求(可选,默认使用 runtime.python_min)
|
||||
# [env.python]
|
||||
# min_version = "3.11"
|
||||
|
||||
# 虚拟环境配置(类型B模块,必须配置)
|
||||
[env.venv]
|
||||
path = ".venv"
|
||||
|
||||
# 依赖文件配置(类型B模块,必须配置)
|
||||
[env.requirements]
|
||||
path = "requirements.txt"
|
||||
|
||||
[flow]
|
||||
phases = ["task-optimize", "flow-design", "impl", "verify", "docs", "finish"]
|
||||
|
||||
119
aide-program/aide/env/ensure.py
vendored
119
aide-program/aide/env/ensure.py
vendored
@@ -1,119 +0,0 @@
|
||||
"""环境检测与修复逻辑。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import platform
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from aide.core import output
|
||||
from aide.core.config import ConfigManager
|
||||
|
||||
|
||||
class EnvManager:
|
||||
def __init__(self, root: Path):
|
||||
self.root = root
|
||||
|
||||
def ensure(self, runtime_only: bool, cfg: ConfigManager) -> bool:
|
||||
"""运行环境检测入口。"""
|
||||
required_py = self._get_required_python(cfg, runtime_only)
|
||||
if not self._check_python_version(required_py):
|
||||
return False
|
||||
uv_version = self._check_uv()
|
||||
if uv_version is None:
|
||||
return False
|
||||
|
||||
if runtime_only:
|
||||
output.ok(f"运行时环境就绪 (python:{platform.python_version()}, uv:{uv_version})")
|
||||
return True
|
||||
|
||||
config = cfg.ensure_config()
|
||||
cfg.ensure_gitignore()
|
||||
|
||||
env_config = config.get("env", {})
|
||||
venv_path = self.root / env_config.get("venv", ".venv")
|
||||
req_path = self.root / env_config.get("requirements", "requirements.txt")
|
||||
|
||||
self._ensure_requirements_file(req_path)
|
||||
if not self._ensure_venv(venv_path):
|
||||
return False
|
||||
if not self._install_requirements(venv_path, req_path):
|
||||
return False
|
||||
|
||||
task_config = config.get("task", {})
|
||||
output.info(f"任务原文档: {task_config.get('source', 'task-now.md')}")
|
||||
output.info(f"任务细则文档: {task_config.get('spec', 'task-spec.md')}")
|
||||
output.ok(f"环境就绪 (python:{platform.python_version()}, uv:{uv_version}, venv:{venv_path})")
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def _get_required_python(cfg: ConfigManager, runtime_only: bool) -> str:
|
||||
if runtime_only:
|
||||
return "3.11"
|
||||
data = cfg.load_config()
|
||||
runtime = data.get("runtime", {})
|
||||
return str(runtime.get("python_min", "3.11"))
|
||||
|
||||
@staticmethod
|
||||
def _parse_version(version: str) -> tuple[int, ...]:
|
||||
parts = []
|
||||
for part in version.split("."):
|
||||
try:
|
||||
parts.append(int(part))
|
||||
except ValueError:
|
||||
break
|
||||
return tuple(parts)
|
||||
|
||||
def _check_python_version(self, required: str) -> bool:
|
||||
current = self._parse_version(platform.python_version())
|
||||
target = self._parse_version(required)
|
||||
if current >= target:
|
||||
return True
|
||||
output.err(f"Python 版本不足,要求>={required},当前 {platform.python_version()}")
|
||||
return False
|
||||
|
||||
def _check_uv(self) -> str | None:
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["uv", "--version"],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
return result.stdout.strip()
|
||||
except (subprocess.CalledProcessError, FileNotFoundError) as exc:
|
||||
output.err(f"未检测到 uv,请先安装({exc})")
|
||||
return None
|
||||
|
||||
def _ensure_venv(self, venv_path: Path) -> bool:
|
||||
if venv_path.exists():
|
||||
return True
|
||||
output.info(f"创建虚拟环境: {venv_path}")
|
||||
try:
|
||||
subprocess.run(["uv", "venv", str(venv_path)], check=True)
|
||||
output.ok("已创建虚拟环境")
|
||||
return True
|
||||
except subprocess.CalledProcessError as exc:
|
||||
output.err(f"创建虚拟环境失败: {exc}")
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _ensure_requirements_file(req_path: Path) -> None:
|
||||
if req_path.exists():
|
||||
return
|
||||
req_path.write_text("# 在此添加依赖\n", encoding="utf-8")
|
||||
output.warn(f"未找到 {req_path.name},已创建空文件")
|
||||
|
||||
def _install_requirements(self, venv_path: Path, req_path: Path) -> bool:
|
||||
if not req_path.exists():
|
||||
output.err(f"缺少 {req_path}")
|
||||
return False
|
||||
cmd = ["uv", "pip", "install", "-r", str(req_path), "--python", str(venv_path)]
|
||||
output.info("安装依赖(uv pip install -r requirements.txt)")
|
||||
try:
|
||||
subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
|
||||
return True
|
||||
except subprocess.CalledProcessError as exc:
|
||||
output.err(f"安装依赖失败: {exc}")
|
||||
return False
|
||||
274
aide-program/aide/env/manager.py
vendored
Normal file
274
aide-program/aide/env/manager.py
vendored
Normal file
@@ -0,0 +1,274 @@
|
||||
"""环境管理器。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from aide.core import output
|
||||
from aide.core.config import ConfigManager
|
||||
from aide.env.registry import ModuleRegistry, register_builtin_modules
|
||||
|
||||
# 运行时模块(--runtime 时使用)
|
||||
RUNTIME_MODULES = ["python", "uv"]
|
||||
|
||||
# 默认启用的模块
|
||||
DEFAULT_MODULES = ["python", "uv", "venv", "requirements"]
|
||||
|
||||
|
||||
class EnvManager:
|
||||
"""环境管理器。"""
|
||||
|
||||
def __init__(self, root: Path, cfg: ConfigManager):
|
||||
self.root = root
|
||||
self.cfg = cfg
|
||||
self.verbose = False
|
||||
# 确保模块已注册
|
||||
register_builtin_modules()
|
||||
|
||||
def list_modules(self) -> None:
|
||||
"""列出所有可用模块(aide env list)。"""
|
||||
config = self.cfg.load_config()
|
||||
enabled = self._get_enabled_modules(config)
|
||||
|
||||
print("可用模块:")
|
||||
print(f" {'模块':<14} {'描述':<20} {'能力':<16} {'需要配置'}")
|
||||
print(" " + "─" * 60)
|
||||
|
||||
for info in ModuleRegistry.list_info():
|
||||
caps = ", ".join(info.capabilities)
|
||||
req_cfg = "是" if info.requires_config else "否"
|
||||
if info.config_keys:
|
||||
req_cfg += f" [{', '.join(info.config_keys)}]"
|
||||
print(f" {info.name:<14} {info.description:<20} {caps:<16} {req_cfg}")
|
||||
|
||||
print()
|
||||
if enabled:
|
||||
print(f"当前启用: {', '.join(enabled)}")
|
||||
else:
|
||||
output.warn("未配置启用模块列表")
|
||||
|
||||
def ensure(
|
||||
self,
|
||||
runtime_only: bool = False,
|
||||
modules: list[str] | None = None,
|
||||
check_only: bool = False,
|
||||
verbose: bool = False,
|
||||
) -> bool:
|
||||
"""检测并修复环境。
|
||||
|
||||
Args:
|
||||
runtime_only: 仅检测运行时环境
|
||||
modules: 指定要检测的模块
|
||||
check_only: 仅检测不修复(--all 模式)
|
||||
verbose: 显示详细配置信息
|
||||
|
||||
Returns:
|
||||
是否全部成功
|
||||
"""
|
||||
self.verbose = verbose
|
||||
config = self.cfg.load_config()
|
||||
enabled_modules = self._get_enabled_modules(config)
|
||||
|
||||
# verbose: 输出基础信息
|
||||
if verbose:
|
||||
self._print_verbose_header(config, enabled_modules)
|
||||
|
||||
# 确定要检测的模块列表
|
||||
if runtime_only:
|
||||
target_modules = RUNTIME_MODULES
|
||||
elif modules:
|
||||
target_modules = modules
|
||||
elif check_only:
|
||||
# --all 模式
|
||||
if not enabled_modules:
|
||||
output.warn("未配置启用模块列表,将检测所有支持的模块")
|
||||
target_modules = ModuleRegistry.names()
|
||||
else:
|
||||
target_modules = enabled_modules
|
||||
else:
|
||||
target_modules = enabled_modules
|
||||
|
||||
if not target_modules:
|
||||
output.warn("没有要检测的模块")
|
||||
return True
|
||||
|
||||
if verbose:
|
||||
print(f" 目标模块: {', '.join(target_modules)}")
|
||||
print()
|
||||
|
||||
# 执行检测
|
||||
all_success = True
|
||||
results: list[tuple[str, bool, str]] = []
|
||||
|
||||
for name in target_modules:
|
||||
is_enabled = name in enabled_modules
|
||||
success, msg = self._process_module(
|
||||
name=name,
|
||||
config=config,
|
||||
is_enabled=is_enabled,
|
||||
check_only=check_only,
|
||||
)
|
||||
results.append((name, success, msg))
|
||||
if not success and is_enabled:
|
||||
all_success = False
|
||||
break # 启用模块失败时停止
|
||||
|
||||
# 输出最终状态
|
||||
if all_success and not check_only:
|
||||
# 构建摘要信息
|
||||
summary_parts = []
|
||||
for name, success, msg in results:
|
||||
if success and msg:
|
||||
summary_parts.append(f"{name}:{msg}")
|
||||
if summary_parts:
|
||||
output.ok(f"环境就绪 ({', '.join(summary_parts)})")
|
||||
|
||||
return all_success
|
||||
|
||||
def _print_verbose_header(self, config: dict[str, Any], enabled_modules: list[str]) -> None:
|
||||
"""输出详细模式的头部信息。"""
|
||||
print("=" * 60)
|
||||
print("环境检测详细信息")
|
||||
print("=" * 60)
|
||||
print()
|
||||
print(f" 工作目录: {self.root}")
|
||||
print(f" 配置文件: {self.cfg.config_path}")
|
||||
print(f" 配置存在: {'是' if self.cfg.config_path.exists() else '否'}")
|
||||
print()
|
||||
print(f" 启用模块: {', '.join(enabled_modules) if enabled_modules else '(未配置)'}")
|
||||
print()
|
||||
|
||||
def _print_verbose_module(self, name: str, module_config: dict[str, Any]) -> None:
|
||||
"""输出模块的详细配置信息。"""
|
||||
print(f" [{name}] 配置:")
|
||||
if not module_config:
|
||||
print(" (无配置)")
|
||||
else:
|
||||
for key, value in module_config.items():
|
||||
if key.startswith("_"):
|
||||
continue # 跳过内部字段
|
||||
if key == "path":
|
||||
# 对于路径,显示绝对路径
|
||||
abs_path = self.root / value
|
||||
print(f" {key}: {value}")
|
||||
print(f" {key} (绝对): {abs_path}")
|
||||
print(f" {key} (存在): {'是' if abs_path.exists() else '否'}")
|
||||
else:
|
||||
print(f" {key}: {value}")
|
||||
|
||||
def _get_enabled_modules(self, config: dict[str, Any]) -> list[str]:
|
||||
"""获取已启用的模块列表。"""
|
||||
env_config = config.get("env", {})
|
||||
return env_config.get("modules", DEFAULT_MODULES)
|
||||
|
||||
def _get_module_config(self, name: str, config: dict[str, Any]) -> dict[str, Any]:
|
||||
"""获取模块配置。"""
|
||||
env_config = config.get("env", {})
|
||||
|
||||
# 尝试新格式 [env.模块名]
|
||||
module_config = env_config.get(name, {})
|
||||
|
||||
# 兼容旧格式:如果值是字符串而不是字典,转换为 {"path": value}
|
||||
if isinstance(module_config, str):
|
||||
module_config = {"path": module_config}
|
||||
|
||||
# 兼容旧格式:如果没有配置但存在旧格式字段
|
||||
if name == "venv" and not module_config:
|
||||
if "venv" in env_config and isinstance(env_config["venv"], str):
|
||||
module_config = {"path": env_config["venv"]}
|
||||
elif name == "requirements" and not module_config:
|
||||
if "requirements" in env_config and isinstance(env_config["requirements"], str):
|
||||
module_config = {"path": env_config["requirements"]}
|
||||
|
||||
# 为 requirements 模块注入 venv 路径
|
||||
if name == "requirements":
|
||||
venv_config = self._get_module_config("venv", config)
|
||||
if "path" in venv_config:
|
||||
module_config["_venv_path"] = venv_config["path"]
|
||||
|
||||
# 从 runtime 配置获取 python 版本要求
|
||||
if name == "python" and "min_version" not in module_config:
|
||||
runtime = config.get("runtime", {})
|
||||
if "python_min" in runtime:
|
||||
module_config["min_version"] = runtime["python_min"]
|
||||
|
||||
return module_config
|
||||
|
||||
def _process_module(
|
||||
self,
|
||||
name: str,
|
||||
config: dict[str, Any],
|
||||
is_enabled: bool,
|
||||
check_only: bool,
|
||||
) -> tuple[bool, str]:
|
||||
"""处理单个模块的检测/修复。
|
||||
|
||||
Returns:
|
||||
(是否成功, 版本/路径信息)
|
||||
"""
|
||||
module = ModuleRegistry.get(name)
|
||||
if not module:
|
||||
if is_enabled:
|
||||
output.err(f"{name}: 未知模块")
|
||||
return False, ""
|
||||
else:
|
||||
output.warn(f"{name}: 未知模块")
|
||||
return True, ""
|
||||
|
||||
module_config = self._get_module_config(name, config)
|
||||
|
||||
# verbose: 输出模块配置
|
||||
if self.verbose:
|
||||
self._print_verbose_module(name, module_config)
|
||||
|
||||
# 检查类型B模块的配置
|
||||
valid, err_msg = module.validate_config(module_config)
|
||||
if not valid:
|
||||
if is_enabled:
|
||||
output.err(f"{name}: 已启用但{err_msg}")
|
||||
return False, ""
|
||||
else:
|
||||
output.warn(f"{name}: {err_msg},跳过检测")
|
||||
return True, ""
|
||||
|
||||
# 执行检测
|
||||
result = module.check(module_config, self.root)
|
||||
|
||||
if result.success:
|
||||
version_info = result.version or ""
|
||||
extra = f" ({result.message})" if result.message else ""
|
||||
output.ok(f"{name}: {version_info}{extra}")
|
||||
return True, version_info
|
||||
|
||||
# 检测失败
|
||||
if check_only:
|
||||
# --all 模式:仅报告
|
||||
output.warn(f"{name}: {result.message}")
|
||||
return True, ""
|
||||
|
||||
if result.can_ensure and module.info.can_ensure:
|
||||
# 尝试修复
|
||||
output.info(f"{name}: {result.message},尝试修复...")
|
||||
ensure_result = module.ensure(module_config, self.root)
|
||||
|
||||
if ensure_result.success:
|
||||
msg = ensure_result.message or "已修复"
|
||||
output.ok(f"{name}: {msg}")
|
||||
return True, ensure_result.version or ""
|
||||
else:
|
||||
if is_enabled:
|
||||
output.err(f"{name}: {ensure_result.message}")
|
||||
return False, ""
|
||||
else:
|
||||
output.warn(f"{name}: {ensure_result.message}")
|
||||
return True, ""
|
||||
else:
|
||||
# 不可修复
|
||||
if is_enabled:
|
||||
extra = " (此模块不支持自动修复)" if not module.info.can_ensure else ""
|
||||
output.err(f"{name}: {result.message}{extra}")
|
||||
return False, ""
|
||||
else:
|
||||
output.warn(f"{name}: {result.message}")
|
||||
return True, ""
|
||||
1
aide-program/aide/env/modules/__init__.py
vendored
Normal file
1
aide-program/aide/env/modules/__init__.py
vendored
Normal file
@@ -0,0 +1 @@
|
||||
"""环境检测模块集合。"""
|
||||
89
aide-program/aide/env/modules/base.py
vendored
Normal file
89
aide-program/aide/env/modules/base.py
vendored
Normal file
@@ -0,0 +1,89 @@
|
||||
"""模块基类定义。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass
|
||||
class CheckResult:
|
||||
"""检测结果。"""
|
||||
|
||||
success: bool
|
||||
version: str | None = None
|
||||
message: str | None = None
|
||||
can_ensure: bool = False # 失败时是否可修复
|
||||
|
||||
|
||||
@dataclass
|
||||
class ModuleInfo:
|
||||
"""模块元信息。"""
|
||||
|
||||
name: str
|
||||
description: str
|
||||
capabilities: list[str] = field(default_factory=lambda: ["check"])
|
||||
requires_config: bool = False # 是否需要配置(类型B模块)
|
||||
config_keys: list[str] = field(default_factory=list) # 需要的配置键
|
||||
|
||||
@property
|
||||
def can_ensure(self) -> bool:
|
||||
"""是否支持 ensure 操作。"""
|
||||
return "ensure" in self.capabilities
|
||||
|
||||
|
||||
class BaseModule(ABC):
|
||||
"""模块基类。"""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def info(self) -> ModuleInfo:
|
||||
"""返回模块元信息。"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def check(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""检测环境。
|
||||
|
||||
Args:
|
||||
config: 模块配置(来自 [env.模块名])
|
||||
root: 项目根目录
|
||||
|
||||
Returns:
|
||||
CheckResult: 检测结果
|
||||
"""
|
||||
pass
|
||||
|
||||
def ensure(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""修复环境(可选实现)。
|
||||
|
||||
Args:
|
||||
config: 模块配置
|
||||
root: 项目根目录
|
||||
|
||||
Returns:
|
||||
CheckResult: 修复结果
|
||||
"""
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message="此模块不支持自动修复",
|
||||
)
|
||||
|
||||
def validate_config(self, config: dict[str, Any]) -> tuple[bool, str | None]:
|
||||
"""验证模块配置是否完整。
|
||||
|
||||
Args:
|
||||
config: 模块配置
|
||||
|
||||
Returns:
|
||||
(是否有效, 错误信息)
|
||||
"""
|
||||
if not self.info.requires_config:
|
||||
return True, None
|
||||
|
||||
missing = [k for k in self.info.config_keys if k not in config]
|
||||
if missing:
|
||||
return False, f"缺少配置项: {', '.join(missing)}"
|
||||
return True, None
|
||||
58
aide-program/aide/env/modules/python.py
vendored
Normal file
58
aide-program/aide/env/modules/python.py
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
"""Python 环境检测模块。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import platform
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from aide.env.modules.base import BaseModule, CheckResult, ModuleInfo
|
||||
|
||||
|
||||
class PythonModule(BaseModule):
|
||||
"""Python 解释器检测模块(类型A:无需配置)。"""
|
||||
|
||||
@property
|
||||
def info(self) -> ModuleInfo:
|
||||
return ModuleInfo(
|
||||
name="python",
|
||||
description="Python 解释器版本",
|
||||
capabilities=["check"],
|
||||
requires_config=False,
|
||||
)
|
||||
|
||||
def check(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""检测 Python 版本。"""
|
||||
current_version = platform.python_version()
|
||||
min_version = config.get("min_version", "3.11")
|
||||
|
||||
current_parts = self._parse_version(current_version)
|
||||
min_parts = self._parse_version(min_version)
|
||||
|
||||
if current_parts >= min_parts:
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=current_version,
|
||||
message=f">={min_version}",
|
||||
)
|
||||
else:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
version=current_version,
|
||||
message=f"版本不足,要求>={min_version},当前 {current_version}",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _parse_version(version: str) -> tuple[int, ...]:
|
||||
"""解析版本号字符串。"""
|
||||
parts = []
|
||||
for part in version.split("."):
|
||||
try:
|
||||
parts.append(int(part))
|
||||
except ValueError:
|
||||
break
|
||||
return tuple(parts)
|
||||
|
||||
|
||||
module = PythonModule()
|
||||
88
aide-program/aide/env/modules/requirements.py
vendored
Normal file
88
aide-program/aide/env/modules/requirements.py
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
"""Python 依赖管理模块。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from aide.env.modules.base import BaseModule, CheckResult, ModuleInfo
|
||||
|
||||
|
||||
class RequirementsModule(BaseModule):
|
||||
"""Python 依赖管理模块(类型B:需要配置)。"""
|
||||
|
||||
@property
|
||||
def info(self) -> ModuleInfo:
|
||||
return ModuleInfo(
|
||||
name="requirements",
|
||||
description="Python 依赖管理",
|
||||
capabilities=["check", "ensure"],
|
||||
requires_config=True,
|
||||
config_keys=["path"],
|
||||
)
|
||||
|
||||
def check(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""检测 requirements.txt 是否存在。"""
|
||||
req_path = root / config["path"]
|
||||
|
||||
if not req_path.exists():
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"文件不存在: {config['path']}",
|
||||
can_ensure=True,
|
||||
)
|
||||
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=config["path"],
|
||||
)
|
||||
|
||||
def ensure(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""创建空的 requirements.txt 并安装依赖。"""
|
||||
req_path = root / config["path"]
|
||||
|
||||
# 如果文件不存在,创建空文件
|
||||
if not req_path.exists():
|
||||
req_path.write_text("# 在此添加依赖\n", encoding="utf-8")
|
||||
|
||||
# 获取 venv 路径(从同级配置中获取)
|
||||
venv_config = config.get("_venv_path")
|
||||
if not venv_config:
|
||||
# 尝试使用默认路径
|
||||
venv_path = root / ".venv"
|
||||
else:
|
||||
venv_path = root / venv_config
|
||||
|
||||
if not venv_path.exists():
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message="虚拟环境不存在,请先创建",
|
||||
)
|
||||
|
||||
# 安装依赖
|
||||
try:
|
||||
subprocess.run(
|
||||
["uv", "pip", "install", "-r", str(req_path), "--python", str(venv_path)],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=config["path"],
|
||||
message="已安装",
|
||||
)
|
||||
except FileNotFoundError:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message="安装失败: uv 未安装",
|
||||
)
|
||||
except subprocess.CalledProcessError as exc:
|
||||
stderr = exc.stderr.decode() if exc.stderr else str(exc)
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"安装失败: {stderr}",
|
||||
)
|
||||
|
||||
|
||||
module = RequirementsModule()
|
||||
52
aide-program/aide/env/modules/uv.py
vendored
Normal file
52
aide-program/aide/env/modules/uv.py
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
"""uv 包管理器检测模块。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from aide.env.modules.base import BaseModule, CheckResult, ModuleInfo
|
||||
|
||||
|
||||
class UvModule(BaseModule):
|
||||
"""uv 包管理器检测模块(类型A:无需配置)。"""
|
||||
|
||||
@property
|
||||
def info(self) -> ModuleInfo:
|
||||
return ModuleInfo(
|
||||
name="uv",
|
||||
description="uv 包管理器",
|
||||
capabilities=["check"],
|
||||
requires_config=False,
|
||||
)
|
||||
|
||||
def check(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""检测 uv 是否可用。"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["uv", "--version"],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
version = result.stdout.strip()
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=version,
|
||||
)
|
||||
except FileNotFoundError:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message="未安装,请先安装 uv",
|
||||
can_ensure=False,
|
||||
)
|
||||
except subprocess.CalledProcessError as exc:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"执行失败: {exc}",
|
||||
can_ensure=False,
|
||||
)
|
||||
|
||||
|
||||
module = UvModule()
|
||||
80
aide-program/aide/env/modules/venv.py
vendored
Normal file
80
aide-program/aide/env/modules/venv.py
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
"""Python 虚拟环境模块。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from aide.env.modules.base import BaseModule, CheckResult, ModuleInfo
|
||||
|
||||
|
||||
class VenvModule(BaseModule):
|
||||
"""Python 虚拟环境模块(类型B:需要配置)。"""
|
||||
|
||||
@property
|
||||
def info(self) -> ModuleInfo:
|
||||
return ModuleInfo(
|
||||
name="venv",
|
||||
description="Python 虚拟环境",
|
||||
capabilities=["check", "ensure"],
|
||||
requires_config=True,
|
||||
config_keys=["path"],
|
||||
)
|
||||
|
||||
def check(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""检测虚拟环境是否存在。"""
|
||||
venv_path = root / config["path"]
|
||||
|
||||
if not venv_path.exists():
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"虚拟环境不存在: {config['path']}",
|
||||
can_ensure=True,
|
||||
)
|
||||
|
||||
# 检查是否是有效的虚拟环境
|
||||
python_path = venv_path / "bin" / "python"
|
||||
if not python_path.exists():
|
||||
python_path = venv_path / "Scripts" / "python.exe" # Windows
|
||||
|
||||
if not python_path.exists():
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"无效的虚拟环境: {config['path']}",
|
||||
can_ensure=True,
|
||||
)
|
||||
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=config["path"],
|
||||
)
|
||||
|
||||
def ensure(self, config: dict[str, Any], root: Path) -> CheckResult:
|
||||
"""创建虚拟环境。"""
|
||||
venv_path = root / config["path"]
|
||||
|
||||
try:
|
||||
subprocess.run(
|
||||
["uv", "venv", str(venv_path)],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
)
|
||||
return CheckResult(
|
||||
success=True,
|
||||
version=config["path"],
|
||||
message="已创建",
|
||||
)
|
||||
except FileNotFoundError:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message="创建失败: uv 未安装",
|
||||
)
|
||||
except subprocess.CalledProcessError as exc:
|
||||
return CheckResult(
|
||||
success=False,
|
||||
message=f"创建失败: {exc.stderr.decode() if exc.stderr else exc}",
|
||||
)
|
||||
|
||||
|
||||
module = VenvModule()
|
||||
50
aide-program/aide/env/registry.py
vendored
Normal file
50
aide-program/aide/env/registry.py
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
"""模块注册表。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from aide.env.modules.base import BaseModule, ModuleInfo
|
||||
|
||||
|
||||
class ModuleRegistry:
|
||||
"""模块注册表,管理所有可用的环境检测模块。"""
|
||||
|
||||
_modules: dict[str, BaseModule] = {}
|
||||
|
||||
@classmethod
|
||||
def register(cls, module: BaseModule) -> None:
|
||||
"""注册模块。"""
|
||||
cls._modules[module.info.name] = module
|
||||
|
||||
@classmethod
|
||||
def get(cls, name: str) -> BaseModule | None:
|
||||
"""获取指定模块。"""
|
||||
return cls._modules.get(name)
|
||||
|
||||
@classmethod
|
||||
def all(cls) -> dict[str, BaseModule]:
|
||||
"""获取所有已注册模块。"""
|
||||
return cls._modules.copy()
|
||||
|
||||
@classmethod
|
||||
def names(cls) -> list[str]:
|
||||
"""获取所有模块名称。"""
|
||||
return list(cls._modules.keys())
|
||||
|
||||
@classmethod
|
||||
def list_info(cls) -> list[ModuleInfo]:
|
||||
"""获取所有模块的元信息。"""
|
||||
return [m.info for m in cls._modules.values()]
|
||||
|
||||
@classmethod
|
||||
def clear(cls) -> None:
|
||||
"""清空注册表(用于测试)。"""
|
||||
cls._modules.clear()
|
||||
|
||||
|
||||
def register_builtin_modules() -> None:
|
||||
"""注册内置模块。"""
|
||||
from aide.env.modules import python, uv, venv, requirements
|
||||
|
||||
for mod in [python, uv, venv, requirements]:
|
||||
if hasattr(mod, "module"):
|
||||
ModuleRegistry.register(mod.module)
|
||||
@@ -9,7 +9,7 @@ from typing import Any
|
||||
|
||||
from aide.core import output
|
||||
from aide.core.config import ConfigManager
|
||||
from aide.env.ensure import EnvManager
|
||||
from aide.env.manager import EnvManager
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
@@ -32,17 +32,50 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(prog="aide", description="Aide 工作流辅助工具")
|
||||
subparsers = parser.add_subparsers(dest="command")
|
||||
|
||||
# aide init
|
||||
init_parser = subparsers.add_parser("init", help="初始化 .aide 目录与默认配置")
|
||||
init_parser.set_defaults(func=handle_init)
|
||||
|
||||
# aide env
|
||||
env_parser = subparsers.add_parser("env", help="环境管理")
|
||||
env_sub = env_parser.add_subparsers(dest="env_command")
|
||||
|
||||
# aide env ensure
|
||||
ensure_parser = env_sub.add_parser("ensure", help="检测并修复运行环境")
|
||||
ensure_parser.add_argument("--runtime", action="store_true", help="仅检查 aide 运行时环境")
|
||||
ensure_parser.add_argument(
|
||||
"--runtime",
|
||||
action="store_true",
|
||||
help="仅检查 aide 运行时环境(python + uv)",
|
||||
)
|
||||
ensure_parser.add_argument(
|
||||
"--modules",
|
||||
type=str,
|
||||
help="指定要检测的模块(逗号分隔)",
|
||||
)
|
||||
ensure_parser.add_argument(
|
||||
"--all",
|
||||
action="store_true",
|
||||
dest="check_all",
|
||||
help="检测所有已启用模块(仅检查不修复)",
|
||||
)
|
||||
ensure_parser.add_argument(
|
||||
"-v", "--verbose",
|
||||
action="store_true",
|
||||
help="显示详细配置信息",
|
||||
)
|
||||
ensure_parser.set_defaults(func=handle_env_ensure)
|
||||
|
||||
# aide env list
|
||||
list_parser = env_sub.add_parser("list", help="列出所有可用模块")
|
||||
list_parser.set_defaults(func=handle_env_list)
|
||||
|
||||
# aide env(无子命令时等同于 ensure)
|
||||
env_parser.set_defaults(func=handle_env_default)
|
||||
|
||||
# aide config
|
||||
config_parser = subparsers.add_parser("config", help="配置管理")
|
||||
config_sub = config_parser.add_subparsers(dest="config_command")
|
||||
|
||||
get_parser = config_sub.add_parser("get", help="读取配置值")
|
||||
get_parser.add_argument("key", help="使用点号分隔的键名,如 task.source")
|
||||
get_parser.set_defaults(func=handle_config_get)
|
||||
@@ -65,14 +98,43 @@ def handle_init(args: argparse.Namespace) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def handle_env_default(args: argparse.Namespace) -> bool:
|
||||
"""aide env(无子命令)等同于 aide env ensure。"""
|
||||
if args.env_command is None:
|
||||
# 无子命令,执行默认的 ensure
|
||||
root = Path.cwd()
|
||||
cfg = ConfigManager(root)
|
||||
manager = EnvManager(root, cfg)
|
||||
return manager.ensure()
|
||||
return True
|
||||
|
||||
|
||||
def handle_env_ensure(args: argparse.Namespace) -> bool:
|
||||
if args.env_command != "ensure":
|
||||
output.err("请指定 env 子命令,如: aide env ensure")
|
||||
return False
|
||||
"""aide env ensure 处理。"""
|
||||
root = Path.cwd()
|
||||
cfg = ConfigManager(root)
|
||||
manager = EnvManager(root)
|
||||
return manager.ensure(runtime_only=args.runtime, cfg=cfg)
|
||||
manager = EnvManager(root, cfg)
|
||||
|
||||
# 解析 --modules 参数
|
||||
modules = None
|
||||
if args.modules:
|
||||
modules = [m.strip() for m in args.modules.split(",") if m.strip()]
|
||||
|
||||
return manager.ensure(
|
||||
runtime_only=args.runtime,
|
||||
modules=modules,
|
||||
check_only=args.check_all,
|
||||
verbose=args.verbose,
|
||||
)
|
||||
|
||||
|
||||
def handle_env_list(args: argparse.Namespace) -> bool:
|
||||
"""aide env list 处理。"""
|
||||
root = Path.cwd()
|
||||
cfg = ConfigManager(root)
|
||||
manager = EnvManager(root, cfg)
|
||||
manager.list_modules()
|
||||
return True
|
||||
|
||||
|
||||
def handle_config_get(args: argparse.Namespace) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user