"""采购工具的本地运行目录策略。""" from __future__ import annotations from dataclasses import dataclass import os from pathlib import Path @dataclass(frozen=True) class RuntimePaths: """仅包含应用生成物的本地目录。 日志和证据产物不落在源码树中,避免把含有现场信息的运行数据误提交到 Git。 """ root: Path logs: Path artifacts: Path @classmethod def from_root(cls, root: Path) -> "RuntimePaths": resolved_root = root.expanduser() return cls( root=resolved_root, logs=resolved_root / "logs", artifacts=resolved_root / "artifacts", ) @classmethod def default(cls) -> "RuntimePaths": local_app_data = os.environ.get("LOCALAPPDATA") if local_app_data: return cls.from_root(Path(local_app_data) / "cmbuyer") return cls.from_root(Path.home() / ".local" / "share" / "cmbuyer") def ensure_exists(self) -> None: """创建运行目录;调用方负责向用户呈现无法创建目录的错误。""" self.logs.mkdir(parents=True, exist_ok=True) self.artifacts.mkdir(parents=True, exist_ok=True)