scriptvault 0.1.6b0__tar.gz → 0.2.1b0__tar.gz
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.
- {scriptvault-0.1.6b0/src/scriptvault.egg-info → scriptvault-0.2.1b0}/PKG-INFO +1 -1
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/pyproject.toml +1 -1
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault/__init__.py +2 -2
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault/cli.py +327 -3
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault/run.py +93 -31
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault/vault.py +56 -7
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0/src/scriptvault.egg-info}/PKG-INFO +1 -1
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/tests/test_add.py +4 -1
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/tests/test_completion.py +7 -4
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/tests/test_real.py +1 -1
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/tests/test_run.py +54 -1
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/LICENSE +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/README.md +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/setup.cfg +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault/__main__.py +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault/envfile.py +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault/templates.py +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault/term.py +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault.egg-info/SOURCES.txt +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault.egg-info/dependency_links.txt +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault.egg-info/entry_points.txt +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault.egg-info/requires.txt +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/src/scriptvault.egg-info/top_level.txt +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/tests/test_edit.py +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/tests/test_init.py +0 -0
- {scriptvault-0.1.6b0 → scriptvault-0.2.1b0}/tests/test_list.py +0 -0
|
@@ -23,7 +23,19 @@ from .vault import (
|
|
|
23
23
|
load_vault,
|
|
24
24
|
)
|
|
25
25
|
|
|
26
|
-
COMMANDS = (
|
|
26
|
+
COMMANDS = (
|
|
27
|
+
"add",
|
|
28
|
+
"activate",
|
|
29
|
+
"deactivate",
|
|
30
|
+
"completion",
|
|
31
|
+
"edit",
|
|
32
|
+
"help",
|
|
33
|
+
"init",
|
|
34
|
+
"init-hooks",
|
|
35
|
+
"install-hooks",
|
|
36
|
+
"list",
|
|
37
|
+
"run",
|
|
38
|
+
)
|
|
27
39
|
BUILTIN_COMMANDS = set(COMMANDS) | {"__names"}
|
|
28
40
|
RESERVED_NAMES = BUILTIN_COMMANDS | {"config", "env"}
|
|
29
41
|
|
|
@@ -72,6 +84,282 @@ def cmd_init(args: argparse.Namespace) -> int:
|
|
|
72
84
|
return 0
|
|
73
85
|
|
|
74
86
|
|
|
87
|
+
def _interactive_select_hooks(hooks: list[tuple[str, str]]) -> list[str]:
|
|
88
|
+
"""Interactive selection with space to select, arrows to navigate."""
|
|
89
|
+
import sys
|
|
90
|
+
import tty
|
|
91
|
+
import termios
|
|
92
|
+
|
|
93
|
+
# Check if we're in a real TTY before trying terminal control
|
|
94
|
+
if not _interactive():
|
|
95
|
+
# Fall back to simple input-based selection
|
|
96
|
+
return _simple_select_hooks(hooks)
|
|
97
|
+
|
|
98
|
+
# At this point we have a real TTY, use interactive mode
|
|
99
|
+
fd = sys.stdin.fileno()
|
|
100
|
+
old_settings = tty.tcgetattr(fd)
|
|
101
|
+
tty.setcbreak(fd)
|
|
102
|
+
|
|
103
|
+
try:
|
|
104
|
+
hooks_list = [
|
|
105
|
+
(i, name, desc, False) for i, (name, desc) in enumerate(hooks, 1)
|
|
106
|
+
]
|
|
107
|
+
total = len(hooks_list)
|
|
108
|
+
selection = [False] * total
|
|
109
|
+
highlighted = 0
|
|
110
|
+
|
|
111
|
+
while True:
|
|
112
|
+
# Clear screen and redraw
|
|
113
|
+
sys.stdout.write("\033[2J\033[H")
|
|
114
|
+
|
|
115
|
+
print("Select hooks to create (space to toggle, Enter to confirm, q to quit)")
|
|
116
|
+
print()
|
|
117
|
+
|
|
118
|
+
for i, (
|
|
119
|
+
index,
|
|
120
|
+
hook_name,
|
|
121
|
+
desc,
|
|
122
|
+
selected,
|
|
123
|
+
) in enumerate(hooks_list):
|
|
124
|
+
if index == highlighted:
|
|
125
|
+
prefix = " > "
|
|
126
|
+
reset = "\033[0m"
|
|
127
|
+
else:
|
|
128
|
+
prefix = " "
|
|
129
|
+
reset = "\033[0m"
|
|
130
|
+
|
|
131
|
+
mark = " [x]" if selected else " [ ]"
|
|
132
|
+
sys.stdout.write(
|
|
133
|
+
f"{prefix}{mark} {index:2d}. {hook_name} - {desc}{reset}\n"
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
print()
|
|
137
|
+
|
|
138
|
+
# Get keypress
|
|
139
|
+
key = sys.stdin.read(1)
|
|
140
|
+
|
|
141
|
+
if key == "\n" or key == "\r":
|
|
142
|
+
# Confirm selection
|
|
143
|
+
selections = [hooks_list[i][1] for i, s in enumerate(selection) if s]
|
|
144
|
+
return selections
|
|
145
|
+
elif key == "q":
|
|
146
|
+
return []
|
|
147
|
+
elif key == "\x1b": # ESC - quit
|
|
148
|
+
return []
|
|
149
|
+
elif key == " ":
|
|
150
|
+
# Toggle selection
|
|
151
|
+
if highlighted < total:
|
|
152
|
+
selection[highlighted] = not selection[highlighted]
|
|
153
|
+
elif key == "\x1b[A": # Up arrow
|
|
154
|
+
highlighted = (highlighted - 1) % total
|
|
155
|
+
elif key == "\x1b[B": # Down arrow
|
|
156
|
+
highlighted = (highlighted + 1) % total
|
|
157
|
+
elif key == "k": # Vim up
|
|
158
|
+
highlighted = (highlighted - 1) % total
|
|
159
|
+
elif key == "j": # Vim down
|
|
160
|
+
highlighted = (highlighted + 1) % total
|
|
161
|
+
finally:
|
|
162
|
+
tty.setraw(fd)
|
|
163
|
+
tty.tcsetattr(fd, tty.TCSAFLUSH, old_settings)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _simple_select_hooks(hooks: list[tuple[str, str]]) -> list[str]:
|
|
167
|
+
"""Simple selection for non-interactive mode."""
|
|
168
|
+
print("Select hooks to create (press Enter to skip):")
|
|
169
|
+
print()
|
|
170
|
+
for i, (hook_name, desc) in enumerate(hooks, 1):
|
|
171
|
+
print(f" [{i}] {hook_name} - {desc}")
|
|
172
|
+
print()
|
|
173
|
+
|
|
174
|
+
try:
|
|
175
|
+
selections = []
|
|
176
|
+
while True:
|
|
177
|
+
line = input(f"Selection [{len(selections) + 1}/{len(hooks)}]: ").strip()
|
|
178
|
+
if not line:
|
|
179
|
+
break
|
|
180
|
+
try:
|
|
181
|
+
num = int(line)
|
|
182
|
+
if 1 <= num <= len(hooks):
|
|
183
|
+
selections.append(hooks[num - 1][0])
|
|
184
|
+
else:
|
|
185
|
+
print(f"Invalid number (1-{len(hooks)})")
|
|
186
|
+
except ValueError:
|
|
187
|
+
print("Invalid input (enter number)")
|
|
188
|
+
except EOFError:
|
|
189
|
+
pass
|
|
190
|
+
|
|
191
|
+
return selections
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def cmd_init_git_hooks(args: argparse.Namespace) -> int:
|
|
195
|
+
"""Initialize git hooks with interactive creation and enable/disable."""
|
|
196
|
+
vault = _load_vault(args.path)
|
|
197
|
+
hook_dir = vault.hidden_dir / "hooks"
|
|
198
|
+
git_dir = Path(vault.root) / ".git"
|
|
199
|
+
|
|
200
|
+
if not hook_dir.exists():
|
|
201
|
+
raise CLIError("no hooks directory found; run 'sv init hooks' first")
|
|
202
|
+
|
|
203
|
+
if git_dir.exists():
|
|
204
|
+
raise CLIError(f".git directory already exists at {git_dir}")
|
|
205
|
+
|
|
206
|
+
# Build list of available hooks (templates or existing scripts)
|
|
207
|
+
|
|
208
|
+
# Standard hook templates
|
|
209
|
+
templates = [
|
|
210
|
+
("pre-commit", "Validate before each commit"),
|
|
211
|
+
("pre-push", "Validate before push"),
|
|
212
|
+
("commit-msg", "Validate commit message"),
|
|
213
|
+
("pre-check", "Run checks before build"),
|
|
214
|
+
("post-check", "Run after check"),
|
|
215
|
+
("pre-build", "Setup before build"),
|
|
216
|
+
("post-build", "Setup after build"),
|
|
217
|
+
("post-commit", "Run after commit"),
|
|
218
|
+
]
|
|
219
|
+
|
|
220
|
+
# Existing custom hooks
|
|
221
|
+
custom_hooks = [
|
|
222
|
+
name
|
|
223
|
+
for name in hook_dir.glob("*.sh")
|
|
224
|
+
if (
|
|
225
|
+
name.name.startswith("pre-")
|
|
226
|
+
or name.name.startswith("post-")
|
|
227
|
+
or name.name.startswith("pre-commit")
|
|
228
|
+
or name.name.startswith("pre-push")
|
|
229
|
+
or name.name.startswith("commit-msg")
|
|
230
|
+
or name.name.startswith("post-commit")
|
|
231
|
+
or name.name.startswith("pre-check")
|
|
232
|
+
or name.name.startswith("post-check")
|
|
233
|
+
or name.name.startswith("pre-build")
|
|
234
|
+
or name.name.startswith("post-build")
|
|
235
|
+
)
|
|
236
|
+
]
|
|
237
|
+
|
|
238
|
+
# Combine templates and custom hooks
|
|
239
|
+
all_hooks = []
|
|
240
|
+
|
|
241
|
+
# Add templates that don't have custom versions
|
|
242
|
+
for name, desc in templates:
|
|
243
|
+
custom_names = [h for h in custom_hooks if h.name.startswith(name)]
|
|
244
|
+
if not custom_names:
|
|
245
|
+
all_hooks.append((name, desc))
|
|
246
|
+
|
|
247
|
+
# Add custom hooks
|
|
248
|
+
for hook_path in sorted(custom_hooks):
|
|
249
|
+
hook_name = hook_path.name
|
|
250
|
+
if hook_name not in [h[0] for h in all_hooks]:
|
|
251
|
+
desc = f"custom: {hook_path}"
|
|
252
|
+
all_hooks.append((hook_name, desc))
|
|
253
|
+
|
|
254
|
+
if not all_hooks:
|
|
255
|
+
raise CLIError("no hooks available to create")
|
|
256
|
+
|
|
257
|
+
if len(all_hooks) == 1:
|
|
258
|
+
print(f"Creating hook: {all_hooks[0][0]}")
|
|
259
|
+
print(f"Description: {all_hooks[0][1]}")
|
|
260
|
+
print()
|
|
261
|
+
selections = [all_hooks[0][0]]
|
|
262
|
+
else:
|
|
263
|
+
selections = _interactive_select_hooks(all_hooks)
|
|
264
|
+
|
|
265
|
+
if not selections:
|
|
266
|
+
print("No hooks selected")
|
|
267
|
+
return 0
|
|
268
|
+
|
|
269
|
+
# Copy hooks to .git/hooks/
|
|
270
|
+
print()
|
|
271
|
+
for hook_name in selections:
|
|
272
|
+
# For templates: hook_name = "pre-commit" -> source = "pre-commit.sh"
|
|
273
|
+
# For custom: hook_name = "pre-commit.sh" -> source = "pre-commit.sh"
|
|
274
|
+
source_name = (
|
|
275
|
+
hook_name + ".sh" if not hook_name.endswith(".sh") else hook_name
|
|
276
|
+
)
|
|
277
|
+
hook_path = hook_dir / source_name
|
|
278
|
+
|
|
279
|
+
# Create template if it doesn't exist
|
|
280
|
+
if not hook_path.exists():
|
|
281
|
+
hook_path.write_text(
|
|
282
|
+
f"""#!/bin/bash
|
|
283
|
+
# Git hook: {hook_name}
|
|
284
|
+
# Edit this file to customize the hook
|
|
285
|
+
|
|
286
|
+
echo "Running {hook_name}..."
|
|
287
|
+
exit 0
|
|
288
|
+
""",
|
|
289
|
+
encoding="utf-8",
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
# Ensure .git/hooks directory exists
|
|
293
|
+
git_hooks_dir = git_dir / "hooks"
|
|
294
|
+
git_hooks_dir.mkdir(parents=True, exist_ok=True)
|
|
295
|
+
|
|
296
|
+
git_hook_path = git_hooks_dir / (hook_name + ".sh")
|
|
297
|
+
git_hook_path.write_text(hook_path.read_text(encoding="utf-8"), encoding="utf-8")
|
|
298
|
+
git_hook_path.chmod(0o755)
|
|
299
|
+
print(f" ✓ Created {git_hook_path}")
|
|
300
|
+
|
|
301
|
+
# Write config
|
|
302
|
+
config_path = vault.hidden_dir / "git-hooks.toml"
|
|
303
|
+
config = f'enabled = {["" + h for h in selections]}'
|
|
304
|
+
config_path.write_text(config, encoding="utf-8")
|
|
305
|
+
|
|
306
|
+
print(f"\nInitialized git hooks")
|
|
307
|
+
print(f"Config saved to {config_path}")
|
|
308
|
+
return 0
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def cmd_install_hooks(args: argparse.Namespace) -> int:
|
|
312
|
+
"""Install selected git hooks from vault to .git/hooks/."""
|
|
313
|
+
vault = _load_vault(args.path)
|
|
314
|
+
hook_dir = vault.hidden_dir / "hooks"
|
|
315
|
+
git_dir = Path(vault.root) / ".git"
|
|
316
|
+
|
|
317
|
+
if not hook_dir.exists():
|
|
318
|
+
raise CLIError("no hooks directory found; run 'sv init hooks' first")
|
|
319
|
+
|
|
320
|
+
if not git_dir.exists():
|
|
321
|
+
raise CLIError(f".git directory not found at {git_dir}; initialize git first")
|
|
322
|
+
|
|
323
|
+
# Load config
|
|
324
|
+
config_path = vault.hidden_dir / "git-hooks.toml"
|
|
325
|
+
if not config_path.exists():
|
|
326
|
+
raise CLIError("hooks not configured; run 'sv init hooks' first")
|
|
327
|
+
|
|
328
|
+
config_text = config_path.read_text(encoding="utf-8")
|
|
329
|
+
try:
|
|
330
|
+
import tomllib
|
|
331
|
+
except ImportError:
|
|
332
|
+
import toml as tomllib
|
|
333
|
+
|
|
334
|
+
config = tomllib.loads(config_text)
|
|
335
|
+
enabled = config.get("enabled", [])
|
|
336
|
+
|
|
337
|
+
if not enabled:
|
|
338
|
+
raise CLIError("no hooks enabled; run 'sv init hooks' first")
|
|
339
|
+
|
|
340
|
+
# Ensure .git/hooks directory exists
|
|
341
|
+
git_hooks_dir = git_dir / "hooks"
|
|
342
|
+
git_hooks_dir.mkdir(parents=True, exist_ok=True)
|
|
343
|
+
|
|
344
|
+
installed = 0
|
|
345
|
+
for hook_name in enabled:
|
|
346
|
+
source_name = hook_name + ".sh" if not hook_name.endswith(".sh") else hook_name
|
|
347
|
+
source_path = hook_dir / source_name
|
|
348
|
+
|
|
349
|
+
if not source_path.exists():
|
|
350
|
+
print(f"Warning: hook {hook_name!r} not found")
|
|
351
|
+
continue
|
|
352
|
+
|
|
353
|
+
dest_path = git_hooks_dir / (hook_name + ".sh")
|
|
354
|
+
dest_path.write_text(source_path.read_text(encoding="utf-8"), encoding="utf-8")
|
|
355
|
+
dest_path.chmod(0o755)
|
|
356
|
+
print(f" ✓ {hook_name}")
|
|
357
|
+
installed += 1
|
|
358
|
+
|
|
359
|
+
print(f"\nInstalled {installed} hook(s) to {git_dir}")
|
|
360
|
+
return 0
|
|
361
|
+
|
|
362
|
+
|
|
75
363
|
def _target_json(target: Script | Orchestration) -> dict:
|
|
76
364
|
if isinstance(target, Orchestration):
|
|
77
365
|
return {
|
|
@@ -195,7 +483,13 @@ def cmd_run(args: argparse.Namespace) -> int:
|
|
|
195
483
|
|
|
196
484
|
# Parse global flags
|
|
197
485
|
global_flags, script_args = _parse_args(run_args, "--")
|
|
198
|
-
return run_target(
|
|
486
|
+
return run_target(
|
|
487
|
+
vault,
|
|
488
|
+
script_name,
|
|
489
|
+
script_args,
|
|
490
|
+
global_flags=global_flags,
|
|
491
|
+
source_vault=vault._nested_vaults[name],
|
|
492
|
+
)
|
|
199
493
|
|
|
200
494
|
# Root vault: single arg only
|
|
201
495
|
target = vault.get(name)
|
|
@@ -214,7 +508,13 @@ def cmd_run(args: argparse.Namespace) -> int:
|
|
|
214
508
|
script_args = run_args
|
|
215
509
|
global_flags = []
|
|
216
510
|
try:
|
|
217
|
-
return run_target(
|
|
511
|
+
return run_target(
|
|
512
|
+
vault,
|
|
513
|
+
name,
|
|
514
|
+
script_args,
|
|
515
|
+
global_flags=global_flags,
|
|
516
|
+
source_vault=vault.owner_of(name),
|
|
517
|
+
)
|
|
218
518
|
except OSError as exc:
|
|
219
519
|
detail = exc.strerror or str(exc)
|
|
220
520
|
raise CLIError(f"failed to run {args.name!r}: {detail}")
|
|
@@ -495,6 +795,30 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
495
795
|
)
|
|
496
796
|
init.set_defaults(func=cmd_init)
|
|
497
797
|
|
|
798
|
+
init_hooks = sub.add_parser(
|
|
799
|
+
"init-hooks",
|
|
800
|
+
help="initialize git hooks with interactive selection",
|
|
801
|
+
)
|
|
802
|
+
init_hooks.add_argument(
|
|
803
|
+
"--path",
|
|
804
|
+
metavar="PATH",
|
|
805
|
+
default=None,
|
|
806
|
+
help="path to .sv directory (default: auto-detect root .sv in current or parent directories)",
|
|
807
|
+
)
|
|
808
|
+
init_hooks.set_defaults(func=cmd_init_git_hooks)
|
|
809
|
+
|
|
810
|
+
install_hooks = sub.add_parser(
|
|
811
|
+
"install-hooks",
|
|
812
|
+
help="install selected git hooks from vault to .git/hooks/",
|
|
813
|
+
)
|
|
814
|
+
install_hooks.add_argument(
|
|
815
|
+
"--path",
|
|
816
|
+
metavar="PATH",
|
|
817
|
+
default=None,
|
|
818
|
+
help="path to .sv directory (default: auto-detect root .sv in current or parent directories)",
|
|
819
|
+
)
|
|
820
|
+
install_hooks.set_defaults(func=cmd_install_hooks)
|
|
821
|
+
|
|
498
822
|
lst = sub.add_parser(
|
|
499
823
|
"list", help="list scripts and orchestrations in the vault"
|
|
500
824
|
)
|
|
@@ -116,25 +116,30 @@ def _step_label(step) -> str:
|
|
|
116
116
|
return _truncate_prefix(step.sh or "", _LABEL_MAX)
|
|
117
117
|
|
|
118
118
|
|
|
119
|
-
def build_env(
|
|
119
|
+
def build_env(
|
|
120
|
+
vault: Vault,
|
|
121
|
+
name: str | None = None,
|
|
122
|
+
source_vault: Vault | None = None,
|
|
123
|
+
) -> dict[str, str]:
|
|
120
124
|
"""Build the environment for a script: global .env, script .env, real env, injected vars."""
|
|
125
|
+
ctx = source_vault or vault
|
|
121
126
|
env: dict[str, str] = {}
|
|
122
|
-
if
|
|
123
|
-
env.update(parse_env_file(
|
|
124
|
-
|
|
127
|
+
if ctx.global_env.is_file():
|
|
128
|
+
env.update(parse_env_file(ctx.global_env))
|
|
129
|
+
|
|
125
130
|
# Load environment files from project root recursively matching environment
|
|
126
|
-
for env_path in
|
|
131
|
+
for env_path in ctx._env_paths:
|
|
127
132
|
if env_path.is_file():
|
|
128
133
|
env.update(parse_env_file(env_path))
|
|
129
|
-
|
|
134
|
+
|
|
130
135
|
if name is not None:
|
|
131
|
-
script_env =
|
|
136
|
+
script_env = ctx.scripts_dir / f"{name}.env"
|
|
132
137
|
if script_env.is_file():
|
|
133
138
|
env.update(parse_env_file(script_env))
|
|
134
|
-
|
|
139
|
+
|
|
135
140
|
env.update(dict(os.environ))
|
|
136
|
-
env["SV_ROOT"] = str(
|
|
137
|
-
env["SV_DIR"] = str(
|
|
141
|
+
env["SV_ROOT"] = str(ctx.root)
|
|
142
|
+
env["SV_DIR"] = str(ctx.hidden_dir)
|
|
138
143
|
env["SV_CWD"] = os.getcwd()
|
|
139
144
|
if name is not None:
|
|
140
145
|
env["SV_NAME"] = name
|
|
@@ -184,17 +189,24 @@ def _run_child(argv: list[str], env: dict[str, str], cwd, out, progress):
|
|
|
184
189
|
|
|
185
190
|
|
|
186
191
|
def run_script(
|
|
187
|
-
vault: Vault,
|
|
192
|
+
vault: Vault,
|
|
193
|
+
script: Script,
|
|
194
|
+
args: list[str],
|
|
195
|
+
global_flags: list[str] | None = None,
|
|
196
|
+
out=None,
|
|
197
|
+
progress=None,
|
|
198
|
+
source_vault: Vault | None = None,
|
|
188
199
|
) -> int:
|
|
189
|
-
env = build_env(vault, script.name)
|
|
200
|
+
env = build_env(vault, script.name, source_vault=source_vault)
|
|
190
201
|
argv = _script_argv(vault, script, args)
|
|
191
202
|
if global_flags is not None:
|
|
192
203
|
argv = global_flags + argv
|
|
193
204
|
if progress is not None:
|
|
194
205
|
return _run_child(argv, env, vault.root, out, progress)
|
|
206
|
+
cwd = source_vault.root if source_vault else vault.root
|
|
195
207
|
proc = subprocess.run(
|
|
196
208
|
argv,
|
|
197
|
-
cwd=str(
|
|
209
|
+
cwd=str(cwd),
|
|
198
210
|
env=env,
|
|
199
211
|
text=True,
|
|
200
212
|
stdout=out,
|
|
@@ -205,7 +217,11 @@ def run_script(
|
|
|
205
217
|
|
|
206
218
|
|
|
207
219
|
def run_target(
|
|
208
|
-
vault: Vault,
|
|
220
|
+
vault: Vault,
|
|
221
|
+
name: str,
|
|
222
|
+
args: list[str],
|
|
223
|
+
global_flags: list[str] | None = None,
|
|
224
|
+
source_vault: Vault | None = None,
|
|
209
225
|
) -> int:
|
|
210
226
|
target = vault.get(name)
|
|
211
227
|
if target is None:
|
|
@@ -213,21 +229,29 @@ def run_target(
|
|
|
213
229
|
if isinstance(target, Orchestration):
|
|
214
230
|
progress = _StepProgress(sys.stdout)
|
|
215
231
|
return run_orchestration(
|
|
216
|
-
vault, target, progress=progress, show_progress=True, global_flags=global_flags
|
|
232
|
+
vault, target, progress=progress, show_progress=True, global_flags=global_flags, source_vault=source_vault
|
|
217
233
|
)
|
|
218
|
-
return run_script(vault, target, args, global_flags=global_flags)
|
|
234
|
+
return run_script(vault, target, args, global_flags=global_flags, source_vault=source_vault)
|
|
219
235
|
|
|
220
236
|
|
|
221
|
-
def _run_sh_step(
|
|
222
|
-
|
|
237
|
+
def _run_sh_step(
|
|
238
|
+
vault: Vault,
|
|
239
|
+
command: str,
|
|
240
|
+
out=None,
|
|
241
|
+
progress=None,
|
|
242
|
+
global_flags: list[str] | None = None,
|
|
243
|
+
source_vault: Vault | None = None,
|
|
244
|
+
) -> int:
|
|
245
|
+
env = build_env(vault, None, source_vault=source_vault)
|
|
223
246
|
argv = [vault.shell, "-c", command]
|
|
224
247
|
if global_flags is not None:
|
|
225
248
|
argv = global_flags + argv
|
|
249
|
+
cwd = source_vault.root if source_vault else vault.root
|
|
226
250
|
if progress is not None:
|
|
227
|
-
return _run_child(argv, env,
|
|
251
|
+
return _run_child(argv, env, cwd, out, progress)
|
|
228
252
|
proc = subprocess.run(
|
|
229
253
|
argv,
|
|
230
|
-
cwd=str(
|
|
254
|
+
cwd=str(cwd),
|
|
231
255
|
env=env,
|
|
232
256
|
text=True,
|
|
233
257
|
stdout=out,
|
|
@@ -237,17 +261,39 @@ def _run_sh_step(vault: Vault, command: str, out=None, progress=None, global_fla
|
|
|
237
261
|
return proc.returncode
|
|
238
262
|
|
|
239
263
|
|
|
240
|
-
def _run_job(
|
|
264
|
+
def _run_job(
|
|
265
|
+
vault: Vault,
|
|
266
|
+
name: str,
|
|
267
|
+
out,
|
|
268
|
+
progress=None,
|
|
269
|
+
global_flags: list[str] | None = None,
|
|
270
|
+
source_vault: Vault | None = None,
|
|
271
|
+
) -> int:
|
|
241
272
|
"""Run one target, sending its stdout to `out` (a text file object)."""
|
|
242
273
|
target = vault.get(name)
|
|
243
274
|
if isinstance(target, Orchestration):
|
|
244
275
|
return run_orchestration(
|
|
245
|
-
vault,
|
|
276
|
+
vault,
|
|
277
|
+
target,
|
|
278
|
+
out=out,
|
|
279
|
+
progress=progress,
|
|
280
|
+
show_progress=False,
|
|
281
|
+
global_flags=global_flags,
|
|
282
|
+
source_vault=source_vault,
|
|
246
283
|
)
|
|
247
|
-
return run_script(
|
|
284
|
+
return run_script(
|
|
285
|
+
vault, target, [], global_flags=global_flags, out=out, progress=progress, source_vault=source_vault
|
|
286
|
+
)
|
|
248
287
|
|
|
249
288
|
|
|
250
|
-
def run_parallel(
|
|
289
|
+
def run_parallel(
|
|
290
|
+
vault: Vault,
|
|
291
|
+
names: list[str],
|
|
292
|
+
out=None,
|
|
293
|
+
progress=None,
|
|
294
|
+
global_flags: list[str] | None = None,
|
|
295
|
+
source_vault: Vault | None = None,
|
|
296
|
+
) -> int:
|
|
251
297
|
if out is None:
|
|
252
298
|
out = sys.stdout
|
|
253
299
|
readers: list[threading.Thread] = []
|
|
@@ -268,13 +314,14 @@ def run_parallel(vault: Vault, names: list[str], out=None, progress=None, global
|
|
|
268
314
|
for name in names:
|
|
269
315
|
target = vault.get(name)
|
|
270
316
|
if isinstance(target, Script):
|
|
271
|
-
env = build_env(vault, target.name)
|
|
317
|
+
env = build_env(vault, target.name, source_vault=source_vault)
|
|
272
318
|
argv = _script_argv(vault, target, [])
|
|
273
319
|
if global_flags is not None:
|
|
274
320
|
argv = global_flags + argv
|
|
321
|
+
cwd = source_vault.root if source_vault else vault.root
|
|
275
322
|
proc = subprocess.Popen(
|
|
276
323
|
argv,
|
|
277
|
-
cwd=str(
|
|
324
|
+
cwd=str(cwd),
|
|
278
325
|
env=env,
|
|
279
326
|
stdout=subprocess.PIPE,
|
|
280
327
|
stderr=subprocess.STDOUT,
|
|
@@ -290,9 +337,9 @@ def run_parallel(vault: Vault, names: list[str], out=None, progress=None, global
|
|
|
290
337
|
job_out = os.fdopen(wfd, "w", encoding="utf-8", errors="replace")
|
|
291
338
|
codes = []
|
|
292
339
|
|
|
293
|
-
def job(n=name, o=job_out, c=codes, p=progress, g=global_flags) -> None:
|
|
340
|
+
def job(n=name, o=job_out, c=codes, p=progress, g=global_flags, v=vault, s=source_vault) -> None:
|
|
294
341
|
try:
|
|
295
|
-
c.append(_run_job(vault, n, out=o, progress=p, global_flags=g))
|
|
342
|
+
c.append(_run_job(vault, n, out=o, progress=p, global_flags=g, source_vault=s))
|
|
296
343
|
except Exception as exc:
|
|
297
344
|
print(f"sv: {n}: {exc}", file=sys.stderr, flush=True)
|
|
298
345
|
c.append(1)
|
|
@@ -336,6 +383,7 @@ def run_orchestration(
|
|
|
336
383
|
progress=None,
|
|
337
384
|
show_progress: bool = False,
|
|
338
385
|
global_flags: list[str] | None = None,
|
|
386
|
+
source_vault: Vault | None = None,
|
|
339
387
|
) -> int:
|
|
340
388
|
total = len(orch.steps)
|
|
341
389
|
for index, step in enumerate(orch.steps, start=1):
|
|
@@ -352,16 +400,30 @@ def run_orchestration(
|
|
|
352
400
|
progress=progress,
|
|
353
401
|
show_progress=False,
|
|
354
402
|
global_flags=global_flags,
|
|
403
|
+
source_vault=source_vault,
|
|
355
404
|
)
|
|
356
405
|
else:
|
|
357
406
|
code = run_script(
|
|
358
|
-
vault,
|
|
407
|
+
vault,
|
|
408
|
+
target,
|
|
409
|
+
step.run_args,
|
|
410
|
+
global_flags=global_flags,
|
|
411
|
+
out=out,
|
|
412
|
+
progress=progress,
|
|
413
|
+
source_vault=source_vault,
|
|
359
414
|
)
|
|
360
415
|
elif step.kind == "sh":
|
|
361
|
-
code = _run_sh_step(
|
|
416
|
+
code = _run_sh_step(
|
|
417
|
+
vault, step.sh, out=out, progress=progress, global_flags=global_flags, source_vault=source_vault
|
|
418
|
+
)
|
|
362
419
|
else:
|
|
363
420
|
code = run_parallel(
|
|
364
|
-
vault,
|
|
421
|
+
vault,
|
|
422
|
+
step.parallel,
|
|
423
|
+
out=out,
|
|
424
|
+
progress=progress,
|
|
425
|
+
global_flags=global_flags,
|
|
426
|
+
source_vault=source_vault,
|
|
365
427
|
)
|
|
366
428
|
finally:
|
|
367
429
|
if progress is not None and show_progress:
|
|
@@ -271,6 +271,15 @@ class Vault:
|
|
|
271
271
|
|
|
272
272
|
return None
|
|
273
273
|
|
|
274
|
+
def owner_of(self, name: str) -> "Vault | None":
|
|
275
|
+
"""Return the nested vault that owns `name`, or None if this vault owns it."""
|
|
276
|
+
if name in self.scripts or name in self.orchestrations:
|
|
277
|
+
return None
|
|
278
|
+
for nested_vault in self._nested_vaults.values():
|
|
279
|
+
if name in nested_vault.scripts or name in nested_vault.orchestrations:
|
|
280
|
+
return nested_vault
|
|
281
|
+
return None
|
|
282
|
+
|
|
274
283
|
def get_script_from_nested(self, vault_name: str, script_name: str) -> Script | None:
|
|
275
284
|
"""Get a script from a nested vault."""
|
|
276
285
|
if vault_name not in self._nested_vaults:
|
|
@@ -312,7 +321,12 @@ def _require_str(mapping: dict, key: str, context: str) -> str:
|
|
|
312
321
|
return value
|
|
313
322
|
|
|
314
323
|
|
|
315
|
-
def _parse_steps(
|
|
324
|
+
def _parse_steps(
|
|
325
|
+
raw_steps: object,
|
|
326
|
+
known_names: set[str],
|
|
327
|
+
where: str,
|
|
328
|
+
nested_vaults: list[Vault] | None = None,
|
|
329
|
+
) -> list[Step]:
|
|
316
330
|
if not isinstance(raw_steps, list) or not raw_steps:
|
|
317
331
|
raise VaultError(f"{where}: 'steps' must be a non-empty list")
|
|
318
332
|
steps: list[Step] = []
|
|
@@ -341,9 +355,21 @@ def _parse_steps(raw_steps: object, known_names: set[str], where: str) -> list[S
|
|
|
341
355
|
raise VaultError(f"{where}.steps[{i}]: 'run' must be a non-empty string")
|
|
342
356
|
_validate_name(target)
|
|
343
357
|
if target not in known_names:
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
358
|
+
# Check nested vaults for the target
|
|
359
|
+
if nested_vaults:
|
|
360
|
+
found = False
|
|
361
|
+
for nested_vault in nested_vaults:
|
|
362
|
+
if target in nested_vault.scripts or target in nested_vault.orchestrations:
|
|
363
|
+
found = True
|
|
364
|
+
break
|
|
365
|
+
if not found:
|
|
366
|
+
raise VaultError(
|
|
367
|
+
f"{where}.steps[{i}]: unknown script or orchestration {target!r}"
|
|
368
|
+
)
|
|
369
|
+
else:
|
|
370
|
+
raise VaultError(
|
|
371
|
+
f"{where}.steps[{i}]: unknown script or orchestration {target!r}"
|
|
372
|
+
)
|
|
347
373
|
run_name = target
|
|
348
374
|
args = raw.get("args", [])
|
|
349
375
|
if not isinstance(args, list) or not all(isinstance(a, str) for a in args):
|
|
@@ -358,7 +384,21 @@ def _parse_steps(raw_steps: object, known_names: set[str], where: str) -> list[S
|
|
|
358
384
|
for n in names:
|
|
359
385
|
_validate_name(n)
|
|
360
386
|
if n not in known_names:
|
|
361
|
-
|
|
387
|
+
# Check nested vaults for the target
|
|
388
|
+
if nested_vaults:
|
|
389
|
+
found = False
|
|
390
|
+
for nested_vault in nested_vaults:
|
|
391
|
+
if n in nested_vault.scripts or n in nested_vault.orchestrations:
|
|
392
|
+
found = True
|
|
393
|
+
break
|
|
394
|
+
if not found:
|
|
395
|
+
raise VaultError(
|
|
396
|
+
f"{where}.steps[{i}]: unknown script or orchestration {n!r}"
|
|
397
|
+
)
|
|
398
|
+
else:
|
|
399
|
+
raise VaultError(
|
|
400
|
+
f"{where}.steps[{i}]: unknown script or orchestration {n!r}"
|
|
401
|
+
)
|
|
362
402
|
parallel = list(names)
|
|
363
403
|
else:
|
|
364
404
|
sh = _require_str(raw, "sh", f"{where}.steps[{i}]")
|
|
@@ -456,8 +496,8 @@ def load_vault(root: Path) -> Vault:
|
|
|
456
496
|
env_paths = _load_env_paths(vault)
|
|
457
497
|
vault._env_paths = env_paths
|
|
458
498
|
|
|
459
|
-
|
|
460
|
-
|
|
499
|
+
# Steps may reference both scripts and orchestrations, in any order of
|
|
500
|
+
# definition; cycles are rejected afterwards, before anything runs.
|
|
461
501
|
known_names = set(vault.scripts)
|
|
462
502
|
orchestrations: dict[str, Orchestration] = {}
|
|
463
503
|
orch_section = data.get("orchestrate", {})
|
|
@@ -470,6 +510,14 @@ def load_vault(root: Path) -> Vault:
|
|
|
470
510
|
if name in vault.scripts:
|
|
471
511
|
raise VaultError(f"name {name!r} is both a script and an orchestration")
|
|
472
512
|
known_names |= set(orch_section)
|
|
513
|
+
|
|
514
|
+
# Collect nested vaults for orchestration step resolution
|
|
515
|
+
nested_vaults = [
|
|
516
|
+
v
|
|
517
|
+
for v in vault._nested_vaults.values()
|
|
518
|
+
if isinstance(v, Vault) and not isinstance(v, type(vault))
|
|
519
|
+
]
|
|
520
|
+
|
|
473
521
|
for name, table in orch_section.items():
|
|
474
522
|
description = table.get("description", "")
|
|
475
523
|
if not isinstance(description, str):
|
|
@@ -478,6 +526,7 @@ def load_vault(root: Path) -> Vault:
|
|
|
478
526
|
table.get("steps"),
|
|
479
527
|
known_names=known_names,
|
|
480
528
|
where=f"[orchestrate.{name}]",
|
|
529
|
+
nested_vaults=nested_vaults,
|
|
481
530
|
)
|
|
482
531
|
orchestrations[name] = Orchestration(
|
|
483
532
|
name=name, steps=steps, description=description
|
|
@@ -4,11 +4,14 @@ import os
|
|
|
4
4
|
import io
|
|
5
5
|
import subprocess
|
|
6
6
|
import sys
|
|
7
|
+
from pathlib import Path
|
|
7
8
|
|
|
8
9
|
import pytest
|
|
9
10
|
|
|
10
11
|
from scriptvault.cli import main
|
|
11
12
|
|
|
13
|
+
SRC = str(Path(__file__).resolve().parents[1] / "src")
|
|
14
|
+
|
|
12
15
|
|
|
13
16
|
def init(project, monkeypatch):
|
|
14
17
|
monkeypatch.chdir(project)
|
|
@@ -40,7 +43,7 @@ def test_add_script_is_listed_and_runnable(project, capsys, monkeypatch):
|
|
|
40
43
|
cwd=str(project),
|
|
41
44
|
capture_output=True,
|
|
42
45
|
text=True,
|
|
43
|
-
env={**os.environ, "PYTHONPATH":
|
|
46
|
+
env={**os.environ, "PYTHONPATH": SRC},
|
|
44
47
|
)
|
|
45
48
|
assert result.returncode == 0
|
|
46
49
|
assert result.stdout == 'Hello from hello\n'
|
|
@@ -4,11 +4,14 @@ import shutil
|
|
|
4
4
|
import os
|
|
5
5
|
import subprocess
|
|
6
6
|
import sys
|
|
7
|
+
from pathlib import Path
|
|
7
8
|
|
|
8
9
|
import pytest
|
|
9
10
|
|
|
10
11
|
from scriptvault.cli import main
|
|
11
12
|
|
|
13
|
+
SRC = str(Path(__file__).resolve().parents[1] / "src")
|
|
14
|
+
|
|
12
15
|
EXTRA_TOML = """
|
|
13
16
|
[scripts.hello]
|
|
14
17
|
cmd = "echo hello"
|
|
@@ -25,7 +28,7 @@ def make_vault(project):
|
|
|
25
28
|
|
|
26
29
|
def sv(*argv, cwd):
|
|
27
30
|
env = os.environ.copy()
|
|
28
|
-
env["PYTHONPATH"] =
|
|
31
|
+
env["PYTHONPATH"] = SRC
|
|
29
32
|
return subprocess.run(
|
|
30
33
|
[sys.executable, "-m", "scriptvault", *map(str, argv)],
|
|
31
34
|
cwd=str(cwd),
|
|
@@ -70,7 +73,7 @@ def test_completion_bash_syntax_and_targets(project):
|
|
|
70
73
|
cwd=str(project),
|
|
71
74
|
capture_output=True,
|
|
72
75
|
text=True,
|
|
73
|
-
env={**os.environ, "PYTHONPATH":
|
|
76
|
+
env={**os.environ, "PYTHONPATH": SRC},
|
|
74
77
|
)
|
|
75
78
|
assert run.returncode == 0, run.stderr
|
|
76
79
|
assert "hello" in run.stdout.split()
|
|
@@ -91,7 +94,7 @@ def test_completion_bash_completes_commands(project):
|
|
|
91
94
|
cwd=str(project),
|
|
92
95
|
capture_output=True,
|
|
93
96
|
text=True,
|
|
94
|
-
env={**os.environ, "PYTHONPATH":
|
|
97
|
+
env={**os.environ, "PYTHONPATH": SRC},
|
|
95
98
|
)
|
|
96
99
|
assert run.returncode == 0, run.stderr
|
|
97
100
|
assert "run" in run.stdout.split()
|
|
@@ -150,7 +153,7 @@ def test_completion_bash_edit_completes_kind(project):
|
|
|
150
153
|
cwd=str(project),
|
|
151
154
|
capture_output=True,
|
|
152
155
|
text=True,
|
|
153
|
-
env={**os.environ, "PYTHONPATH":
|
|
156
|
+
env={**os.environ, "PYTHONPATH": SRC},
|
|
154
157
|
)
|
|
155
158
|
assert run.returncode == 0, run.stderr
|
|
156
159
|
assert "env" in run.stdout.split()
|
|
@@ -4,11 +4,12 @@ import os
|
|
|
4
4
|
import io
|
|
5
5
|
import subprocess
|
|
6
6
|
import sys
|
|
7
|
+
from pathlib import Path
|
|
7
8
|
|
|
8
9
|
from scriptvault.cli import main
|
|
9
10
|
from scriptvault.run import _StepProgress
|
|
10
11
|
|
|
11
|
-
SRC = "
|
|
12
|
+
SRC = str(Path(__file__).resolve().parents[1] / "src")
|
|
12
13
|
|
|
13
14
|
EXTRA_TOML = """
|
|
14
15
|
[scripts.hello]
|
|
@@ -407,3 +408,55 @@ steps = [
|
|
|
407
408
|
result = sv("check", cwd=project)
|
|
408
409
|
assert result.returncode == 1
|
|
409
410
|
assert "unknown key(s)" in result.stderr
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
NESTED_TOML = """
|
|
414
|
+
[scripts.ctx]
|
|
415
|
+
cmd = 'echo "svroot=$SV_ROOT greeting=$GREETING"'
|
|
416
|
+
"""
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def make_nested_root(root):
|
|
420
|
+
assert main(["init", str(root)]) == 0
|
|
421
|
+
proj = root / "proj"
|
|
422
|
+
proj.mkdir()
|
|
423
|
+
assert main(["init", str(proj)]) == 0
|
|
424
|
+
hidden = proj / ".sv"
|
|
425
|
+
(hidden / "vault.toml").write_text(
|
|
426
|
+
(hidden / "vault.toml").read_text(encoding="utf-8") + NESTED_TOML
|
|
427
|
+
)
|
|
428
|
+
(hidden / ".env").write_text("GREETING=nested-hello\n")
|
|
429
|
+
rtoml = root / ".sv" / "vault.toml"
|
|
430
|
+
rtoml.write_text(
|
|
431
|
+
rtoml.read_text(encoding="utf-8") + '\n[include.proj]\npath = "proj"\n'
|
|
432
|
+
)
|
|
433
|
+
return root, proj
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
def test_run_nested_bare_name_uses_owner_context(project):
|
|
437
|
+
root, proj = make_nested_root(project)
|
|
438
|
+
result = sv("run", "ctx", cwd=root)
|
|
439
|
+
assert result.returncode == 0
|
|
440
|
+
assert f"svroot={proj}" in result.stdout
|
|
441
|
+
assert "greeting=nested-hello" in result.stdout
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def test_run_nested_explicit_uses_owner_context(project):
|
|
445
|
+
root, proj = make_nested_root(project)
|
|
446
|
+
result = sv("run", "proj", "ctx", cwd=root)
|
|
447
|
+
assert result.returncode == 0
|
|
448
|
+
assert f"svroot={proj}" in result.stdout
|
|
449
|
+
assert "greeting=nested-hello" in result.stdout
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def test_run_root_owned_target_keeps_root_context(project):
|
|
453
|
+
root, proj = make_nested_root(project)
|
|
454
|
+
rhidden = root / ".sv"
|
|
455
|
+
(rhidden / "vault.toml").write_text(
|
|
456
|
+
(rhidden / "vault.toml").read_text(encoding="utf-8")
|
|
457
|
+
+ '\n[scripts.rootctx]\ncmd = \'echo "svroot=$SV_ROOT greeting=${GREETING:-none}"\'\n'
|
|
458
|
+
)
|
|
459
|
+
result = sv("run", "rootctx", cwd=root)
|
|
460
|
+
assert result.returncode == 0
|
|
461
|
+
assert f"svroot={root}" in result.stdout
|
|
462
|
+
assert "greeting=none" in result.stdout
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|