from __future__ import annotations import sys from pathlib import Path def find_project_root(start_path: Path | None = None) -> Path: """Find the emotion_fusion_demo project root from a file or directory. A valid root contains app.py, config.py, and at least two of the core module directories. If detection fails, return the current working directory with a clear warning instead of raising. """ start = Path(start_path) if start_path is not None else Path.cwd() try: start = start.resolve() except Exception: start = Path.cwd().resolve() if start.is_file(): start = start.parent candidates = [start, *start.parents] for candidate in candidates: if _looks_like_project_root(candidate): return candidate print( "Warning: 未能自动识别项目根目录,请确认你在 emotion_fusion_demo 项目目录中运行。", file=sys.stderr, ) return Path.cwd().resolve() def _looks_like_project_root(path: Path) -> bool: if not (path / "app.py").is_file(): return False if not (path / "config.py").is_file(): return False core_dirs = ["face_module", "ecg_module", "fusion"] existing_count = sum(1 for dirname in core_dirs if (path / dirname).is_dir()) return existing_count >= 2