✨ feat: 完成env重新设计
This commit is contained in:
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()
|
||||
Reference in New Issue
Block a user