scriptvault 0.0.1__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.
scriptvault/run.py ADDED
@@ -0,0 +1,354 @@
1
+ """Execution engine for scripts and orchestrations."""
2
+
3
+ import os
4
+ import subprocess
5
+ import sys
6
+ import threading
7
+ from pathlib import Path
8
+
9
+ from .envfile import parse_env_file
10
+ from .term import CYAN, Palette
11
+ from .vault import Orchestration, Script, Vault
12
+
13
+ _PREFIX_MAX = 64
14
+ _LABEL_MAX = 60
15
+ _SPIN_FRAMES = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
16
+ _SPIN_INTERVAL = 0.1
17
+
18
+
19
+ def _truncate_prefix(text: str, limit: int) -> str:
20
+ if len(text) <= limit:
21
+ return text
22
+ return text[: limit - 1] + "…"
23
+
24
+
25
+ class _StepProgress:
26
+ def __init__(self, out):
27
+ self._out = out
28
+ self._tty = False
29
+ if hasattr(out, "isatty"):
30
+ try:
31
+ self._tty = bool(out.isatty())
32
+ except (OSError, ValueError):
33
+ self._tty = False
34
+ self._palette = Palette(out)
35
+ self._lock = threading.Lock()
36
+ self._header = ""
37
+ self._thread = None
38
+ self._stop = None
39
+ self._line_written = False
40
+
41
+ def begin(self, index: int, total: int, label: str) -> None:
42
+ plain = f"==> [{index}/{total}] {label}"
43
+ if self._palette.enabled:
44
+ self._header = " ".join(
45
+ [
46
+ self._palette.paint("==>", bold=True, fg=CYAN),
47
+ self._palette.paint(f"[{index}/{total}]", dim=True),
48
+ self._palette.paint(label, bold=True),
49
+ ]
50
+ )
51
+ else:
52
+ self._header = plain
53
+ if self._tty:
54
+ self._line_written = False
55
+ stop = threading.Event()
56
+ self._stop = stop
57
+ self._thread = threading.Thread(
58
+ target=self._spin, args=(stop,), daemon=True
59
+ )
60
+ self._thread.start()
61
+ else:
62
+ self._out.write(self._header + "\n")
63
+ self._out.flush()
64
+
65
+ def _spin(self, stop: threading.Event) -> None:
66
+ frame = 0
67
+ while not stop.wait(_SPIN_INTERVAL):
68
+ with self._lock:
69
+ self._out.write(
70
+ "\r"
71
+ + self._header
72
+ + " "
73
+ + self._palette.paint(
74
+ _SPIN_FRAMES[frame % len(_SPIN_FRAMES)], fg=CYAN
75
+ )
76
+ )
77
+ self._out.flush()
78
+ frame += 1
79
+
80
+ def _finish_line(self) -> None:
81
+ with self._lock:
82
+ if self._line_written:
83
+ return
84
+ self._line_written = True
85
+ if self._stop is not None:
86
+ self._stop.set()
87
+ thread = self._thread
88
+ self._thread = None
89
+ self._stop = None
90
+ else:
91
+ thread = None
92
+ if thread is not None:
93
+ thread.join()
94
+ self._out.write("\r" + self._header + " \n")
95
+ self._out.flush()
96
+
97
+ def note_output(self) -> None:
98
+ if self._tty:
99
+ self._finish_line()
100
+
101
+ def end(self) -> None:
102
+ if self._tty:
103
+ self._finish_line()
104
+
105
+
106
+ def _step_label(step) -> str:
107
+ if step.run_name is not None:
108
+ text = step.run_name
109
+ if step.run_args:
110
+ text += " " + " ".join(repr(a) for a in step.run_args)
111
+ return _truncate_prefix(text, _LABEL_MAX)
112
+ if step.parallel is not None:
113
+ return _truncate_prefix(
114
+ "parallel: " + ", ".join(step.parallel), _LABEL_MAX
115
+ )
116
+ return _truncate_prefix(step.sh or "", _LABEL_MAX)
117
+
118
+
119
+ def build_env(vault: Vault, name: str | None = None) -> dict[str, str]:
120
+ """Build the environment for a script: global .env, script .env, real env, injected vars."""
121
+ env: dict[str, str] = {}
122
+ if vault.global_env.is_file():
123
+ env.update(parse_env_file(vault.global_env))
124
+ if name is not None:
125
+ script_env = vault.scripts_dir / f"{name}.env"
126
+ if script_env.is_file():
127
+ env.update(parse_env_file(script_env))
128
+ env.update(dict(os.environ))
129
+ env["SV_ROOT"] = str(vault.root)
130
+ env["SV_DIR"] = str(vault.hidden_dir)
131
+ env["SV_CWD"] = os.getcwd()
132
+ if name is not None:
133
+ env["SV_NAME"] = name
134
+ return env
135
+
136
+
137
+ def _script_argv(vault: Vault, script: Script, args: list[str]) -> list[str]:
138
+ if script.path is not None:
139
+ if os.access(script.path, os.X_OK):
140
+ return [str(script.path), *args]
141
+ return [vault.shell, str(script.path), *args]
142
+ return [vault.shell, "-c", script.cmd, script.name, *args]
143
+
144
+
145
+ def _run_child(argv: list[str], env: dict[str, str], cwd, out, progress):
146
+ if out is None:
147
+ out = sys.stdout
148
+ proc = subprocess.Popen(
149
+ argv,
150
+ cwd=str(cwd),
151
+ env=env,
152
+ stdout=subprocess.PIPE,
153
+ stderr=subprocess.STDOUT,
154
+ text=True,
155
+ encoding="utf-8",
156
+ errors="replace",
157
+ )
158
+ stdout = proc.stdout
159
+ if stdout is None:
160
+ proc.wait()
161
+ return proc.returncode
162
+ wrote = False
163
+ ends_with_newline = False
164
+ for line in stdout:
165
+ if progress is not None:
166
+ progress.note_output()
167
+ out.write(line)
168
+ out.flush()
169
+ wrote = True
170
+ ends_with_newline = line.endswith("\n")
171
+ stdout.close()
172
+ rc = proc.wait()
173
+ if wrote and not ends_with_newline:
174
+ out.write("\n")
175
+ out.flush()
176
+ return rc
177
+
178
+
179
+ def run_script(
180
+ vault: Vault, script: Script, args: list[str], out=None, progress=None
181
+ ) -> int:
182
+ env = build_env(vault, script.name)
183
+ argv = _script_argv(vault, script, args)
184
+ if progress is not None:
185
+ return _run_child(argv, env, vault.root, out, progress)
186
+ proc = subprocess.run(
187
+ argv,
188
+ cwd=str(vault.root),
189
+ env=env,
190
+ text=True,
191
+ stdout=out,
192
+ encoding="utf-8",
193
+ errors="replace",
194
+ )
195
+ return proc.returncode
196
+
197
+
198
+ def run_target(vault: Vault, name: str, args: list[str]) -> int:
199
+ target = vault.get(name)
200
+ if target is None:
201
+ return 127
202
+ if isinstance(target, Orchestration):
203
+ progress = _StepProgress(sys.stdout)
204
+ return run_orchestration(
205
+ vault, target, progress=progress, show_progress=True
206
+ )
207
+ return run_script(vault, target, args)
208
+
209
+
210
+ def _run_sh_step(vault: Vault, command: str, out=None, progress=None) -> int:
211
+ env = build_env(vault, None)
212
+ argv = [vault.shell, "-c", command]
213
+ if progress is not None:
214
+ return _run_child(argv, env, vault.root, out, progress)
215
+ proc = subprocess.run(
216
+ argv,
217
+ cwd=str(vault.root),
218
+ env=env,
219
+ text=True,
220
+ stdout=out,
221
+ encoding="utf-8",
222
+ errors="replace",
223
+ )
224
+ return proc.returncode
225
+
226
+
227
+ def _run_job(vault: Vault, name: str, out, progress=None) -> int:
228
+ """Run one target, sending its stdout to `out` (a text file object)."""
229
+ target = vault.get(name)
230
+ if isinstance(target, Orchestration):
231
+ return run_orchestration(
232
+ vault, target, out=out, progress=progress, show_progress=False
233
+ )
234
+ return run_script(vault, target, [], out=out, progress=progress)
235
+
236
+
237
+ def run_parallel(vault: Vault, names: list[str], out=None, progress=None) -> int:
238
+ if out is None:
239
+ out = sys.stdout
240
+ readers: list[threading.Thread] = []
241
+
242
+ def reader(name: str, stream) -> None:
243
+ prefix = f"[{name[:_PREFIX_MAX]}] "
244
+ try:
245
+ for line in stream:
246
+ if progress is not None:
247
+ progress.note_output()
248
+ out.write(f"{prefix}{line}")
249
+ out.flush()
250
+ except Exception as exc:
251
+ print(f"sv: {name}: {exc}", file=sys.stderr, flush=True)
252
+
253
+ procs: list[subprocess.Popen] = []
254
+ jobs: list[tuple[threading.Thread, list[int]]] = []
255
+ for name in names:
256
+ target = vault.get(name)
257
+ if isinstance(target, Script):
258
+ env = build_env(vault, target.name)
259
+ argv = _script_argv(vault, target, [])
260
+ proc = subprocess.Popen(
261
+ argv,
262
+ cwd=str(vault.root),
263
+ env=env,
264
+ stdout=subprocess.PIPE,
265
+ stderr=subprocess.STDOUT,
266
+ text=True,
267
+ encoding="utf-8",
268
+ errors="replace",
269
+ )
270
+ procs.append(proc)
271
+ stream = proc.stdout
272
+ else:
273
+ rfd, wfd = os.pipe()
274
+ stream = os.fdopen(rfd, "r", encoding="utf-8", errors="replace")
275
+ job_out = os.fdopen(wfd, "w", encoding="utf-8", errors="replace")
276
+ codes = []
277
+
278
+ def job(n=name, o=job_out, c=codes, p=progress) -> None:
279
+ try:
280
+ c.append(_run_job(vault, n, out=o, progress=p))
281
+ except Exception as exc:
282
+ print(f"sv: {n}: {exc}", file=sys.stderr, flush=True)
283
+ c.append(1)
284
+ finally:
285
+ try:
286
+ o.close()
287
+ except Exception:
288
+ pass
289
+
290
+ job_thread = threading.Thread(target=job, daemon=True)
291
+ job_thread.start()
292
+ jobs.append((job_thread, codes))
293
+ thread = threading.Thread(
294
+ target=reader, args=(name, stream), daemon=True
295
+ )
296
+ readers.append(thread)
297
+ thread.start()
298
+
299
+ for proc in procs:
300
+ proc.wait()
301
+ for job_thread, _ in jobs:
302
+ job_thread.join()
303
+ for thread in readers:
304
+ thread.join()
305
+ for proc in procs:
306
+ proc.stdout.close()
307
+
308
+ codes = [proc.returncode for proc in procs]
309
+ for _, job_codes in jobs:
310
+ codes.append(job_codes[0])
311
+ for code in codes:
312
+ if code != 0:
313
+ return code
314
+ return 0
315
+
316
+
317
+ def run_orchestration(
318
+ vault: Vault,
319
+ orch: Orchestration,
320
+ out=None,
321
+ progress=None,
322
+ show_progress: bool = False,
323
+ ) -> int:
324
+ total = len(orch.steps)
325
+ for index, step in enumerate(orch.steps, start=1):
326
+ if progress is not None and show_progress:
327
+ progress.begin(index, total, _step_label(step))
328
+ try:
329
+ if step.kind == "run":
330
+ target = vault.get(step.run_name)
331
+ if isinstance(target, Orchestration):
332
+ code = run_orchestration(
333
+ vault,
334
+ target,
335
+ out=out,
336
+ progress=progress,
337
+ show_progress=False,
338
+ )
339
+ else:
340
+ code = run_script(
341
+ vault, target, step.run_args, out=out, progress=progress
342
+ )
343
+ elif step.kind == "sh":
344
+ code = _run_sh_step(vault, step.sh, out=out, progress=progress)
345
+ else:
346
+ code = run_parallel(
347
+ vault, step.parallel, out=out, progress=progress
348
+ )
349
+ finally:
350
+ if progress is not None and show_progress:
351
+ progress.end()
352
+ if code != 0:
353
+ return code
354
+ return 0
@@ -0,0 +1,93 @@
1
+ """Templates for files created by `sv init` and `sv add`."""
2
+
3
+ VAULT_TOML = """\
4
+ [vault]
5
+ name = "{name}"
6
+ """
7
+
8
+ SCRIPT_TEMPLATE = """\
9
+ #!/bin/sh
10
+ # ScriptVault script: {name}
11
+ # Usage: sv run {name} [args...]
12
+ echo "Hello from {name}"
13
+ """
14
+
15
+ GLOBAL_ENV = """\
16
+ # ScriptVault global environment
17
+ # Variables here apply to every script and are overridable per script.
18
+ """
19
+
20
+ SCRIPT_ENV = """\
21
+ # ScriptVault environment for "{name}"
22
+ # Values here override the global .sv/.env for this script only.
23
+ """
24
+
25
+ COMPLETION_BASH = """\
26
+ # bash completion for sv
27
+ # Targets are completed by asking the vault itself: `sv __names`.
28
+ _sv() {
29
+ local cur
30
+ cur="${COMP_WORDS[COMP_CWORD]}"
31
+ if [[ "${#COMP_WORDS[@]}" -gt 1 ]]; then
32
+ case "${COMP_WORDS[1]}" in
33
+ run)
34
+ COMPREPLY=($(compgen -W "$(sv __names 2>/dev/null)" -- "$cur"))
35
+ return
36
+ ;;
37
+ edit)
38
+ if [[ "${#COMP_WORDS[@]}" -eq 3 ]]; then
39
+ COMPREPLY=($(compgen -W "$(sv __names 2>/dev/null) config env" -- "$cur"))
40
+ else
41
+ COMPREPLY=($(compgen -W "env config" -- "$cur"))
42
+ fi
43
+ return
44
+ ;;
45
+ esac
46
+ fi
47
+ COMPREPLY=($(compgen -W "__COMMANDS__ $(sv __names 2>/dev/null)" -- "$cur"))
48
+ }
49
+ complete -F _sv sv
50
+ """
51
+
52
+ COMPLETION_ZSH = """\
53
+ # zsh completion for sv
54
+ # Targets are completed by asking the vault itself: `sv __names`.
55
+ _sv() {
56
+ local -a builts names
57
+ local out
58
+ builts=(__COMMANDS__)
59
+ names=()
60
+ if out=$(sv __names 2>/dev/null) && [[ -n $out ]]; then
61
+ names=("${(@f)$out}")
62
+ fi
63
+ case "${words[2]-}" in
64
+ run)
65
+ if (( CURRENT == 3 )); then
66
+ _describe 'target' names
67
+ fi
68
+ ;;
69
+ edit)
70
+ if (( CURRENT == 3 )); then
71
+ compadd -- $names config env
72
+ elif (( CURRENT == 4 )); then
73
+ compadd -- env config
74
+ fi
75
+ ;;
76
+ *)
77
+ _describe 'command' builts
78
+ _describe 'target' names
79
+ ;;
80
+ esac
81
+ }
82
+ compdef _sv sv
83
+ """
84
+
85
+ COMPLETION_FISH = """\
86
+ # fish completion for sv
87
+ # Targets are completed by asking the vault itself: `sv __names`.
88
+ for cmd in __COMMANDS__
89
+ complete -c sv -n "__fish_is_first_arg" -f -a "$cmd"
90
+ end
91
+ complete -c sv -n "__fish_seen_subcommand_from run" -f -a "(sv __names 2>/dev/null)"
92
+ complete -c sv -n "__fish_seen_subcommand_from edit" -f -a "(sv __names 2>/dev/null)" -a "config" -a "env"
93
+ """
scriptvault/term.py ADDED
@@ -0,0 +1,42 @@
1
+ """Terminal formatting helpers."""
2
+
3
+ import os
4
+ import sys
5
+
6
+ GREEN = "32"
7
+ CYAN = "36"
8
+ MAGENTA = "35"
9
+
10
+
11
+ class Palette:
12
+ def __init__(self, stream=None):
13
+ self._stream = sys.stdout if stream is None else stream
14
+ self.enabled = False
15
+ if os.environ.get("NO_COLOR") is None:
16
+ isatty = getattr(self._stream, "isatty", None)
17
+ if callable(isatty):
18
+ try:
19
+ self.enabled = bool(isatty())
20
+ except (OSError, ValueError):
21
+ self.enabled = False
22
+
23
+ def paint(
24
+ self,
25
+ text: str,
26
+ *,
27
+ bold: bool = False,
28
+ dim: bool = False,
29
+ fg: str | None = None,
30
+ ) -> str:
31
+ if not self.enabled:
32
+ return text
33
+ parts: list[str] = []
34
+ if bold:
35
+ parts.append("1")
36
+ if dim:
37
+ parts.append("2")
38
+ if fg is not None:
39
+ parts.append(str(fg))
40
+ if not parts:
41
+ return text
42
+ return f"\033[{';'.join(parts)}m{text}\033[0m"