51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
"""working_dir 在 DB 与文件系统两种形态之间的归一(原 `task_dir` 已改名)。
|
|
|
|
存储约定(DESIGN §7.4):
|
|
- working_dir 在 ROOT 内 → 相对 ROOT 的 posix 串(如 `workspace/users/<uid>/<name>`)
|
|
- working_dir 在 ROOT 外 → 绝对 str(如 `D:\\projects\\other\\proj` 或 `/home/u/proj`)
|
|
- 空串 → 空串(legacy / 未绑项目)
|
|
|
|
跨机器迁移 / 切 OS / 移 repo 后,ROOT-内路径仍能 resolve;ROOT-外仍存绝对是务实选择
|
|
—— 用户自指定的项目目录没有更好的归一基。
|
|
|
|
Read 端两种来源走两个入口:
|
|
- DB tasks.working_dir → `from_db_path(s)` → absolute Path
|
|
- 用户 CLI `--working-dir` / Web `/v1/tasks` 表单 → `Path(arg).expanduser().resolve()`
|
|
|
|
Write 端只通过 `to_db_path(absolute Path)` → DB 串。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from typing import Union
|
|
|
|
ROOT: Path = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
def to_db_path(p: Union[Path, str, None]) -> str:
|
|
"""absolute Path / str → DB 串。
|
|
|
|
输入应已是绝对路径(build_agent / web 路由那一层都 .resolve() 过)。
|
|
ROOT 内 → 相对 posix(`workspace/users/<uid>/<name>`)
|
|
ROOT 外 → str(Path)(保留 OS 原生分隔符)
|
|
空 → ""
|
|
"""
|
|
if not p:
|
|
return ""
|
|
pp = Path(p).resolve()
|
|
try:
|
|
return pp.relative_to(ROOT).as_posix()
|
|
except ValueError:
|
|
return str(pp)
|
|
|
|
|
|
def from_db_path(s: str) -> Path:
|
|
"""DB 串 → absolute Path。
|
|
|
|
相对串 → ROOT / s(再 resolve);绝对串 → resolve();空 → Path("")(调用方判)。
|
|
"""
|
|
if not s or not s.strip():
|
|
return Path("")
|
|
p = Path(s)
|
|
return p.resolve() if p.is_absolute() else (ROOT / p).resolve()
|