darwin-shell 0.1.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.
@@ -0,0 +1,394 @@
1
+ #!/usr/bin/env bash
2
+ # darwin-shell process: bounded concurrency, timeouts and process lifecycle.
3
+ # This module never changes the caller's shell options, never installs a trap,
4
+ # and never exits. Child processes run in subshells where isolation is needed;
5
+ # results cross the process boundary through rc files, not shell variables.
6
+
7
+ # Dependencies sit below the guard so a failed dependency is retryable on the
8
+ # next source attempt.
9
+ if ! declare -F dsh::i18n::t >/dev/null 2>&1; then
10
+ if declare -F dsh::use >/dev/null 2>&1; then
11
+ dsh::use i18n || return $?
12
+ else
13
+ _dsh_private_process_self="${BASH_SOURCE[0]}"
14
+ source "${_dsh_private_process_self%/*}/i18n.sh" || return $?
15
+ unset _dsh_private_process_self
16
+ fi
17
+ fi
18
+ if ! declare -F dsh::core::fail >/dev/null 2>&1; then
19
+ if declare -F dsh::use >/dev/null 2>&1; then
20
+ dsh::use core || return $?
21
+ else
22
+ _dsh_private_process_self="${BASH_SOURCE[0]}"
23
+ source "${_dsh_private_process_self%/*}/core.sh" || return $?
24
+ unset _dsh_private_process_self
25
+ fi
26
+ fi
27
+ if ! declare -F dsh::file::temp_dir >/dev/null 2>&1; then
28
+ if declare -F dsh::use >/dev/null 2>&1; then
29
+ dsh::use file || return $?
30
+ else
31
+ _dsh_private_process_self="${BASH_SOURCE[0]}"
32
+ source "${_dsh_private_process_self%/*}/file.sh" || return $?
33
+ unset _dsh_private_process_self
34
+ fi
35
+ fi
36
+
37
+ if [[ -n "${_dsh_private_process_sourced:-}" ]]; then
38
+ return 0
39
+ fi
40
+ _dsh_private_process_sourced=1
41
+ readonly _dsh_private_process_sourced
42
+
43
+ _dsh_private_process_help() {
44
+ case "${1:-}" in
45
+ parallel)
46
+ dsh::i18n::t 'Usage: dsh::process::parallel --tasks FILE --handler FN [--max-parallel N] [--into VAR]'
47
+ printf '\n'
48
+ dsh::i18n::t 'Run tasks with bounded concurrency; hand the caller a stats associative array.'
49
+ ;;
50
+ run_timeout)
51
+ dsh::i18n::t 'Usage: dsh::process::run_timeout <seconds> [--kill-after SEC] -- <cmd> [args...]'
52
+ printf '\n'
53
+ dsh::i18n::t 'Run a command under a timeout; return 124 when it expires.'
54
+ ;;
55
+ spawn)
56
+ dsh::i18n::t 'Usage: dsh::process::spawn [--pid-into VAR] -- <cmd> [args...]'
57
+ printf '\n'
58
+ dsh::i18n::t 'Start a background command and hand its PID to the caller.'
59
+ ;;
60
+ alive)
61
+ dsh::i18n::t 'Usage: dsh::process::alive <pid>'
62
+ printf '\n'
63
+ dsh::i18n::t 'Return 0 when <pid> exists, 1 otherwise. No output.'
64
+ ;;
65
+ stop)
66
+ dsh::i18n::t 'Usage: dsh::process::stop <pid> [--grace SEC] [--kill-after SEC]'
67
+ printf '\n'
68
+ dsh::i18n::t 'Send TERM, wait, then KILL if still alive. Never installs a trap.'
69
+ ;;
70
+ *)
71
+ dsh::i18n::t 'dsh::process: no help for %1$s' "${1:-<empty>}" >&2
72
+ return "$DSH_RC_USAGE"
73
+ ;;
74
+ esac
75
+ }
76
+
77
+ _dsh_private_process_safe_var() {
78
+ local name="${1:-}"
79
+ [[ "$name" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || return 1
80
+ case "$name" in
81
+ BASH_ENV|ENV|IFS|PATH|PS4|SHELLOPTS|SECONDS|_dsh_private_process_*) return 1 ;;
82
+ esac
83
+ return 0
84
+ }
85
+
86
+ # Comma-separated list membership. IFS is a local variable so the caller's
87
+ # shell state survives even when the list is malformed.
88
+ _dsh_private_process_in_list() {
89
+ local rc="$1" codes="$2" item
90
+ [[ -n "$codes" ]] || return 1
91
+ local IFS=','
92
+ for item in $codes; do
93
+ [[ "$item" == "$rc" ]] && return 0
94
+ done
95
+ return 1
96
+ }
97
+
98
+ # Collect every finished rc file. Called from parallel, whose local counters
99
+ # are visible through bash dynamic scope; no globals are exported.
100
+ _dsh_private_process_collect_rc() {
101
+ local dir="$1" skip_codes="$2" max_failures="$3" file rc
102
+ for file in "$dir"/*.rc; do
103
+ [[ -f "$file" ]] || continue
104
+ rc="$(<"$file")"
105
+ rm -f -- "$file"
106
+ total=$((total + 1))
107
+ if (( rc == 0 )); then
108
+ ok=$((ok + 1))
109
+ elif _dsh_private_process_in_list "$rc" "$skip_codes"; then
110
+ skip=$((skip + 1))
111
+ else
112
+ fail=$((fail + 1))
113
+ fi
114
+ if (( max_failures > 0 && fail >= max_failures )); then
115
+ stop=1
116
+ fi
117
+ done
118
+ }
119
+
120
+ # Public: run a flat task list with bounded concurrency. Every task runs in a
121
+ # subshell, including at max-parallel=1, so handler behaviour does not depend
122
+ # on the concurrency setting. Statistics are returned through --into only.
123
+ dsh::process::parallel() {
124
+ if [[ "${1:-}" == "--help" ]]; then
125
+ _dsh_private_process_help parallel
126
+ return $?
127
+ fi
128
+ local tasks_file='' handler='' max_parallel=1 skip_codes='' max_failures=0 into=''
129
+ while (( $# )); do
130
+ case "$1" in
131
+ --tasks|--handler|--max-parallel|--skip-codes|--max-failures|--into)
132
+ if (( $# < 2 )); then
133
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process: %1$s requires a value' "$1"
134
+ return $?
135
+ fi
136
+ case "$1" in
137
+ --tasks) tasks_file="$2" ;;
138
+ --handler) handler="$2" ;;
139
+ --max-parallel) max_parallel="$2" ;;
140
+ --skip-codes) skip_codes="$2" ;;
141
+ --max-failures) max_failures="$2" ;;
142
+ --into) into="$2" ;;
143
+ esac
144
+ shift 2
145
+ ;;
146
+ *)
147
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process: unknown option: %1$s' "$1"
148
+ return $?
149
+ ;;
150
+ esac
151
+ done
152
+
153
+ if [[ -z "$tasks_file" ]]; then
154
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::parallel: --tasks requires a file'
155
+ return $?
156
+ fi
157
+ if [[ ! -f "$tasks_file" ]]; then
158
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::parallel: tasks file not found: %1$s' "$tasks_file"
159
+ return $?
160
+ fi
161
+ if [[ -z "$handler" ]]; then
162
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::parallel: --handler requires a function name'
163
+ return $?
164
+ fi
165
+ if ! declare -F "$handler" >/dev/null 2>&1; then
166
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::parallel: handler is not a function: %1$s' "$handler"
167
+ return $?
168
+ fi
169
+ if [[ ! "$max_parallel" =~ ^[0-9]+$ ]] || (( max_parallel < 1 )); then
170
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::parallel: --max-parallel requires a positive integer: %1$s' "$max_parallel"
171
+ return $?
172
+ fi
173
+ if [[ ! "$max_failures" =~ ^[0-9]+$ ]]; then
174
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::parallel: --max-failures requires a non-negative integer: %1$s' "$max_failures"
175
+ return $?
176
+ fi
177
+ if [[ -n "$into" ]] && ! _dsh_private_process_safe_var "$into"; then
178
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process: --into requires a safe variable name: %1$s' "$into"
179
+ return $?
180
+ fi
181
+
182
+ local dir
183
+ dir="$(dsh::file::temp_dir darwin-process)" || return $?
184
+ local total=0 ok=0 skip=0 fail=0 stop=0
185
+ local task_id=0 task running=0
186
+ local -a pids=()
187
+
188
+ while IFS= read -r task || [[ -n "$task" ]]; do
189
+ [[ -n "$task" ]] || continue
190
+ (( stop )) && break
191
+ if (( running >= max_parallel )); then
192
+ wait -n 2>/dev/null || true
193
+ running=$((running - 1))
194
+ _dsh_private_process_collect_rc "$dir" "$skip_codes" "$max_failures"
195
+ # A stop condition observed while reaping must prevent the next spawn.
196
+ (( stop )) && break
197
+ fi
198
+ task_id=$((task_id + 1))
199
+ (
200
+ local rc=0
201
+ "$handler" "$task_id" "$task" || rc=$?
202
+ printf '%s\n' "$rc" > "$dir/${task_id}.rc"
203
+ ) &
204
+ pids+=("$!")
205
+ running=$((running + 1))
206
+ done < "$tasks_file"
207
+
208
+ local pid
209
+ for pid in "${pids[@]}"; do
210
+ wait "$pid" 2>/dev/null || true
211
+ done
212
+ _dsh_private_process_collect_rc "$dir" "$skip_codes" "$max_failures"
213
+ rm -rf -- "$dir"
214
+
215
+ if [[ -n "$into" ]]; then
216
+ local -n _dsh_private_process_ref="$into"
217
+ _dsh_private_process_ref=([total]="$total" [ok]="$ok" [skip]="$skip" [fail]="$fail")
218
+ fi
219
+
220
+ (( fail > 0 )) && return "$DSH_RC_FAILURE"
221
+ return "$DSH_RC_OK"
222
+ }
223
+
224
+ # Public: run a command under a timeout. Arguments are passed through as an
225
+ # argv array; no shell string is built from them.
226
+ dsh::process::run_timeout() {
227
+ if [[ "${1:-}" == "--help" ]]; then
228
+ _dsh_private_process_help run_timeout
229
+ return $?
230
+ fi
231
+ if (( $# < 1 )); then
232
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::run_timeout: missing <seconds>'
233
+ return $?
234
+ fi
235
+ if [[ ! "$1" =~ ^[0-9]+$ ]]; then
236
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::run_timeout: invalid seconds: %1$s' "$1"
237
+ return $?
238
+ fi
239
+ local seconds="$1" kill_after=30
240
+ shift
241
+ while (( $# )) && [[ "$1" == --* ]] && [[ "$1" != -- ]]; do
242
+ case "$1" in
243
+ --kill-after)
244
+ if (( $# < 2 )) || [[ ! "$2" =~ ^[0-9]+$ ]]; then
245
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::run_timeout: --kill-after requires a non-negative integer: %1$s' "${2:-}"
246
+ return $?
247
+ fi
248
+ kill_after="$2"
249
+ shift 2
250
+ ;;
251
+ *)
252
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process: unknown option: %1$s' "$1"
253
+ return $?
254
+ ;;
255
+ esac
256
+ done
257
+ if [[ "${1:-}" != "--" ]] || (( $# < 2 )); then
258
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::run_timeout: missing command after --'
259
+ return $?
260
+ fi
261
+ shift
262
+ local -a command=("$@")
263
+
264
+ local bin
265
+ if ! bin="$(dsh::core::tool timeout 2>/dev/null)"; then
266
+ case "${DARWIN_SH_PROCESS_BEST_EFFORT:-0}" in
267
+ 1|true|TRUE|yes|YES|on|ON)
268
+ dsh::core::warn_once 'process-no-timeout' \
269
+ 'dsh::process: timeout command unavailable; execution is not bounded'
270
+ "${command[@]}"
271
+ return $?
272
+ ;;
273
+ esac
274
+ dsh::core::fail "$DSH_RC_TRANSIENT" 'dsh::process: timeout command unavailable; execution is not bounded'
275
+ return $?
276
+ fi
277
+ # Some coreutils timeout builds treat a trailing -- as the command name.
278
+ "$bin" --kill-after="$kill_after" "$seconds" "${command[@]}"
279
+ return $?
280
+ }
281
+
282
+ # Public: start a command in the background and hand its PID to the caller.
283
+ dsh::process::spawn() {
284
+ if [[ "${1:-}" == "--help" ]]; then
285
+ _dsh_private_process_help spawn
286
+ return $?
287
+ fi
288
+ local pid_var=''
289
+ while (( $# )) && [[ "$1" == --* ]] && [[ "$1" != -- ]]; do
290
+ case "$1" in
291
+ --pid-into)
292
+ if (( $# < 2 )); then
293
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process: --pid-into requires a value'
294
+ return $?
295
+ fi
296
+ pid_var="$2"
297
+ shift 2
298
+ ;;
299
+ *)
300
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process: unknown option: %1$s' "$1"
301
+ return $?
302
+ ;;
303
+ esac
304
+ done
305
+ if [[ "${1:-}" != "--" ]] || (( $# < 2 )); then
306
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::spawn: missing command after --'
307
+ return $?
308
+ fi
309
+ if [[ -n "$pid_var" ]] && ! _dsh_private_process_safe_var "$pid_var"; then
310
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::spawn: --pid-into requires a safe variable name: %1$s' "$pid_var"
311
+ return $?
312
+ fi
313
+ shift
314
+ "${@}" &
315
+ # Private-prefixed name: --pid-into rejects this prefix, so the nameref
316
+ # cannot resolve back here and shadow the caller's target variable.
317
+ local _dsh_private_process_pid=$!
318
+ if [[ -n "$pid_var" ]]; then
319
+ local -n _dsh_private_process_ref="$pid_var"
320
+ _dsh_private_process_ref="$_dsh_private_process_pid"
321
+ fi
322
+ return 0
323
+ }
324
+
325
+ # Public: liveness probe with no output.
326
+ dsh::process::alive() {
327
+ if [[ "${1:-}" == "--help" ]]; then
328
+ _dsh_private_process_help alive
329
+ return $?
330
+ fi
331
+ if (( $# != 1 )); then
332
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::alive: expected <pid>'
333
+ return $?
334
+ fi
335
+ if [[ ! "$1" =~ ^[0-9]+$ ]]; then
336
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::alive: invalid pid: %1$s' "$1"
337
+ return $?
338
+ fi
339
+ kill -0 "$1" 2>/dev/null
340
+ }
341
+
342
+ # Public: TERM the process, wait up to grace seconds, then KILL if requested
343
+ # and still alive. Polling is intentional here: the target may not be a child
344
+ # of this shell, so wait(2) cannot observe it.
345
+ dsh::process::stop() {
346
+ if [[ "${1:-}" == "--help" ]]; then
347
+ _dsh_private_process_help stop
348
+ return $?
349
+ fi
350
+ if (( $# < 1 )) || [[ "$1" == --* ]]; then
351
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::stop: expected <pid>'
352
+ return $?
353
+ fi
354
+ if [[ ! "$1" =~ ^[0-9]+$ ]]; then
355
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::stop: invalid pid: %1$s' "$1"
356
+ return $?
357
+ fi
358
+ local pid="$1" grace=5 kill_after=2
359
+ shift
360
+ while (( $# )); do
361
+ case "$1" in
362
+ --grace|--kill-after)
363
+ if (( $# < 2 )) || [[ ! "$2" =~ ^[0-9]+$ ]]; then
364
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process::stop: %1$s requires a non-negative integer: %2$s' "$1" "${2:-}"
365
+ return $?
366
+ fi
367
+ [[ "$1" == --grace ]] && grace="$2" || kill_after="$2"
368
+ shift 2
369
+ ;;
370
+ *)
371
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::process: unknown option: %1$s' "$1"
372
+ return $?
373
+ ;;
374
+ esac
375
+ done
376
+
377
+ if ! dsh::process::alive "$pid"; then
378
+ return 0
379
+ fi
380
+ kill -TERM "$pid" 2>/dev/null || true
381
+ local i
382
+ for (( i = 0; i < grace * 10; i++ )); do
383
+ dsh::process::alive "$pid" || return 0
384
+ sleep 0.1
385
+ done
386
+ if (( kill_after > 0 )); then
387
+ kill -KILL "$pid" 2>/dev/null || true
388
+ for (( i = 0; i < kill_after * 10; i++ )); do
389
+ dsh::process::alive "$pid" || return 0
390
+ sleep 0.1
391
+ done
392
+ fi
393
+ return "$DSH_RC_FAILURE"
394
+ }