-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths.py
More file actions
111 lines (91 loc) · 3.7 KB
/
Copy pathpaths.py
File metadata and controls
111 lines (91 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
"""BroccoliDB root resolution — single source for DietCode plugin + tools."""
from __future__ import annotations
import os
from pathlib import Path
from typing import Optional
_BROCCOLIDB_DIRNAME = "broccolidb"
def get_plugin_root() -> Path:
"""Canonical DietCode plugin install root (this repository / plugin folder)."""
return Path(__file__).resolve().parent
def is_valid_broccolidb_root(path: Path | str) -> bool:
"""Return True when *path* looks like a BroccoliDB checkout."""
root = Path(path).expanduser()
return (root / "package.json").is_file() and (root / "core").is_dir()
def _plugin_broccolidb_candidates() -> list[Path]:
"""Plugin install fallback — canonical tree is repo-root ``broccolidb/``."""
if os.environ.get("HERMES_BROCCOLIDB_DISABLE_PLUGIN_FALLBACK", "").strip().lower() in {
"1", "true", "yes", "on",
}:
return []
out: list[Path] = []
try:
from hermes_cli.plugins import get_bundled_plugins_dir
out.append(get_bundled_plugins_dir() / "dietcode" / _BROCCOLIDB_DIRNAME)
except Exception:
pass
try:
from hermes_constants import get_hermes_home
out.append(get_hermes_home() / "plugins" / "dietcode" / _BROCCOLIDB_DIRNAME)
except Exception:
pass
return out
def resolve_broccolidb_root() -> Optional[str]:
"""Locate broccolidb/ for the active process (workspace-aware).
Resolution order:
1. ``HERMES_BROCCOLIDB_ROOT`` env (set by kanban dispatcher)
2. Bundled / user DietCode plugin directories
3. ``kanban.broccolidb.root`` in config.yaml
4. Walk parents from ``HERMES_KANBAN_WORKSPACE`` then ``cwd``
5. Relative ``broccolidb/`` when cwd already contains it
"""
env_root = os.environ.get("HERMES_BROCCOLIDB_ROOT", "").strip()
if env_root:
candidate = Path(env_root).expanduser()
if is_valid_broccolidb_root(candidate):
return str(candidate.resolve())
try:
from hermes_cli.config import load_config
cfg = load_config()
kanban_cfg = cfg.get("kanban", {}) if isinstance(cfg, dict) else {}
bdb = kanban_cfg.get("broccolidb", {})
if isinstance(bdb, dict):
cfg_root = str(bdb.get("root") or "").strip()
if cfg_root:
candidate = Path(cfg_root).expanduser()
if not candidate.is_absolute():
candidate = Path.cwd() / candidate
if is_valid_broccolidb_root(candidate):
return str(candidate.resolve())
except Exception:
pass
# Pip / standalone package: broccolidb/ ships beside this module.
package_bdb = Path(__file__).resolve().parent / _BROCCOLIDB_DIRNAME
if is_valid_broccolidb_root(package_bdb):
return str(package_bdb.resolve())
seeds: list[Path] = []
ws = os.environ.get("HERMES_KANBAN_WORKSPACE", "").strip()
if ws:
seeds.append(Path(ws))
seeds.append(Path.cwd())
seen: set[str] = set()
for seed in seeds:
try:
resolved_seed = seed.resolve()
except OSError:
continue
for parent in [resolved_seed, *resolved_seed.parents]:
key = str(parent)
if key in seen:
continue
seen.add(key)
candidate = parent / _BROCCOLIDB_DIRNAME
if is_valid_broccolidb_root(candidate):
return str(candidate.resolve())
for plugin_bdb in _plugin_broccolidb_candidates():
if is_valid_broccolidb_root(plugin_bdb):
return str(plugin_bdb.resolve())
rel = Path(_BROCCOLIDB_DIRNAME)
if is_valid_broccolidb_root(rel):
return str(rel.resolve())
return None