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,540 @@
1
+ #!/usr/bin/env bash
2
+ # darwin-shell stats: generic counters, lists and messages with cross-process
3
+ # aggregation and atomic JSON export. This module does not interpret rc codes,
4
+ # read manifests, install traps or assemble notifications.
5
+
6
+ if ! declare -F dsh::i18n::t >/dev/null 2>&1; then
7
+ if declare -F dsh::use >/dev/null 2>&1; then
8
+ dsh::use i18n || return $?
9
+ else
10
+ _dsh_private_stats_self="${BASH_SOURCE[0]}"
11
+ source "${_dsh_private_stats_self%/*}/i18n.sh" || return $?
12
+ unset _dsh_private_stats_self
13
+ fi
14
+ fi
15
+ if ! declare -F dsh::core::fail >/dev/null 2>&1; then
16
+ if declare -F dsh::use >/dev/null 2>&1; then
17
+ dsh::use core || return $?
18
+ else
19
+ _dsh_private_stats_self="${BASH_SOURCE[0]}"
20
+ source "${_dsh_private_stats_self%/*}/core.sh" || return $?
21
+ unset _dsh_private_stats_self
22
+ fi
23
+ fi
24
+ if ! declare -F dsh::file::atomic_write >/dev/null 2>&1; then
25
+ if declare -F dsh::use >/dev/null 2>&1; then
26
+ dsh::use file || return $?
27
+ else
28
+ _dsh_private_stats_self="${BASH_SOURCE[0]}"
29
+ source "${_dsh_private_stats_self%/*}/file.sh" || return $?
30
+ unset _dsh_private_stats_self
31
+ fi
32
+ fi
33
+
34
+ if [[ -n "${_dsh_private_stats_sourced:-}" ]]; then
35
+ return 0
36
+ fi
37
+ _dsh_private_stats_sourced=1
38
+ readonly _dsh_private_stats_sourced
39
+
40
+ # Private containers exist so functions can share state; they are populated
41
+ # only by an explicit init call, never by sourcing this file.
42
+ declare -gA _dsh_private_stats_counters=()
43
+ declare -gA _dsh_private_stats_lists=()
44
+ declare -gA _dsh_private_stats_messages=()
45
+ declare -ga _dsh_private_stats_dims=()
46
+
47
+ _dsh_private_stats_help() {
48
+ case "${1:-}" in
49
+ init)
50
+ dsh::i18n::t 'Usage: dsh::stats::init [--dims "a,b,c"]'
51
+ printf '\n'
52
+ dsh::i18n::t 'Reset private counters, lists and messages without touching the filesystem.'
53
+ ;;
54
+ record)
55
+ dsh::i18n::t 'Usage: dsh::stats::record <dim> [n]'
56
+ printf '\n'
57
+ dsh::i18n::t 'Add n (default 1) to a named counter; no rc or business semantics.'
58
+ ;;
59
+ add)
60
+ dsh::i18n::t 'Usage: dsh::stats::add <dim> <n>'
61
+ printf '\n'
62
+ dsh::i18n::t 'Add an integer to a named counter.'
63
+ ;;
64
+ list)
65
+ dsh::i18n::t 'Usage: dsh::stats::list <name> <item>...'
66
+ printf '\n'
67
+ dsh::i18n::t 'Append string items to a named list.'
68
+ ;;
69
+ message)
70
+ dsh::i18n::t 'Usage: dsh::stats::message <level> <text>'
71
+ printf '\n'
72
+ dsh::i18n::t 'Append a message under a named level.'
73
+ ;;
74
+ get)
75
+ dsh::i18n::t 'Usage: dsh::stats::get <dim>'
76
+ printf '\n'
77
+ dsh::i18n::t 'Print the current value of a counter.'
78
+ ;;
79
+ result)
80
+ dsh::i18n::t 'Usage: dsh::stats::result [--into VAR]'
81
+ printf '\n'
82
+ dsh::i18n::t 'Return a flat associative array of counter.*, list.* and msg.* keys.'
83
+ ;;
84
+ flush)
85
+ dsh::i18n::t 'Usage: dsh::stats::flush <dir>'
86
+ printf '\n'
87
+ dsh::i18n::t 'Atomically write this process stats as <BASHPID>.stats using NUL records.'
88
+ ;;
89
+ merge)
90
+ dsh::i18n::t 'Usage: dsh::stats::merge <dir>'
91
+ printf '\n'
92
+ dsh::i18n::t 'Merge every *.stats file into local state and remove merged files.'
93
+ ;;
94
+ dump_json)
95
+ dsh::i18n::t 'Usage: dsh::stats::dump_json <file> [--started N] [--finished N] [--exit-code N] [--stage S]'
96
+ printf '\n'
97
+ dsh::i18n::t 'Atomically export counters, lists and messages as JSON via jq.'
98
+ ;;
99
+ *)
100
+ dsh::i18n::t 'dsh::stats: no help for %1$s' "${1:-<empty>}" >&2
101
+ return "$DSH_RC_USAGE"
102
+ ;;
103
+ esac
104
+ }
105
+
106
+ _dsh_private_stats_safe_name() {
107
+ [[ "${1:-}" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]
108
+ }
109
+
110
+ _dsh_private_stats_safe_var() {
111
+ local name="${1:-}"
112
+ _dsh_private_stats_safe_name "$name" || return 1
113
+ case "$name" in
114
+ BASH_ENV|ENV|IFS|PATH|PS4|SHELLOPTS|SECONDS|_dsh_private_stats_*) return 1 ;;
115
+ esac
116
+ return 0
117
+ }
118
+
119
+ # Public: reset all private state. No directory is created here.
120
+ dsh::stats::init() {
121
+ if [[ "${1:-}" == "--help" ]]; then
122
+ _dsh_private_stats_help init
123
+ return $?
124
+ fi
125
+ local dims='success,fail,skip,empty'
126
+ while (( $# )); do
127
+ case "$1" in
128
+ --dims)
129
+ if (( $# < 2 )); then
130
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats: --dims requires a value'
131
+ return $?
132
+ fi
133
+ dims="$2"
134
+ shift 2
135
+ ;;
136
+ *)
137
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats: unknown option: %1$s' "$1"
138
+ return $?
139
+ ;;
140
+ esac
141
+ done
142
+
143
+ local -a parsed=()
144
+ local IFS=','
145
+ for dim in $dims; do
146
+ if ! _dsh_private_stats_safe_name "$dim"; then
147
+ dsh::core::fail "$DSH_RC_USAGE" \
148
+ 'dsh::stats::init: invalid dimension name: %1$s' "$dim"
149
+ return $?
150
+ fi
151
+ parsed+=("$dim")
152
+ done
153
+ if (( ${#parsed[@]} == 0 )); then
154
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats::init: --dims must not be empty'
155
+ return $?
156
+ fi
157
+
158
+ _dsh_private_stats_dims=("${parsed[@]}")
159
+ _dsh_private_stats_counters=()
160
+ _dsh_private_stats_lists=()
161
+ _dsh_private_stats_messages=()
162
+ local dim
163
+ for dim in "${parsed[@]}"; do
164
+ _dsh_private_stats_counters["$dim"]=0
165
+ done
166
+ return 0
167
+ }
168
+
169
+ # Public: add an integer to a counter.
170
+ dsh::stats::record() {
171
+ if [[ "${1:-}" == "--help" ]]; then
172
+ _dsh_private_stats_help record
173
+ return $?
174
+ fi
175
+ if (( $# < 1 )); then
176
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats::record: expected <dim>'
177
+ return $?
178
+ fi
179
+ local dim="$1" n="${2:-1}"
180
+ if ! _dsh_private_stats_safe_name "$dim"; then
181
+ dsh::core::fail "$DSH_RC_USAGE" \
182
+ 'dsh::stats::record: invalid dimension name: %1$s' "$dim"
183
+ return $?
184
+ fi
185
+ if [[ ! "$n" =~ ^-?[0-9]+$ ]]; then
186
+ dsh::core::fail "$DSH_RC_USAGE" \
187
+ 'dsh::stats::record: value must be an integer: %1$s' "$n"
188
+ return $?
189
+ fi
190
+ local current="${_dsh_private_stats_counters[$dim]:-0}"
191
+ _dsh_private_stats_counters["$dim"]=$((current + n))
192
+ return 0
193
+ }
194
+
195
+ dsh::stats::add() {
196
+ if [[ "${1:-}" == "--help" ]]; then
197
+ _dsh_private_stats_help add
198
+ return $?
199
+ fi
200
+ if (( $# != 2 )); then
201
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats::add: expected <dim> <n>'
202
+ return $?
203
+ fi
204
+ dsh::stats::record "$1" "$2"
205
+ return $?
206
+ }
207
+
208
+ # Public: append items to a named list.
209
+ dsh::stats::list() {
210
+ if [[ "${1:-}" == "--help" ]]; then
211
+ _dsh_private_stats_help list
212
+ return $?
213
+ fi
214
+ if (( $# < 2 )); then
215
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats::list: expected <name> <item>...'
216
+ return $?
217
+ fi
218
+ local name="$1"
219
+ if ! _dsh_private_stats_safe_name "$name"; then
220
+ dsh::core::fail "$DSH_RC_USAGE" \
221
+ 'dsh::stats::list: invalid list name: %1$s' "$name"
222
+ return $?
223
+ fi
224
+ shift
225
+ local key="${name}:${#_dsh_private_stats_lists[@]}" item
226
+ for item in "$@"; do
227
+ _dsh_private_stats_lists["$key"]="$item"
228
+ key="${name}:${#_dsh_private_stats_lists[@]}"
229
+ done
230
+ return 0
231
+ }
232
+
233
+ # Public: append a message under a named level.
234
+ dsh::stats::message() {
235
+ if [[ "${1:-}" == "--help" ]]; then
236
+ _dsh_private_stats_help message
237
+ return $?
238
+ fi
239
+ if (( $# != 2 )); then
240
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats::message: expected <level> <text>'
241
+ return $?
242
+ fi
243
+ local level="$1"
244
+ if ! _dsh_private_stats_safe_name "$level"; then
245
+ dsh::core::fail "$DSH_RC_USAGE" \
246
+ 'dsh::stats::message: invalid level: %1$s' "$level"
247
+ return $?
248
+ fi
249
+ _dsh_private_stats_messages["${level}:${#_dsh_private_stats_messages[@]}"]="$2"
250
+ return 0
251
+ }
252
+
253
+ # Public: print a counter value.
254
+ dsh::stats::get() {
255
+ if [[ "${1:-}" == "--help" ]]; then
256
+ _dsh_private_stats_help get
257
+ return $?
258
+ fi
259
+ if (( $# != 1 )); then
260
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats::get: expected <dim>'
261
+ return $?
262
+ fi
263
+ if ! _dsh_private_stats_safe_name "$1"; then
264
+ dsh::core::fail "$DSH_RC_USAGE" \
265
+ 'dsh::stats::get: invalid dimension name: %1$s' "$1"
266
+ return $?
267
+ fi
268
+ printf '%s\n' "${_dsh_private_stats_counters[$1]:-0}"
269
+ return 0
270
+ }
271
+
272
+ # Public: hand the caller a flat associative array. Items with arbitrary text
273
+ # stay in values; keys encode structure without needing nested arrays.
274
+ dsh::stats::result() {
275
+ if [[ "${1:-}" == "--help" ]]; then
276
+ _dsh_private_stats_help result
277
+ return $?
278
+ fi
279
+ local into=''
280
+ while (( $# )); do
281
+ case "$1" in
282
+ --into)
283
+ if (( $# < 2 )); then
284
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats: --into requires a value'
285
+ return $?
286
+ fi
287
+ into="$2"
288
+ shift 2
289
+ ;;
290
+ *)
291
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats: unknown option: %1$s' "$1"
292
+ return $?
293
+ ;;
294
+ esac
295
+ done
296
+ if [[ -z "$into" ]]; then
297
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats::result: --into is required'
298
+ return $?
299
+ fi
300
+ if ! _dsh_private_stats_safe_var "$into"; then
301
+ dsh::core::fail "$DSH_RC_USAGE" \
302
+ 'dsh::stats::result: --into requires a safe variable name: %1$s' "$into"
303
+ return $?
304
+ fi
305
+
306
+ local -n _dsh_private_stats_ref="$into"
307
+ _dsh_private_stats_ref=()
308
+ local key name value
309
+ for key in "${!_dsh_private_stats_counters[@]}"; do
310
+ _dsh_private_stats_ref["counter.$key"]="${_dsh_private_stats_counters[$key]}"
311
+ done
312
+ for key in "${!_dsh_private_stats_lists[@]}"; do
313
+ name="${key%%:*}"
314
+ _dsh_private_stats_ref["list.$name.count"]=$(( \
315
+ ${_dsh_private_stats_ref["list.$name.count"]:-0} + 1 ))
316
+ _dsh_private_stats_ref["list.$name.${key#*:}"]="${_dsh_private_stats_lists[$key]}"
317
+ done
318
+ for key in "${!_dsh_private_stats_messages[@]}"; do
319
+ name="${key%%:*}"
320
+ _dsh_private_stats_ref["msg.$name.count"]=$(( \
321
+ ${_dsh_private_stats_ref["msg.$name.count"]:-0} + 1 ))
322
+ _dsh_private_stats_ref["msg.$name.${key#*:}"]="${_dsh_private_stats_messages[$key]}"
323
+ done
324
+ return 0
325
+ }
326
+
327
+ # Public: write this process's state as a NUL-delimited record file. NUL keeps
328
+ # newlines and backslashes in task descriptions intact.
329
+ dsh::stats::flush() {
330
+ if [[ "${1:-}" == "--help" ]]; then
331
+ _dsh_private_stats_help flush
332
+ return $?
333
+ fi
334
+ if (( $# != 1 )); then
335
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats::flush: expected <dir>'
336
+ return $?
337
+ fi
338
+ local dir="$1"
339
+ mkdir -p "$dir" || {
340
+ dsh::core::fail "$DSH_RC_FAILURE" \
341
+ 'dsh::stats::flush: cannot create directory: %1$s' "$dir"
342
+ return $?
343
+ }
344
+ local file="$dir/${BASHPID:-$$}.stats" key
345
+ {
346
+ for key in "${!_dsh_private_stats_counters[@]}"; do
347
+ printf 'cnt\0%s\0%s\0' "$key" "${_dsh_private_stats_counters[$key]}"
348
+ done
349
+ for key in "${!_dsh_private_stats_lists[@]}"; do
350
+ printf 'list\0%s\0%s\0' "${key%%:*}" "${_dsh_private_stats_lists[$key]}"
351
+ done
352
+ for key in "${!_dsh_private_stats_messages[@]}"; do
353
+ printf 'msg\0%s\0%s\0' "${key%%:*}" "${_dsh_private_stats_messages[$key]}"
354
+ done
355
+ } | dsh::file::atomic_write "$file"
356
+ return $?
357
+ }
358
+
359
+ # Public: merge every stats file in a directory, then remove only the files
360
+ # that were merged. No recursive directory removal.
361
+ dsh::stats::merge() {
362
+ if [[ "${1:-}" == "--help" ]]; then
363
+ _dsh_private_stats_help merge
364
+ return $?
365
+ fi
366
+ if (( $# != 1 )); then
367
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats::merge: expected <dir>'
368
+ return $?
369
+ fi
370
+ local dir="$1" f kind name value
371
+ [[ -d "$dir" ]] || return 0
372
+ for f in "$dir"/*.stats; do
373
+ [[ -f "$f" ]] || continue
374
+ while IFS= read -r -d '' kind; do
375
+ IFS= read -r -d '' name || return "$DSH_RC_FAILURE"
376
+ IFS= read -r -d '' value || return "$DSH_RC_FAILURE"
377
+ case "$kind" in
378
+ cnt)
379
+ if [[ ! "$value" =~ ^-?[0-9]+$ ]]; then
380
+ dsh::core::fail "$DSH_RC_FAILURE" \
381
+ 'dsh::stats::merge: invalid counter in %1$s: %2$s' "$f" "$value"
382
+ return $?
383
+ fi
384
+ _dsh_private_stats_counters["$name"]=$(( \
385
+ ${_dsh_private_stats_counters[$name]:-0} + value ))
386
+ ;;
387
+ list) _dsh_private_stats_lists["${name}:${#_dsh_private_stats_lists[@]}"]="$value" ;;
388
+ msg) _dsh_private_stats_messages["${name}:${#_dsh_private_stats_messages[@]}"]="$value" ;;
389
+ *)
390
+ dsh::core::fail "$DSH_RC_FAILURE" \
391
+ 'dsh::stats::merge: unknown record in %1$s: %2$s' "$f" "$kind"
392
+ return $?
393
+ ;;
394
+ esac
395
+ done < "$f"
396
+ rm -f -- "$f" || {
397
+ dsh::core::fail "$DSH_RC_FAILURE" \
398
+ 'dsh::stats::merge: cannot remove merged file: %1$s' "$f"
399
+ return $?
400
+ }
401
+ done
402
+ return 0
403
+ }
404
+
405
+ # Public: export state as JSON. jq is resolved at call time; a missing tool is
406
+ # a hard error so silent data loss cannot happen.
407
+ dsh::stats::dump_json() {
408
+ if [[ "${1:-}" == "--help" ]]; then
409
+ _dsh_private_stats_help dump_json
410
+ return $?
411
+ fi
412
+ if (( $# < 1 )); then
413
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats::dump_json: expected <file>'
414
+ return $?
415
+ fi
416
+ local target="$1" started=0 finished=0 exit_code=0 stage='' n
417
+ shift
418
+ while (( $# )); do
419
+ case "$1" in
420
+ --started|--finished|--exit-code|--stage)
421
+ if (( $# < 2 )); then
422
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats: %1$s requires a value' "$1"
423
+ return $?
424
+ fi
425
+ case "$1" in
426
+ --started) started="$2" ;;
427
+ --finished) finished="$2" ;;
428
+ --exit-code) exit_code="$2" ;;
429
+ --stage) stage="$2" ;;
430
+ esac
431
+ shift 2
432
+ ;;
433
+ *)
434
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::stats: unknown option: %1$s' "$1"
435
+ return $?
436
+ ;;
437
+ esac
438
+ done
439
+ for n in "$started" "$finished" "$exit_code"; do
440
+ if [[ ! "$n" =~ ^-?[0-9]+$ ]]; then
441
+ dsh::core::fail "$DSH_RC_USAGE" \
442
+ 'dsh::stats::dump_json: numeric option requires an integer: %1$s' "$n"
443
+ return $?
444
+ fi
445
+ done
446
+
447
+ local bin
448
+ if ! bin="$(dsh::core::tool jq 2>/dev/null)"; then
449
+ dsh::core::fail "$DSH_RC_TRANSIENT" \
450
+ 'dsh::stats::dump_json: jq command unavailable'
451
+ return $?
452
+ fi
453
+
454
+ local counters_json='{' key first=1
455
+ for key in "${!_dsh_private_stats_counters[@]}"; do
456
+ (( first )) || counters_json+=','
457
+ first=0
458
+ counters_json+="\"$key\":${_dsh_private_stats_counters[$key]}"
459
+ done
460
+ counters_json+='}'
461
+
462
+ local -a keys=() group name
463
+ for key in "${!_dsh_private_stats_lists[@]}"; do
464
+ keys+=("$key")
465
+ done
466
+ local lists_json='{' lists_first=1 list_json
467
+ local -A seen_lists=()
468
+ for key in "${keys[@]}"; do
469
+ name="${key%%:*}"
470
+ [[ -n "${seen_lists[$name]+x}" ]] && continue
471
+ seen_lists["$name"]=1
472
+ local -a items=()
473
+ for group in "${keys[@]}"; do
474
+ [[ "${group%%:*}" == "$name" ]] && items+=("${_dsh_private_stats_lists[$group]}")
475
+ done
476
+ # jq builds the JSON array so arbitrary text is escaped correctly.
477
+ if (( ${#items[@]} > 0 )); then
478
+ list_json="$(printf '%s\0' "${items[@]}" | "$bin" -Rs \
479
+ 'split("\u0000") | map(select(length > 0))')" || return $?
480
+ else
481
+ list_json='[]'
482
+ fi
483
+ (( lists_first )) || lists_json+=','
484
+ lists_first=0
485
+ lists_json+="\"$name\":$list_json"
486
+ done
487
+ lists_json+='}'
488
+
489
+ keys=()
490
+ for key in "${!_dsh_private_stats_messages[@]}"; do
491
+ keys+=("$key")
492
+ done
493
+ local messages_json='{' messages_first=1 message_json
494
+ local -A seen_messages=()
495
+ for key in "${keys[@]}"; do
496
+ name="${key%%:*}"
497
+ [[ -n "${seen_messages[$name]+x}" ]] && continue
498
+ seen_messages["$name"]=1
499
+ local -a items=()
500
+ for group in "${keys[@]}"; do
501
+ [[ "${group%%:*}" == "$name" ]] && items+=("${_dsh_private_stats_messages[$group]}")
502
+ done
503
+ if (( ${#items[@]} > 0 )); then
504
+ message_json="$(printf '%s\0' "${items[@]}" | "$bin" -Rs \
505
+ 'split("\u0000") | map(select(length > 0))')" || return $?
506
+ else
507
+ message_json='[]'
508
+ fi
509
+ (( messages_first )) || messages_json+=','
510
+ messages_first=0
511
+ messages_json+="\"$name\":$message_json"
512
+ done
513
+ messages_json+='}'
514
+
515
+ local json
516
+ json="$("$bin" -n \
517
+ --argjson started_at "$started" \
518
+ --argjson finished_at "$finished" \
519
+ --argjson exit_code "$exit_code" \
520
+ --arg stage "$stage" \
521
+ --argjson counters "$counters_json" \
522
+ --argjson lists "$lists_json" \
523
+ --argjson messages "$messages_json" \
524
+ '{
525
+ started_at: $started_at,
526
+ finished_at: $finished_at,
527
+ duration: ($finished_at - $started_at),
528
+ exit_code: $exit_code,
529
+ stage: $stage,
530
+ counters: $counters,
531
+ lists: $lists,
532
+ messages: $messages
533
+ }')" || {
534
+ dsh::core::fail "$DSH_RC_FAILURE" \
535
+ 'dsh::stats::dump_json: jq failed to build JSON'
536
+ return $?
537
+ }
538
+ printf '%s\n' "$json" | dsh::file::atomic_write "$target"
539
+ return $?
540
+ }