@sakanano-moe/agenthelm 6.13.1 → 6.14.0
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.
- package/package.json +1 -1
- package/runtime/index.json +6 -6
- package/runtime/plugins/agent-pack/.codex-plugin/plugin.json +1 -1
- package/runtime/plugins/agent-pack/plugin.json +1 -1
- package/runtime/plugins/agent-pack/references/capability-config.md +10 -0
- package/runtime/plugins/agent-pack/references/check-opinion-migrate.en.md +31 -0
- package/runtime/plugins/agent-pack/references/check-opinion-migrate.md +31 -0
- package/runtime/plugins/agent-pack/references/dedupe-contract.en.md +27 -0
- package/runtime/plugins/agent-pack/references/dedupe-contract.md +27 -0
- package/runtime/plugins/agent-pack/references/inventory-contract.en.md +83 -0
- package/runtime/plugins/agent-pack/references/inventory-contract.md +83 -0
- package/runtime/plugins/agent-pack/references/nudge.en.md +10 -2
- package/runtime/plugins/agent-pack/references/nudge.md +10 -2
- package/runtime/plugins/agent-pack/references/relocate-contract.en.md +41 -0
- package/runtime/plugins/agent-pack/references/relocate-contract.md +41 -0
- package/runtime/plugins/agent-pack/runtime/agent_pack_claude.py +572 -30
- package/runtime/plugins/agent-pack/runtime/agent_pack_clients.py +233 -0
- package/runtime/plugins/agent-pack/runtime/agent_pack_config.py +112 -4
- package/runtime/plugins/agent-pack/runtime/agent_pack_dedupe.py +502 -0
- package/runtime/plugins/agent-pack/runtime/agent_pack_desktop.py +92 -0
- package/runtime/plugins/agent-pack/runtime/agent_pack_ext_common.py +251 -0
- package/runtime/plugins/agent-pack/runtime/agent_pack_inventory.py +989 -0
- package/runtime/plugins/agent-pack/runtime/agent_pack_mcp_write.py +447 -0
- package/runtime/plugins/agent-pack/runtime/agent_pack_nudge.py +29 -4
- package/runtime/plugins/agent-pack/runtime/agent_pack_relocate.py +385 -0
- package/runtime/plugins/agent-pack/runtime/agent_pack_scope.py +233 -0
- package/runtime/plugins/agent-pack/runtime/agenthelm.py +183 -14
- package/runtime/plugins/agent-pack/runtime/locale/en.json +250 -1
- package/runtime/plugins/agent-pack/runtime/mcp_server.py +12 -0
- package/runtime/plugins/agent-pack/runtime/nudge_text.py +8 -2
- package/runtime/plugins/agent-pack/runtime/user_settings.py +2 -1
- package/runtime/plugins/agent-pack/skills/agent-pack/SKILL.md +15 -0
- package/runtime/plugins/agents-init/.codex-plugin/plugin.json +1 -1
- package/runtime/plugins/agents-init/content.json +4 -2
- package/runtime/plugins/agents-init/plugin.json +1 -1
- package/runtime/plugins/agents-init/skills/agents-init/SKILL.md +2 -0
- package/runtime/plugins/agents-init/skills/agents-init/launcher.en.sh +93 -0
- package/runtime/plugins/agents-init/skills/agents-init/project.schema.json +27 -0
- package/runtime/plugins/agents-init/skills/agents-init/scripts/baseline.py +4 -1
- package/runtime/plugins/agents-init/skills/agents-init/scripts/init_project.py +24 -9
- package/runtime/plugins/agents-init/skills/agents-init/skill.json +1 -1
- package/runtime/plugins/opinion-manager/.codex-plugin/plugin.json +1 -1
- package/runtime/plugins/opinion-manager/catalog/README.md +3 -1
- package/runtime/plugins/opinion-manager/catalog/rules/om.align-requirements/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.assumptions-authority/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.batch-questions/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.bilingual-terms/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.calculations/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.chinese-response/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.clean-deliverable/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.complete-options/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.conclusion-first/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.concrete-titles/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.consistent-items/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.device-readable/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.editable-diagrams/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.evidence-inference/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.maintenance-chinese/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.maintenance-project-language/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.markdown-structure/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.portable-files/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.report-context/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.report-formats/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.report-review/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.response-brief/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.response-dense/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.rules-tools/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.semantic-wrap/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.solutions-proportional/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.solutions-three-level/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.tables-compact/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.tables-list-first/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.test-evidence/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/rules/om.versioned-files/versions/1.1.0.json +34 -0
- package/runtime/plugins/opinion-manager/catalog/templates/delivery-standard/compact-tables/versions/1.1.0.json +32 -0
- package/runtime/plugins/opinion-manager/catalog/templates/delivery-standard/list-first/versions/1.1.0.json +32 -0
- package/runtime/plugins/opinion-manager/catalog/templates/development-standard/chinese-maintenance/versions/1.1.0.json +26 -0
- package/runtime/plugins/opinion-manager/catalog/templates/development-standard/project-language/versions/1.1.0.json +26 -0
- package/runtime/plugins/opinion-manager/catalog/templates/response-style/brief-chinese/versions/1.1.0.json +30 -0
- package/runtime/plugins/opinion-manager/catalog/templates/response-style/dense-chinese/versions/1.1.0.json +30 -0
- package/runtime/plugins/opinion-manager/catalog/templates/scenario/concise-project/versions/1.1.0.json +181 -0
- package/runtime/plugins/opinion-manager/catalog/templates/scenario/reviewed-project/versions/1.1.0.json +179 -0
- package/runtime/plugins/opinion-manager/catalog/templates/work-method/proportional/versions/1.1.0.json +31 -0
- package/runtime/plugins/opinion-manager/catalog/templates/work-method/three-level/versions/1.1.0.json +31 -0
- package/runtime/plugins/opinion-manager/content.json +44 -3
- package/runtime/plugins/opinion-manager/plugin.json +1 -1
- package/runtime/plugins/opinion-manager/references/inbox.en.md +25 -4
- package/runtime/plugins/opinion-manager/references/inbox.md +25 -4
- package/runtime/plugins/opinion-manager/references/profile.schema.json +19 -2
- package/runtime/plugins/opinion-manager/references/rule.schema.json +25 -3
- package/runtime/plugins/opinion-manager/references/versioned-template.schema.json +5 -4
- package/runtime/plugins/opinion-manager/references/versioning.md +89 -4
- package/runtime/plugins/opinion-manager/runtime/context.py +26 -19
- package/runtime/plugins/opinion-manager/runtime/inbox.py +400 -25
- package/runtime/plugins/opinion-manager/runtime/inbox_relations.py +106 -0
- package/runtime/plugins/opinion-manager/runtime/inbox_routes.py +132 -0
- package/runtime/plugins/opinion-manager/runtime/inbox_text.py +117 -11
- package/runtime/plugins/opinion-manager/runtime/lifecycle.py +35 -5
- package/runtime/plugins/opinion-manager/runtime/locale/en.json +137 -2
- package/runtime/plugins/opinion-manager/runtime/migrate.py +582 -0
- package/runtime/plugins/opinion-manager/runtime/opinion_manager.py +9 -4
- package/runtime/plugins/opinion-manager/runtime/rule_lines.py +247 -0
- package/runtime/plugins/opinion-manager/runtime/tags.py +217 -0
- package/runtime/plugins/opinion-manager/runtime/versioned.py +289 -41
- package/runtime/plugins/opinion-manager/skills/opinion-manager/SKILL.md +8 -7
- package/runtime/plugins/project-orchestrator/.codex-plugin/plugin.json +1 -1
- package/runtime/plugins/project-orchestrator/plugin.json +1 -1
- package/runtime/plugins/project-orchestrator/runtime/locale/en.json +305 -3
- package/runtime/plugins/project-orchestrator/runtime/private_paths.py +11 -1
- package/runtime/plugins/project-orchestrator/runtime/project_cli.py +101 -1
- package/runtime/plugins/project-orchestrator/runtime/project_demo.py +13 -1
- package/runtime/plugins/project-orchestrator/runtime/project_ext_actions.py +415 -0
- package/runtime/plugins/project-orchestrator/runtime/project_extensions.py +971 -0
- package/runtime/plugins/project-orchestrator/runtime/project_extensions_demo.py +86 -0
- package/runtime/plugins/project-orchestrator/runtime/project_menu.py +345 -2
- package/runtime/plugins/project-orchestrator/runtime/project_real.py +17 -1
- package/runtime/plugins/project-orchestrator/skills/project-orchestrator/SKILL.md +2 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Shared building blocks of the Skill write commands (``agenthelm relocate`` / ``dedupe``).
|
|
3
|
+
|
|
4
|
+
Plan shape, ``plan_digest``, directory-tree digest and read-back, synced-folder heuristic, and the archive step
|
|
5
|
+
(a move into the recovery directory, never a delete) live here so both commands behave the same way. Nothing in
|
|
6
|
+
this module prints a secret: it only ever sees Skill directories and paths.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import hashlib
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
import shlex
|
|
15
|
+
import shutil
|
|
16
|
+
import sys
|
|
17
|
+
from datetime import UTC, datetime
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
RUNTIME_DIR = Path(__file__).resolve().parent
|
|
22
|
+
if str(RUNTIME_DIR) not in sys.path:
|
|
23
|
+
sys.path.insert(0, str(RUNTIME_DIR))
|
|
24
|
+
|
|
25
|
+
SCHEMA_VERSION = 1
|
|
26
|
+
PROMISES = ("certain", "best-effort", "cannot-promise")
|
|
27
|
+
MAX_TREE_FILES = 20000
|
|
28
|
+
# Folder names of the common file-sync products. A heuristic: a match demotes a symlink to a copy and the plan says
|
|
29
|
+
# so; no match proves nothing (the official documentation on symlinks inside synced folders was not found).
|
|
30
|
+
SYNC_FOLDER_MARKERS = ("icloud drive", "mobile documents", "dropbox", "onedrive", "google drive", "googledrive",
|
|
31
|
+
"cloudstorage", "box sync", "pcloud", "nextcloud", "syncthing")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class ExtError(Exception):
|
|
35
|
+
"""A failure that must roll back and be reported without a traceback."""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def canonical(data: Any) -> str:
|
|
39
|
+
return json.dumps(data, sort_keys=True, ensure_ascii=False, separators=(",", ":"))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def plan_digest(plan: dict) -> str:
|
|
43
|
+
"""SHA-256 over the plan without its own digest and without per-run values (timestamps)."""
|
|
44
|
+
body = {key: value for key, value in plan.items() if key not in ("plan_digest", "recovery_dir", "next")}
|
|
45
|
+
return hashlib.sha256(canonical(_without_text(body)).encode("utf-8")).hexdigest()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _without_text(value: Any) -> Any:
|
|
49
|
+
"""Localized wording (``message`` / ``text``) is not part of the digest: it must not depend on the language."""
|
|
50
|
+
if isinstance(value, dict):
|
|
51
|
+
return {key: _without_text(item) for key, item in value.items() if key not in ("message", "text")}
|
|
52
|
+
if isinstance(value, list):
|
|
53
|
+
return [_without_text(item) for item in value]
|
|
54
|
+
return value
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def warning(code: str, message: str, **detail: Any) -> dict:
|
|
58
|
+
return {"code": code, "message": message, **detail}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def step(step_id: str, action: str, promise: str, **detail: Any) -> dict:
|
|
62
|
+
assert promise in PROMISES
|
|
63
|
+
return {"id": step_id, "action": action, "promise": promise, **detail}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def blocked(code: str, message: str, **detail: Any) -> dict:
|
|
67
|
+
return {"code": code, "message": message, **detail}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# ------------------------------------------------------------------ directory trees
|
|
71
|
+
|
|
72
|
+
def tree_manifest(root: Path) -> list[list[str]] | None:
|
|
73
|
+
"""``[kind, relative path, digest-or-target]`` for every entry below ``root`` (a directory, or a link to one).
|
|
74
|
+
|
|
75
|
+
Files carry their SHA-256 and executable bit, symbolic links their literal target, empty directories only the
|
|
76
|
+
path. ``None`` when the tree has more than ``MAX_TREE_FILES`` entries. Symbolic links are never followed.
|
|
77
|
+
"""
|
|
78
|
+
top = Path(os.path.realpath(root))
|
|
79
|
+
rows: list[list[str]] = []
|
|
80
|
+
for base, dirs, files in os.walk(top, followlinks=False):
|
|
81
|
+
dirs.sort()
|
|
82
|
+
base_path = Path(base)
|
|
83
|
+
relative_base = base_path.relative_to(top)
|
|
84
|
+
for name in sorted(dirs):
|
|
85
|
+
full = base_path / name
|
|
86
|
+
if full.is_symlink():
|
|
87
|
+
rows.append(["l", (relative_base / name).as_posix(), os.readlink(full)])
|
|
88
|
+
if not dirs and not files and base_path != top:
|
|
89
|
+
rows.append(["d", relative_base.as_posix(), ""])
|
|
90
|
+
for name in sorted(files):
|
|
91
|
+
full = base_path / name
|
|
92
|
+
rel = (relative_base / name).as_posix()
|
|
93
|
+
if full.is_symlink():
|
|
94
|
+
rows.append(["l", rel, os.readlink(full)])
|
|
95
|
+
continue
|
|
96
|
+
digest = hashlib.sha256()
|
|
97
|
+
with full.open("rb") as handle:
|
|
98
|
+
for chunk in iter(lambda: handle.read(1 << 20), b""):
|
|
99
|
+
digest.update(chunk)
|
|
100
|
+
rows.append(["f", rel, digest.hexdigest() + (":x" if os.access(full, os.X_OK) else "")])
|
|
101
|
+
if len(rows) > MAX_TREE_FILES:
|
|
102
|
+
return None
|
|
103
|
+
rows.sort(key=lambda row: (row[1], row[0]))
|
|
104
|
+
return rows
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def tree_digest(manifest: list[list[str]]) -> str:
|
|
108
|
+
return hashlib.sha256(canonical(manifest).encode("utf-8")).hexdigest()
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def external_links(root: Path, manifest: list[list[str]]) -> list[str]:
|
|
112
|
+
"""Relative paths of symbolic links inside the tree that point outside it (or are absolute)."""
|
|
113
|
+
top = Path(os.path.realpath(root))
|
|
114
|
+
bad = []
|
|
115
|
+
for kind, rel, target in manifest:
|
|
116
|
+
if kind != "l":
|
|
117
|
+
continue
|
|
118
|
+
resolved = Path(os.path.realpath(top / rel))
|
|
119
|
+
inside = resolved == top or str(resolved).startswith(str(top) + os.sep)
|
|
120
|
+
if os.path.isabs(target) or not inside:
|
|
121
|
+
bad.append(rel)
|
|
122
|
+
return bad
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def copy_tree(source: Path, destination: Path) -> None:
|
|
126
|
+
"""Copy keeping inner symbolic links as links and file modes; ``destination`` must not exist."""
|
|
127
|
+
shutil.copytree(Path(os.path.realpath(source)), destination, symlinks=True)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def readback_matches(source_manifest: list[list[str]], target: Path) -> bool:
|
|
131
|
+
"""Per-file comparison of relative paths and content digests (includes ``SKILL.md``)."""
|
|
132
|
+
manifest = tree_manifest(target)
|
|
133
|
+
return manifest is not None and manifest == source_manifest
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
# ------------------------------------------------------------------ paths
|
|
137
|
+
|
|
138
|
+
def display(path: Path | str, home: Path, project: Path | None) -> str:
|
|
139
|
+
text = str(path)
|
|
140
|
+
for base, label in ((project, "<project>"), (home, "~")):
|
|
141
|
+
if base is None:
|
|
142
|
+
continue
|
|
143
|
+
prefix = str(base)
|
|
144
|
+
if text == prefix:
|
|
145
|
+
return label
|
|
146
|
+
if text.startswith(prefix + os.sep):
|
|
147
|
+
return label + text[len(prefix):]
|
|
148
|
+
return text
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def shell_path(path: Path | str, home: Path) -> str:
|
|
152
|
+
"""A path that can be pasted into a shell: ``~/`` kept outside the quotes so the shell expands it."""
|
|
153
|
+
text = str(path)
|
|
154
|
+
prefix = str(home) + os.sep
|
|
155
|
+
if text.startswith(prefix):
|
|
156
|
+
return "~/" + shlex.quote(text[len(prefix):])
|
|
157
|
+
return shlex.quote(text)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def sync_folder_hint(path: Path) -> str | None:
|
|
161
|
+
"""Name of the sync-product folder ``path`` sits in (checked on the literal and the resolved path), else ``None``."""
|
|
162
|
+
for candidate in (Path(os.path.abspath(path)), Path(os.path.realpath(path))):
|
|
163
|
+
for part in candidate.parts:
|
|
164
|
+
lowered = part.lower()
|
|
165
|
+
for marker in SYNC_FOLDER_MARKERS:
|
|
166
|
+
if lowered == marker or lowered.startswith(marker + " ") or lowered.startswith(marker + "-"):
|
|
167
|
+
return part
|
|
168
|
+
return None
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def recovery_base(home: Path, override: Path | None = None) -> Path:
|
|
172
|
+
return (override or home / ".agent-pack" / "recovery").expanduser().resolve()
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def new_recovery_dir(base: Path, label: str) -> Path:
|
|
176
|
+
stamp = datetime.now(UTC).strftime("%Y%m%dT%H%M%S.%fZ")
|
|
177
|
+
directory = base / f"{stamp}-{label}"
|
|
178
|
+
directory.mkdir(parents=True, mode=0o700)
|
|
179
|
+
os.chmod(directory, 0o700)
|
|
180
|
+
return directory
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
# ------------------------------------------------------------------ archive (a move, never a delete)
|
|
184
|
+
|
|
185
|
+
def archive(path: Path, recovery: Path, bucket: str, name: str) -> Path:
|
|
186
|
+
"""Move ``path`` (a directory or a symbolic link) to ``recovery/<bucket>/<name>``; returns the new place."""
|
|
187
|
+
destination = recovery / bucket / name
|
|
188
|
+
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
189
|
+
if destination.exists() or destination.is_symlink():
|
|
190
|
+
raise ExtError(f"archive destination exists: {destination.name}")
|
|
191
|
+
shutil.move(str(path), str(destination))
|
|
192
|
+
return destination
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def restore_line(archived: Path, original: Path, home: Path) -> str:
|
|
196
|
+
return f"mv {shell_path(archived, home)} {shell_path(original, home)}"
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def rollback_moves(moved: list[tuple[Path, Path]]) -> None:
|
|
200
|
+
"""Undo archive moves in reverse order; an original that reappeared meanwhile is never overwritten."""
|
|
201
|
+
for archived, original in reversed(moved):
|
|
202
|
+
if (archived.exists() or archived.is_symlink()) and not (original.exists() or original.is_symlink()):
|
|
203
|
+
original.parent.mkdir(parents=True, exist_ok=True)
|
|
204
|
+
shutil.move(str(archived), str(original))
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def remove_created(path: Path) -> None:
|
|
208
|
+
"""Remove something this run created (a link, or a directory copy)."""
|
|
209
|
+
if path.is_symlink():
|
|
210
|
+
path.unlink()
|
|
211
|
+
elif path.is_dir():
|
|
212
|
+
shutil.rmtree(path, ignore_errors=True)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def remove_empty_parents(paths: list[Path]) -> None:
|
|
216
|
+
for path in reversed(paths):
|
|
217
|
+
try:
|
|
218
|
+
path.rmdir()
|
|
219
|
+
except OSError:
|
|
220
|
+
pass
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def make_parents(directory: Path) -> list[Path]:
|
|
224
|
+
"""``mkdir -p`` that returns the directories it created, outermost first."""
|
|
225
|
+
missing = []
|
|
226
|
+
current = directory
|
|
227
|
+
while not current.exists():
|
|
228
|
+
missing.append(current)
|
|
229
|
+
current = current.parent
|
|
230
|
+
missing.reverse()
|
|
231
|
+
for item in missing:
|
|
232
|
+
item.mkdir()
|
|
233
|
+
return missing
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def write_private_json(path: Path, data: Any) -> None:
|
|
237
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
238
|
+
temporary = path.with_name(f".{path.name}.{os.getpid()}.tmp")
|
|
239
|
+
descriptor = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
240
|
+
with os.fdopen(descriptor, "w", encoding="utf-8") as handle:
|
|
241
|
+
handle.write(json.dumps(data, ensure_ascii=False, indent=2) + "\n")
|
|
242
|
+
os.replace(temporary, path)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def use_color(stream=None) -> bool:
|
|
246
|
+
stream = stream or sys.stdout
|
|
247
|
+
return bool(getattr(stream, "isatty", lambda: False)()) and not os.environ.get("NO_COLOR")
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def red(text: str, color: bool) -> str:
|
|
251
|
+
return f"\033[31m{text}\033[0m" if color else text
|