tracebase-cli 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tracebase_cli/__init__.py +1 -0
- tracebase_cli/client.py +192 -0
- tracebase_cli/connect.py +135 -0
- tracebase_cli/main.py +708 -0
- tracebase_cli/mcp_server.py +286 -0
- tracebase_cli/merge.py +312 -0
- tracebase_cli/plans.py +130 -0
- tracebase_cli/project.py +134 -0
- tracebase_cli/reuse.py +261 -0
- tracebase_cli/ui.py +113 -0
- tracebase_cli-0.3.0.dist-info/METADATA +95 -0
- tracebase_cli-0.3.0.dist-info/RECORD +15 -0
- tracebase_cli-0.3.0.dist-info/WHEEL +4 -0
- tracebase_cli-0.3.0.dist-info/entry_points.txt +2 -0
- tracebase_cli-0.3.0.dist-info/licenses/LICENSE.md +105 -0
tracebase_cli/plans.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Pending copy plans: what Tracebase *would* write into the project, saved until the user approves it.
|
|
2
|
+
|
|
3
|
+
An AI agent (through MCP) or `tracebase copy --later` creates a plan; nothing is written until the user approves
|
|
4
|
+
it (in the agent's approval prompt or with `tracebase apply` in a terminal). A plan is applied exactly as it was
|
|
5
|
+
previewed, and only if the files it touches have not changed since."""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
import secrets
|
|
10
|
+
import time
|
|
11
|
+
from dataclasses import asdict
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from . import reuse
|
|
15
|
+
|
|
16
|
+
DIR = Path(".tracebase") / "pending"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _dir(root: Path) -> Path:
|
|
20
|
+
return root / DIR
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def save(root: Path, bundle: dict, changes: list[reuse.Change], request: str = "") -> dict:
|
|
24
|
+
todo = [c for c in changes if c.new_text is not None]
|
|
25
|
+
written = "\n".join(c.new_text or "" for c in todo)
|
|
26
|
+
plan = {
|
|
27
|
+
"id": secrets.token_hex(3),
|
|
28
|
+
"created": time.strftime("%Y-%m-%d %H:%M:%S"),
|
|
29
|
+
"request": request,
|
|
30
|
+
"repo": bundle["repo"],
|
|
31
|
+
"picked": [f["path"] for f in bundle["files"] if f.get("role") == "picked"],
|
|
32
|
+
"adapted": bundle.get("adapted") or [],
|
|
33
|
+
"changes": [asdict(c) for c in changes],
|
|
34
|
+
"packages": reuse.packages_needed(bundle, written),
|
|
35
|
+
"env": reuse.env_missing(root, bundle, written),
|
|
36
|
+
}
|
|
37
|
+
reuse.private_dir(root) # backups and pending plans never get committed
|
|
38
|
+
d = _dir(root)
|
|
39
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
40
|
+
(d / f"{plan['id']}.json").write_text(json.dumps(plan, indent=1))
|
|
41
|
+
return plan
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def load(root: Path, plan_id: str) -> dict:
|
|
45
|
+
if not plan_id.isalnum():
|
|
46
|
+
raise reuse.CopyError(f"bad plan id: {plan_id}")
|
|
47
|
+
p = _dir(root) / f"{plan_id}.json"
|
|
48
|
+
if not p.is_file():
|
|
49
|
+
raise reuse.CopyError(f"no pending plan {plan_id} in {root} (run `tracebase pending` to list them)")
|
|
50
|
+
return json.loads(p.read_text())
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def pending(root: Path) -> list[dict]:
|
|
54
|
+
d = _dir(root)
|
|
55
|
+
plans = [json.loads(p.read_text()) for p in d.glob("*.json")] if d.is_dir() else []
|
|
56
|
+
return sorted(plans, key=lambda p: p["created"])
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def discard(root: Path, plan_id: str) -> None:
|
|
60
|
+
(_dir(root) / f"{plan_id}.json").unlink(missing_ok=True)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def changes(plan: dict) -> list[reuse.Change]:
|
|
64
|
+
return [reuse.Change(**c) for c in plan["changes"]]
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def stale(root: Path, plan: dict) -> list[str]:
|
|
68
|
+
"""Files that changed since the plan was previewed (applying would overwrite those edits)."""
|
|
69
|
+
out = []
|
|
70
|
+
for c in changes(plan):
|
|
71
|
+
if c.new_text is None:
|
|
72
|
+
continue
|
|
73
|
+
target = reuse.safe_target(root, c.target)
|
|
74
|
+
now = target.read_text(errors="ignore") if target.exists() else None
|
|
75
|
+
if now != c.old_text:
|
|
76
|
+
out.append(c.target)
|
|
77
|
+
return out
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def summary(plan: dict) -> str:
|
|
81
|
+
"""Short, human-readable description of a plan (used in approval prompts)."""
|
|
82
|
+
lines = [f"Copy from {plan['repo']} ({', '.join(plan['picked'])})"]
|
|
83
|
+
for c in changes(plan):
|
|
84
|
+
if c.new_text is None and c.kind != "manual":
|
|
85
|
+
continue
|
|
86
|
+
n = len((c.new_text or "").splitlines()) - len((c.old_text or "").splitlines())
|
|
87
|
+
size = f"{len(c.new_text.splitlines())} lines" if c.kind == "create" else f"+{max(n, 0)} lines"
|
|
88
|
+
what = {"create": "new file", "merge": "add " + ", ".join(c.added), "wire": "add wiring line",
|
|
89
|
+
"manual": "you add by hand", "deps": "add " + ", ".join(c.added),
|
|
90
|
+
"env": "document " + ", ".join(c.added)}.get(c.kind, c.kind)
|
|
91
|
+
sized = c.kind not in ("manual", "deps", "env")
|
|
92
|
+
lines.append(f" {c.kind:6} {c.target} ({what}{', ' + size if sized else ''})")
|
|
93
|
+
if plan.get("adapted"):
|
|
94
|
+
lines.append(" adapted to this project: " + "; ".join(plan["adapted"]))
|
|
95
|
+
loose = [p["name"] for p in plan["packages"] if not p.get("added_to")]
|
|
96
|
+
if loose:
|
|
97
|
+
lines.append(" packages to install: " + ", ".join(loose))
|
|
98
|
+
if plan["env"]:
|
|
99
|
+
lines.append(" env vars to set: " + ", ".join(plan["env"]))
|
|
100
|
+
return "\n".join(lines)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def apply(root: Path, plan: dict, only: set[str] | None = None) -> list[tuple[str, str]]:
|
|
104
|
+
"""Write the plan (or only the `only` targets). Returns [(note, target)]. Removes the plan afterwards."""
|
|
105
|
+
bad = stale(root, plan)
|
|
106
|
+
if bad:
|
|
107
|
+
raise reuse.CopyError("these files changed after the preview, preview again: " + ", ".join(bad))
|
|
108
|
+
backup = root / ".tracebase" / "backups" / time.strftime("%Y%m%d-%H%M%S")
|
|
109
|
+
done = [(reuse.apply(root, c, backup), c.target) for c in changes(plan)
|
|
110
|
+
if c.new_text is not None and (only is None or c.target in only)]
|
|
111
|
+
discard(root, plan["id"])
|
|
112
|
+
return done
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def install_steps(packages: list[dict]) -> list[str]:
|
|
116
|
+
"""`npm install` once for packages added to package.json, `pip install x` for the rest…"""
|
|
117
|
+
added = sorted({p["added_to"] for p in packages if p.get("added_to")})
|
|
118
|
+
out = [f"run `{reuse.INSTALL_AFTER[m]}` ({', '.join(p['name'] for p in packages if p.get('added_to') == m)} "
|
|
119
|
+
f"added to {m})" for m in added]
|
|
120
|
+
return out + [f"install {p['name']}: {p['install']}" for p in packages if not p.get("added_to")]
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def todo_text(plan: dict) -> list[str]:
|
|
124
|
+
"""What the user still has to do by hand after applying."""
|
|
125
|
+
out = [f"{c.target}: add `{s['line'].strip()}`" + (f" (inside: {s['inside']})" if s.get("inside") else "")
|
|
126
|
+
for c in changes(plan) for s in c.manual]
|
|
127
|
+
out += install_steps(plan["packages"])
|
|
128
|
+
if plan["env"]:
|
|
129
|
+
out.append(f"add to .env: {', '.join(plan['env'])}")
|
|
130
|
+
return out
|
tracebase_cli/project.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""Read the project in the current folder: file tree (respecting .gitignore) and the files most relevant
|
|
2
|
+
to a task. Only this context is sent to the Tracebase server; nothing is written here."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import pathspec
|
|
10
|
+
|
|
11
|
+
SKIP_DIRS = {".git", "node_modules", "vendor", ".venv", "venv", "__pycache__", "dist", "build", ".next",
|
|
12
|
+
".nuxt", "target", ".idea", ".vscode", "storage", "coverage", ".tracebase", ".pytest_cache"}
|
|
13
|
+
SKIP_EXT = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".ico", ".pdf", ".zip", ".gz", ".tar", ".mp4", ".mp3",
|
|
14
|
+
".woff", ".woff2", ".ttf", ".eot", ".lock", ".map", ".min.js", ".sqlite", ".db", ".jar", ".class"}
|
|
15
|
+
MANIFESTS = {"composer.json", "package.json", "requirements.txt", "pyproject.toml", "go.mod", "Gemfile",
|
|
16
|
+
"pom.xml", "build.gradle", "Cargo.toml", "tsconfig.json"}
|
|
17
|
+
|
|
18
|
+
# files that wire a feature into an app (routes, middleware registration, entry points, auth config)
|
|
19
|
+
WIRING = re.compile(
|
|
20
|
+
r"(^|/)(routes/[^/]+\.php|app/Http/Kernel\.php|bootstrap/app\.php|config/auth\.php|app/Models/User\.php|"
|
|
21
|
+
r"app/Providers/AuthServiceProvider\.php|(src/)?(app|server|index|main)\.(ts|js|mjs)|(src/)?routes?/[^/]+\.(ts|js)|"
|
|
22
|
+
r"settings\.py|urls\.py|main\.py|app\.py|models\.py|cmd/[^/]+/main\.go)$")
|
|
23
|
+
|
|
24
|
+
TOPICS = [
|
|
25
|
+
(r"log ?in|sign ?in|auth|jwt|token|session|password", ["auth", "login", "user", "session", "token", "guard", "password"]),
|
|
26
|
+
(r"rbac|role|permission|access control|authori[sz]|admin|policy|gate",
|
|
27
|
+
["role", "permission", "policy", "gate", "user", "middleware", "auth", "admin"]),
|
|
28
|
+
(r"upload|image|file", ["upload", "file", "image", "storage", "media"]),
|
|
29
|
+
(r"payment|stripe|checkout|subscription|webhook", ["payment", "stripe", "checkout", "webhook", "order", "invoice"]),
|
|
30
|
+
(r"mail|email|notif", ["mail", "notification", "notify"]),
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _gitignore(root: Path) -> pathspec.PathSpec | None:
|
|
35
|
+
f = root / ".gitignore"
|
|
36
|
+
if f.is_file():
|
|
37
|
+
lines = f.read_text(errors="ignore").splitlines()
|
|
38
|
+
for style in ("gitignore", "gitwildmatch"): # "gitignore" in pathspec >= 0.12.2
|
|
39
|
+
try:
|
|
40
|
+
return pathspec.PathSpec.from_lines(style, lines)
|
|
41
|
+
except Exception:
|
|
42
|
+
continue
|
|
43
|
+
return None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def scan_tree(root: Path, limit: int = 3000) -> list[str]:
|
|
47
|
+
spec = _gitignore(root)
|
|
48
|
+
out: list[str] = []
|
|
49
|
+
for dirpath, dirnames, filenames in os.walk(root):
|
|
50
|
+
rel_dir = Path(dirpath).relative_to(root).as_posix()
|
|
51
|
+
dirnames[:] = sorted(d for d in dirnames if d not in SKIP_DIRS and not d.startswith(".")
|
|
52
|
+
and not (spec and spec.match_file(f"{'' if rel_dir == '.' else rel_dir + '/'}{d}/")))
|
|
53
|
+
for name in sorted(filenames):
|
|
54
|
+
rel = name if rel_dir == "." else f"{rel_dir}/{name}"
|
|
55
|
+
low = name.lower()
|
|
56
|
+
if any(low.endswith(e) for e in SKIP_EXT) or name.startswith(".env"):
|
|
57
|
+
continue
|
|
58
|
+
if spec and spec.match_file(rel):
|
|
59
|
+
continue
|
|
60
|
+
out.append(rel)
|
|
61
|
+
if len(out) >= limit:
|
|
62
|
+
return out
|
|
63
|
+
return out
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def read_text(root: Path, rel: str, max_bytes: int = 200_000) -> str | None:
|
|
67
|
+
p = root / rel
|
|
68
|
+
try:
|
|
69
|
+
if not p.is_file() or p.stat().st_size > max_bytes:
|
|
70
|
+
return None
|
|
71
|
+
data = p.read_bytes()
|
|
72
|
+
if b"\x00" in data[:4096]:
|
|
73
|
+
return None
|
|
74
|
+
return data.decode("utf-8", errors="replace")
|
|
75
|
+
except OSError:
|
|
76
|
+
return None
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def keywords(task: str) -> list[str]:
|
|
80
|
+
low = task.lower()
|
|
81
|
+
words = [w for w in re.findall(r"[a-z]{3,}", low) if w not in {"the", "and", "add", "with", "from", "into",
|
|
82
|
+
"implement", "create", "write", "make", "for",
|
|
83
|
+
"project", "projects", "search", "this", "function",
|
|
84
|
+
"like", "use", "using", "based", "previous", "old"}]
|
|
85
|
+
for rx, extra in TOPICS:
|
|
86
|
+
if re.search(rx, low):
|
|
87
|
+
words += extra
|
|
88
|
+
return list(dict.fromkeys(words))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def context(root: Path, task: str = "", max_files: int = 14, budget: int = 60_000) -> dict:
|
|
92
|
+
"""{name, tree, files{path: text}}: manifests + wiring files + files whose path matches the task."""
|
|
93
|
+
tree = scan_tree(root)
|
|
94
|
+
kws = keywords(task)
|
|
95
|
+
|
|
96
|
+
def score(p: str) -> float:
|
|
97
|
+
name = p.split("/")[-1]
|
|
98
|
+
s = 0.0
|
|
99
|
+
if name in MANIFESTS and p.count("/") == 0:
|
|
100
|
+
s += 10
|
|
101
|
+
if WIRING.search(p):
|
|
102
|
+
s += 4
|
|
103
|
+
low = p.lower()
|
|
104
|
+
s += sum(2.0 for k in kws if k in low)
|
|
105
|
+
if "test" in low or low.startswith("docs/"):
|
|
106
|
+
s -= 3
|
|
107
|
+
return s
|
|
108
|
+
|
|
109
|
+
ranked = sorted((p for p in tree if score(p) > 0), key=lambda p: (-score(p), p.count("/"), p))
|
|
110
|
+
# classes the routes/wiring files point at (e.g. BookingController), so the model knows what exists
|
|
111
|
+
by_stem = {}
|
|
112
|
+
for p in tree:
|
|
113
|
+
by_stem.setdefault(Path(p).stem, p)
|
|
114
|
+
referenced = []
|
|
115
|
+
for p in ranked[:max_files]:
|
|
116
|
+
if WIRING.search(p):
|
|
117
|
+
for name in re.findall(r"\b([A-Z]\w+(?:Controller|Handler|Service))\b", read_text(root, p) or ""):
|
|
118
|
+
if name in by_stem and by_stem[name] not in ranked[:max_files] + referenced:
|
|
119
|
+
referenced.append(by_stem[name])
|
|
120
|
+
ranked = ranked[:max_files] + referenced[:4] + ranked[max_files:]
|
|
121
|
+
max_files += min(len(referenced), 4)
|
|
122
|
+
files, used = {}, 0
|
|
123
|
+
for p in ranked[:max_files * 2]:
|
|
124
|
+
text = read_text(root, p)
|
|
125
|
+
if text is None:
|
|
126
|
+
continue
|
|
127
|
+
text = text if len(text) <= 12_000 else text[:12_000] + "\n[truncated]"
|
|
128
|
+
if used + len(text) > budget:
|
|
129
|
+
continue
|
|
130
|
+
files[p] = text
|
|
131
|
+
used += len(text)
|
|
132
|
+
if len(files) >= max_files:
|
|
133
|
+
break
|
|
134
|
+
return {"name": root.name, "tree": tree, "files": files}
|
tracebase_cli/reuse.py
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"""Turn a bundle (the picked implementation + what it needs, from an old repo) into concrete file changes for
|
|
2
|
+
the project in the current folder. Shared by `tracebase copy` and the MCP tools. No AI, no network
|
|
3
|
+
(besides the local Tracebase server)."""
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
import re
|
|
8
|
+
import shutil
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from . import merge, project
|
|
13
|
+
|
|
14
|
+
LANG_BY_EXT = {".php": "php", ".py": "python", ".ts": "typescript", ".tsx": "typescript", ".js": "javascript",
|
|
15
|
+
".jsx": "javascript", ".mjs": "javascript", ".go": "go", ".java": "java", ".kt": "kotlin",
|
|
16
|
+
".rb": "ruby", ".cs": "csharp", ".rs": "rust"}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class Change:
|
|
21
|
+
target: str
|
|
22
|
+
kind: str # create | merge | wire | same | manual | deps | env
|
|
23
|
+
reason: str
|
|
24
|
+
source: str # repo/path it comes from
|
|
25
|
+
new_text: str | None = None
|
|
26
|
+
old_text: str | None = None
|
|
27
|
+
added: list[str] = field(default_factory=list)
|
|
28
|
+
manual: list[dict] = field(default_factory=list)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class CopyError(RuntimeError):
|
|
32
|
+
pass
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def safe_target(root: Path, rel: str) -> Path:
|
|
36
|
+
target = (root / rel).resolve()
|
|
37
|
+
if root.resolve() not in target.parents:
|
|
38
|
+
raise CopyError(f"refusing to write outside the project: {rel}")
|
|
39
|
+
return target
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _lang(path: str) -> str:
|
|
43
|
+
return LANG_BY_EXT.get(Path(path).suffix.lower(), "")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def needed_methods(incoming: str, lang: str, used_by: str) -> list[str]:
|
|
47
|
+
"""Methods of `incoming` that the rest of the copied code calls, plus what those call on $this/this/self."""
|
|
48
|
+
fns = merge.functions(incoming, lang)
|
|
49
|
+
want = [n for n in fns if re.search(rf"(->|\.|::|&\.)\s*{re.escape(n)}(?:\s*\(|(?![\w?!=])(?=[\s,)\]}};]|$))", used_by, re.M)]
|
|
50
|
+
i = 0
|
|
51
|
+
while i < len(want):
|
|
52
|
+
s, e = fns[want[i]]
|
|
53
|
+
body = incoming[s:e]
|
|
54
|
+
for n in fns:
|
|
55
|
+
if n not in want and re.search(rf"(\$this->|this\.|self\.){re.escape(n)}\b", body):
|
|
56
|
+
want.append(n)
|
|
57
|
+
i += 1
|
|
58
|
+
return want
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def plan_changes(root: Path, bundle: dict, only_new: bool = False) -> list[Change]:
|
|
62
|
+
files = bundle["files"]
|
|
63
|
+
changes: list[Change] = []
|
|
64
|
+
for f in files:
|
|
65
|
+
rel = f["target"]
|
|
66
|
+
src = f"{bundle['repo']}/{f['path']}"
|
|
67
|
+
safe_target(root, rel)
|
|
68
|
+
current = project.read_text(root, rel) if (root / rel).exists() else None
|
|
69
|
+
if current is None and f.get("role") == "wiring":
|
|
70
|
+
# an entry point (main.py, routes file…) this project doesn't have: never copy the old one whole
|
|
71
|
+
changes.append(Change(rel, "manual", f["reason"], src,
|
|
72
|
+
manual=[s for s in f.get("snippets") or [] if s["kind"] != "import"]))
|
|
73
|
+
continue
|
|
74
|
+
if current is None:
|
|
75
|
+
changes.append(Change(rel, "create", f["reason"], src, new_text=f["content"]))
|
|
76
|
+
continue
|
|
77
|
+
if only_new:
|
|
78
|
+
continue
|
|
79
|
+
if f.get("role") == "wiring":
|
|
80
|
+
snips = f.get("snippets") or []
|
|
81
|
+
text, added, manual = merge.insert_lines(current, snips)
|
|
82
|
+
if rel.endswith(".go"): # Go won't compile a declared-but-unused variable: leave those to the developer
|
|
83
|
+
code = re.sub(r'"(?:\\.|[^"\\])*"|`[^`]*`|//[^\n]*', '""', text)
|
|
84
|
+
unused = [ln for ln in added if (m := re.match(r"(\w+)\s*:=", ln))
|
|
85
|
+
and len(re.findall(rf"\b{re.escape(m.group(1))}\b", code)) == 1]
|
|
86
|
+
if unused:
|
|
87
|
+
keep = [sn for sn in snips if sn["line"].strip() not in unused]
|
|
88
|
+
text, added, manual = merge.insert_lines(current, keep)
|
|
89
|
+
manual += [dict(sn, kind="nested") for sn in snips if sn["line"].strip() in unused]
|
|
90
|
+
# …nor an unused import
|
|
91
|
+
body = re.sub(r"(?ms)^import\s*\(.*?^\)|^import\s+[^\n]+", "", text)
|
|
92
|
+
dead = [ln for ln in added if ln.startswith("import ") and not re.search(
|
|
93
|
+
rf"\b{re.escape(ln.split()[-1].strip(chr(34)).rsplit('/', 1)[-1])}\.", body)]
|
|
94
|
+
if dead:
|
|
95
|
+
keep = [sn for sn in snips if sn["line"].strip() not in dead and sn["line"].strip() not in unused]
|
|
96
|
+
text, added, manual2 = merge.insert_lines(current, keep)
|
|
97
|
+
manual = manual2 + [dict(sn, kind="nested") for sn in snips
|
|
98
|
+
if sn["line"].strip() in dead or sn["line"].strip() in unused]
|
|
99
|
+
kind = "wire" if added else ("manual" if manual else "same")
|
|
100
|
+
changes.append(Change(rel, kind, f["reason"], src, text if added else None, current, added, manual))
|
|
101
|
+
continue
|
|
102
|
+
lang = _lang(rel)
|
|
103
|
+
if not lang:
|
|
104
|
+
changes.append(Change(rel, "same" if current == f["content"] else "manual", f["reason"], src))
|
|
105
|
+
continue
|
|
106
|
+
others = "\n".join(x["content"] for x in files if x is not f)
|
|
107
|
+
only = None if f.get("role") == "picked" else needed_methods(f["content"], lang, others)
|
|
108
|
+
text, added = merge.add_functions(current, f["content"], lang, only=only)
|
|
109
|
+
if lang == "ruby":
|
|
110
|
+
text, macros = merge.add_ruby_macros(text, f["content"], others + "\n" + text)
|
|
111
|
+
added += macros
|
|
112
|
+
changes.append(Change(rel, "merge" if added else "same", f["reason"], src,
|
|
113
|
+
text if added else None, current, added))
|
|
114
|
+
changes = _drop_unmerged_associations(changes)
|
|
115
|
+
if not only_new:
|
|
116
|
+
written = "\n".join(c.new_text or "" for c in changes)
|
|
117
|
+
changes += manifest_changes(root, bundle, written)
|
|
118
|
+
env = env_example_change(root, bundle, written)
|
|
119
|
+
if env:
|
|
120
|
+
changes.append(env)
|
|
121
|
+
return changes
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _drop_unmerged_associations(changes: list[Change]) -> list[Change]:
|
|
125
|
+
"""Rails: Role came along only because the OLD User `has_many :roles`. If that association isn't merged
|
|
126
|
+
into this project's User, Role (and its migration) aren't needed."""
|
|
127
|
+
drop: set[str] = set()
|
|
128
|
+
for c in changes:
|
|
129
|
+
if not c.target.endswith(".rb") or c.kind not in ("merge", "same"):
|
|
130
|
+
continue
|
|
131
|
+
model = Path(c.target).stem
|
|
132
|
+
final = c.new_text if c.new_text is not None else (c.old_text or "")
|
|
133
|
+
for d in changes:
|
|
134
|
+
m = re.match(rf"^(\w+) is associated with {re.escape(model)}\b", d.reason)
|
|
135
|
+
if m and d.kind == "create":
|
|
136
|
+
table = re.sub(r"(?<=[a-z0-9])(?=[A-Z])", "_", m.group(1)).lower()
|
|
137
|
+
if not re.search(rf":{table}s?\b", final):
|
|
138
|
+
drop.add(d.target)
|
|
139
|
+
drop |= {x.target for x in changes if x.kind == "create" and
|
|
140
|
+
re.match(rf"^creates the \w+ table for {re.escape(Path(d.target).stem)}$", x.reason)}
|
|
141
|
+
return [c for c in changes if c.target not in drop]
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
# ------------------------------------------------------------------ dependency files and .env.example
|
|
145
|
+
INSTALL_AFTER = {"package.json": "npm install", "composer.json": "composer update", "requirements.txt":
|
|
146
|
+
"pip install -r requirements.txt", "Gemfile": "bundle install", "pom.xml": "mvn compile"}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _json_indent(text: str) -> int:
|
|
150
|
+
m = re.search(r"\n( +)\"", text)
|
|
151
|
+
return len(m.group(1)) if m else 2
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _add_json_deps(text: str, key: str, deps: dict[str, str]) -> str:
|
|
155
|
+
data = json.loads(text or "{}")
|
|
156
|
+
data.setdefault(key, {}).update(deps)
|
|
157
|
+
return json.dumps(data, indent=_json_indent(text), ensure_ascii=False) + "\n"
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def manifest_changes(root: Path, bundle: dict, written: str) -> list[Change]:
|
|
161
|
+
"""Add the packages the copied code uses to this project's dependency file (never installs anything)."""
|
|
162
|
+
pkgs = packages_needed(bundle, written)
|
|
163
|
+
by_file: dict[str, list[dict]] = {}
|
|
164
|
+
for p in pkgs:
|
|
165
|
+
target = {"package.json": "package.json", "composer.json": "composer.json", "python": "requirements.txt",
|
|
166
|
+
"Gemfile": "Gemfile", "pom.xml": "pom.xml"}.get(p["manager"])
|
|
167
|
+
if target and (root / target).is_file():
|
|
168
|
+
by_file.setdefault(target, []).append(p)
|
|
169
|
+
out = []
|
|
170
|
+
for target, items in by_file.items():
|
|
171
|
+
old = project.read_text(root, target) or ""
|
|
172
|
+
try:
|
|
173
|
+
if target == "package.json":
|
|
174
|
+
new = _add_json_deps(old, "dependencies", {p["name"]: p["version"] for p in items})
|
|
175
|
+
elif target == "composer.json":
|
|
176
|
+
new = _add_json_deps(old, "require", {p["name"]: p["version"] for p in items})
|
|
177
|
+
elif target == "requirements.txt":
|
|
178
|
+
new = old.rstrip("\n") + ("\n" if old.strip() else "") + "\n".join(p["version"] for p in items) + "\n"
|
|
179
|
+
elif target == "pom.xml":
|
|
180
|
+
close = old.rfind("</dependencies>")
|
|
181
|
+
if close < 0:
|
|
182
|
+
continue
|
|
183
|
+
ind = (re.search(r"\n([ \t]*)<dependency>", old) or re.search(r"\n([ \t]*)</dependencies>", old)).group(1)
|
|
184
|
+
ind = ind if "<dependency>" in old else ind + " "
|
|
185
|
+
xml = "".join(f"{ind}{p['xml']}\n" for p in items)
|
|
186
|
+
line_start = old.rfind("\n", 0, close) + 1
|
|
187
|
+
new = old[:line_start] + xml + old[line_start:]
|
|
188
|
+
else: # Gemfile
|
|
189
|
+
new = old.rstrip("\n") + "\n" + "\n".join(p.get("line") or f"gem '{p['name']}'" for p in items) + "\n"
|
|
190
|
+
except ValueError:
|
|
191
|
+
continue # unreadable manifest: leave it to the developer
|
|
192
|
+
if new != old:
|
|
193
|
+
for p in items:
|
|
194
|
+
p["added_to"] = target
|
|
195
|
+
out.append(Change(target, "deps", f"packages the copied code uses (then run `{INSTALL_AFTER[target]}`)",
|
|
196
|
+
bundle["repo"], new, old, [p["name"] for p in items]))
|
|
197
|
+
return out
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def env_example_change(root: Path, bundle: dict, written: str) -> Change | None:
|
|
201
|
+
"""Document the env vars the copied code reads in .env.example (empty values; .env is never touched)."""
|
|
202
|
+
missing = env_missing(root, bundle, written)
|
|
203
|
+
if not missing:
|
|
204
|
+
return None
|
|
205
|
+
target = ".env.example"
|
|
206
|
+
old = project.read_text(root, target) if (root / target).is_file() else None
|
|
207
|
+
head = (old.rstrip("\n") + "\n\n") if old and old.strip() else ""
|
|
208
|
+
new = head + f"# used by code copied from {bundle['repo']}\n" + "\n".join(f"{k}=" for k in missing) + "\n"
|
|
209
|
+
return Change(target, "env", "env vars the copied code reads (fill them in your .env)", bundle["repo"], new, old,
|
|
210
|
+
missing)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def private_dir(root: Path) -> Path:
|
|
214
|
+
""".tracebase/ in the project (backups, pending plans), never committed."""
|
|
215
|
+
d = root / ".tracebase"
|
|
216
|
+
d.mkdir(exist_ok=True)
|
|
217
|
+
gi = d / ".gitignore"
|
|
218
|
+
if not gi.exists():
|
|
219
|
+
gi.write_text("*\n")
|
|
220
|
+
return d
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def apply(root: Path, change: Change, backup_dir: Path) -> str:
|
|
224
|
+
if change.new_text is None:
|
|
225
|
+
return "skipped"
|
|
226
|
+
target = safe_target(root, change.target)
|
|
227
|
+
note = "created"
|
|
228
|
+
if target.exists():
|
|
229
|
+
private_dir(root)
|
|
230
|
+
dest = backup_dir / change.target
|
|
231
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
232
|
+
shutil.copy2(target, dest)
|
|
233
|
+
note = "updated"
|
|
234
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
235
|
+
target.write_text(change.new_text)
|
|
236
|
+
return note
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def packages_needed(bundle: dict, written: str) -> list[dict]:
|
|
240
|
+
"""Packages from the old project that the code actually written here uses."""
|
|
241
|
+
low = written.lower()
|
|
242
|
+
out = []
|
|
243
|
+
for p in bundle.get("packages", []):
|
|
244
|
+
name = p["name"].lower()
|
|
245
|
+
needles = {name, (p.get("module") or name).lower()} # import name, e.g. "jwt" for PyJWT
|
|
246
|
+
if "/" in name and not name.startswith("@"):
|
|
247
|
+
needles.add(name.split("/")[0]) # composer vendor, e.g. "tymon" for tymon/jwt-auth
|
|
248
|
+
if p.get("implicit") and (p.get("module") or "").lower() in low or \
|
|
249
|
+
any(re.search(rf"(?<![\w-]){re.escape(n)}(?![\w-])", low) for n in needles):
|
|
250
|
+
out.append(p)
|
|
251
|
+
return out
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def env_missing(root: Path, bundle: dict, written: str) -> list[str]:
|
|
255
|
+
keys = [k for k in bundle.get("env", []) if k in written]
|
|
256
|
+
have = ""
|
|
257
|
+
for name in (".env.example", ".env.sample", ".env"):
|
|
258
|
+
p = root / name
|
|
259
|
+
if p.is_file():
|
|
260
|
+
have += p.read_text(errors="ignore")
|
|
261
|
+
return [k for k in keys if not re.search(rf"^{re.escape(k)}=", have, re.M)]
|
tracebase_cli/ui.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""Small terminal helpers: colors, a spinner, colored unified diffs and prompts. No dependencies."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import difflib
|
|
5
|
+
import itertools
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
import threading
|
|
9
|
+
import time
|
|
10
|
+
|
|
11
|
+
COLOR = sys.stdout.isatty() and not os.environ.get("NO_COLOR")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def c(text: str, code: str) -> str:
|
|
15
|
+
return f"\033[{code}m{text}\033[0m" if COLOR else text
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def bold(t: str) -> str: return c(t, "1")
|
|
19
|
+
def dim(t: str) -> str: return c(t, "2")
|
|
20
|
+
def green(t: str) -> str: return c(t, "32")
|
|
21
|
+
def red(t: str) -> str: return c(t, "31")
|
|
22
|
+
def yellow(t: str) -> str: return c(t, "33")
|
|
23
|
+
def cyan(t: str) -> str: return c(t, "36")
|
|
24
|
+
def teal(t: str) -> str: return c(t, "38;5;37")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def header(text: str) -> None:
|
|
28
|
+
print(f"\n{teal('●')} {bold(text)}")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def info(text: str) -> None:
|
|
32
|
+
print(f" {text}")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def warn(text: str) -> None:
|
|
36
|
+
print(f" {yellow('!')} {text}")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def error(text: str) -> None:
|
|
40
|
+
print(f"{red('✗')} {text}", file=sys.stderr)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class Spinner:
|
|
44
|
+
"""`with Spinner("Searching your projects"):` — animated on a TTY, a single line otherwise."""
|
|
45
|
+
|
|
46
|
+
def __init__(self, text: str) -> None:
|
|
47
|
+
self.text = text
|
|
48
|
+
self._stop = threading.Event()
|
|
49
|
+
self._t0 = time.time()
|
|
50
|
+
self._thread: threading.Thread | None = None
|
|
51
|
+
|
|
52
|
+
def __enter__(self) -> "Spinner":
|
|
53
|
+
if sys.stderr.isatty():
|
|
54
|
+
self._thread = threading.Thread(target=self._spin, daemon=True)
|
|
55
|
+
self._thread.start()
|
|
56
|
+
else:
|
|
57
|
+
print(f" {self.text}…", file=sys.stderr)
|
|
58
|
+
return self
|
|
59
|
+
|
|
60
|
+
def _spin(self) -> None:
|
|
61
|
+
for ch in itertools.cycle("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"):
|
|
62
|
+
if self._stop.is_set():
|
|
63
|
+
break
|
|
64
|
+
sys.stderr.write(f"\r {teal(ch)} {self.text}… {dim(f'{time.time() - self._t0:.0f}s')} ")
|
|
65
|
+
sys.stderr.flush()
|
|
66
|
+
time.sleep(0.1)
|
|
67
|
+
|
|
68
|
+
def __exit__(self, *exc: object) -> None:
|
|
69
|
+
self._stop.set()
|
|
70
|
+
if self._thread:
|
|
71
|
+
self._thread.join()
|
|
72
|
+
sys.stderr.write("\r\033[K")
|
|
73
|
+
sys.stderr.flush()
|
|
74
|
+
|
|
75
|
+
@property
|
|
76
|
+
def elapsed(self) -> float:
|
|
77
|
+
return time.time() - self._t0
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def diff(path: str, old: str | None, new: str, max_lines: int = 400) -> str:
|
|
81
|
+
a = (old or "").splitlines(keepends=False)
|
|
82
|
+
b = new.splitlines(keepends=False)
|
|
83
|
+
lines = list(difflib.unified_diff(a, b, fromfile=f"a/{path}" if old is not None else "/dev/null",
|
|
84
|
+
tofile=f"b/{path}", lineterm="", n=3))
|
|
85
|
+
out = []
|
|
86
|
+
for ln in lines[:max_lines]:
|
|
87
|
+
if ln.startswith(("+++", "---")):
|
|
88
|
+
out.append(bold(ln))
|
|
89
|
+
elif ln.startswith("@@"):
|
|
90
|
+
out.append(cyan(ln))
|
|
91
|
+
elif ln.startswith("+"):
|
|
92
|
+
out.append(green(ln))
|
|
93
|
+
elif ln.startswith("-"):
|
|
94
|
+
out.append(red(ln))
|
|
95
|
+
else:
|
|
96
|
+
out.append(ln)
|
|
97
|
+
if len(lines) > max_lines:
|
|
98
|
+
out.append(dim(f"… {len(lines) - max_lines} more diff lines (use [e] to open the full file)"))
|
|
99
|
+
return "\n".join(out)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def ask(prompt: str, choices: str, default: str) -> str:
|
|
103
|
+
"""Single-letter prompt, e.g. ask("Apply?", "ynaeq", "y")."""
|
|
104
|
+
shown = "/".join(ch.upper() if ch == default else ch for ch in choices)
|
|
105
|
+
while True:
|
|
106
|
+
try:
|
|
107
|
+
ans = input(f"{prompt} [{shown}] ").strip().lower()
|
|
108
|
+
except EOFError:
|
|
109
|
+
return "q"
|
|
110
|
+
if not ans:
|
|
111
|
+
return default
|
|
112
|
+
if ans[0] in choices:
|
|
113
|
+
return ans[0]
|