40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""cwd 校验工具 — .kiro/scripts/ 下所有脚本共享。
|
||
|
||
用法:
|
||
from _ensure_root import ensure_repo_root
|
||
ensure_repo_root()
|
||
|
||
委托给 neozqyy_shared.repo_root(共享包),未安装时 fallback。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import warnings
|
||
from pathlib import Path
|
||
|
||
|
||
def ensure_repo_root() -> Path:
|
||
"""校验 cwd 是否为仓库根目录,不是则自动切换。"""
|
||
try:
|
||
from neozqyy_shared.repo_root import ensure_repo_root as _shared
|
||
return _shared()
|
||
except ImportError:
|
||
pass
|
||
# fallback
|
||
cwd = Path.cwd()
|
||
if (cwd / "pyproject.toml").is_file() and (cwd / ".kiro").is_dir():
|
||
return cwd
|
||
root = Path(__file__).resolve().parents[2]
|
||
if (root / "pyproject.toml").is_file() and (root / ".kiro").is_dir():
|
||
os.chdir(root)
|
||
warnings.warn(
|
||
f"cwd 不是仓库根目录,已自动切换: {cwd} → {root}",
|
||
stacklevel=2,
|
||
)
|
||
return root
|
||
raise RuntimeError(
|
||
f"无法定位仓库根目录。当前 cwd={cwd},推断 root={root}。"
|
||
f"请在仓库根目录下运行脚本。"
|
||
)
|