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,414 @@
1
+ #!/usr/bin/env bash
2
+ # darwin-shell lock: process-exclusive locks built on flock.
3
+ # A lock lives on the opened inode, not on its path: deleting a lock file
4
+ # while somebody holds it silently breaks mutual exclusion. This module never
5
+ # removes lock files during acquire/release; only prune_stale may do that,
6
+ # and only after an explicit flock probe with no concurrent holders.
7
+ # Public functions never exit, install a trap or change shell options.
8
+
9
+ # Dependencies sit below the guard so a failed dependency is retryable on the
10
+ # next source attempt.
11
+ if ! declare -F dsh::i18n::t >/dev/null 2>&1; then
12
+ if declare -F dsh::use >/dev/null 2>&1; then
13
+ dsh::use i18n || return $?
14
+ else
15
+ _dsh_private_lock_self="${BASH_SOURCE[0]}"
16
+ source "${_dsh_private_lock_self%/*}/i18n.sh" || return $?
17
+ unset _dsh_private_lock_self
18
+ fi
19
+ fi
20
+ if ! declare -F dsh::core::fail >/dev/null 2>&1; then
21
+ if declare -F dsh::use >/dev/null 2>&1; then
22
+ dsh::use core || return $?
23
+ else
24
+ _dsh_private_lock_self="${BASH_SOURCE[0]}"
25
+ source "${_dsh_private_lock_self%/*}/core.sh" || return $?
26
+ unset _dsh_private_lock_self
27
+ fi
28
+ fi
29
+
30
+ if [[ -n "${_dsh_private_lock_sourced:-}" ]]; then
31
+ return 0
32
+ fi
33
+ _dsh_private_lock_sourced=1
34
+ readonly _dsh_private_lock_sourced
35
+
36
+ # Fds handed out by acquire, tracked so release --all can close them.
37
+ declare -ga _dsh_private_lock_fds=()
38
+ _dsh_private_lock_opt_dir=''
39
+ _dsh_private_lock_opt_timeout=0
40
+ _dsh_private_lock_opt_into=''
41
+ declare -ga _dsh_private_lock_remaining=()
42
+
43
+ _dsh_private_lock_help() {
44
+ case "${1:-}" in
45
+ acquire)
46
+ dsh::i18n::t 'Usage: dsh::lock::acquire <name> [--dir DIR] [--timeout SEC] [--into VAR]'
47
+ printf '\n'
48
+ dsh::i18n::t 'Acquire an exclusive lock; return 7 when another process holds it.'
49
+ ;;
50
+ release)
51
+ dsh::i18n::t 'Usage: dsh::lock::release <fd>|--all'
52
+ printf '\n'
53
+ dsh::i18n::t 'Close a lock fd; --all closes every fd acquired by this module.'
54
+ ;;
55
+ with_lock)
56
+ dsh::i18n::t 'Usage: dsh::lock::with_lock <name> [--dir DIR] [--timeout SEC] -- <cmd> [args...]'
57
+ printf '\n'
58
+ dsh::i18n::t 'Run <cmd> under the lock, then release it even when the command fails.'
59
+ ;;
60
+ prune_stale)
61
+ dsh::i18n::t 'Usage: dsh::lock::prune_stale [days] [--dir DIR] [--dry-run]'
62
+ printf '\n'
63
+ dsh::i18n::t 'Remove old lock files after a flock probe; run only with no concurrent holders.'
64
+ ;;
65
+ *)
66
+ dsh::i18n::t 'dsh::lock: no help for %1$s' "${1:-<empty>}" >&2
67
+ return "$DSH_RC_USAGE"
68
+ ;;
69
+ esac
70
+ }
71
+
72
+ # Parse the options shared by acquire and with_lock. Results land in private
73
+ # module variables; -- ends option parsing and keeps the rest verbatim.
74
+ _dsh_private_lock_parse_opts() {
75
+ _dsh_private_lock_opt_dir=''
76
+ _dsh_private_lock_opt_timeout=0
77
+ _dsh_private_lock_opt_into=''
78
+ _dsh_private_lock_remaining=()
79
+ while (( $# )); do
80
+ case "$1" in
81
+ --dir)
82
+ if (( $# < 2 )); then
83
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock: --dir requires a value'
84
+ return $?
85
+ fi
86
+ _dsh_private_lock_opt_dir="$2"
87
+ shift 2
88
+ ;;
89
+ --timeout)
90
+ if (( $# < 2 )) || [[ ! "$2" =~ ^[0-9]+$ ]]; then
91
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock: --timeout requires a non-negative integer: %1$s' "${2:-}"
92
+ return $?
93
+ fi
94
+ _dsh_private_lock_opt_timeout="$2"
95
+ shift 2
96
+ ;;
97
+ --into)
98
+ if (( $# < 2 )) || [[ ! "$2" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
99
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock: --into requires a safe variable name: %1$s' "${2:-}"
100
+ return $?
101
+ fi
102
+ case "$2" in
103
+ BASH_ENV|ENV|IFS|PATH|PS4|SHELLOPTS|_dsh_private_lock_*)
104
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock: --into requires a safe variable name: %1$s' "$2"
105
+ return $?
106
+ ;;
107
+ esac
108
+ _dsh_private_lock_opt_into="$2"
109
+ shift 2
110
+ ;;
111
+ --)
112
+ shift
113
+ _dsh_private_lock_remaining=("$@")
114
+ return 0
115
+ ;;
116
+ *)
117
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock: unknown option: %1$s' "$1"
118
+ return $?
119
+ ;;
120
+ esac
121
+ done
122
+ return 0
123
+ }
124
+
125
+ _dsh_private_lock_dir() {
126
+ local dir="${_dsh_private_lock_opt_dir:-${DARWIN_SH_LOCK_DIR:-${TMPDIR:-/tmp}/darwin-shell-locks}}"
127
+ dir="${dir%/}"
128
+ [[ -n "$dir" ]] || dir='/'
129
+ printf '%s\n' "$dir"
130
+ }
131
+
132
+ _dsh_private_lock_best_effort() {
133
+ case "${DARWIN_SH_LOCK_BEST_EFFORT:-0}" in
134
+ 1|true|TRUE|yes|YES|on|ON) return 0 ;;
135
+ *) return 1 ;;
136
+ esac
137
+ }
138
+
139
+ # Return 0 when the caller opted into degraded operation. Strict mode (the
140
+ # default) returns 1 so the caller reports the failure instead of pretending
141
+ # mutual exclusion still works.
142
+ _dsh_private_lock_degrade() {
143
+ local key="$1" msgid="$2"
144
+ shift 2
145
+ if _dsh_private_lock_best_effort; then
146
+ dsh::core::warn_once "$key" "$msgid" "$@"
147
+ return 0
148
+ fi
149
+ return 1
150
+ }
151
+
152
+ _dsh_private_lock_resolve_flock() {
153
+ local bin
154
+ if ! bin="$(dsh::core::tool flock 2>/dev/null)"; then
155
+ if _dsh_private_lock_degrade 'lock-no-flock' \
156
+ 'dsh::lock: flock is unavailable; mutual exclusion disabled'; then
157
+ printf '%s\n' ''
158
+ return 0
159
+ fi
160
+ dsh::core::fail "$DSH_RC_TRANSIENT" 'dsh::lock: flock is unavailable; mutual exclusion disabled'
161
+ return $?
162
+ fi
163
+ printf '%s\n' "$bin"
164
+ }
165
+
166
+ # Public: acquire an exclusive lock. The fd stays open in the caller's shell
167
+ # until release; the kernel releases it automatically when the process exits.
168
+ # 0 = acquired, 7 = busy, 124 = timeout, 3 = flock unavailable (strict mode),
169
+ # 2 = invalid arguments.
170
+ dsh::lock::acquire() {
171
+ if [[ "${1:-}" == "--help" ]]; then
172
+ _dsh_private_lock_help acquire
173
+ return $?
174
+ fi
175
+ if (( $# < 1 )); then
176
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock::acquire: missing <name>'
177
+ return $?
178
+ fi
179
+ local name="$1"
180
+ shift
181
+ if [[ ! "$name" =~ ^[A-Za-z0-9_.-]+$ ]]; then
182
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock::acquire: invalid name: %1$s' "$name"
183
+ return $?
184
+ fi
185
+ _dsh_private_lock_parse_opts "$@" || return $?
186
+
187
+ local flock_bin dir
188
+ flock_bin="$(_dsh_private_lock_resolve_flock)" || return $?
189
+ if [[ -z "$flock_bin" ]]; then
190
+ return 0
191
+ fi
192
+ dir="$(_dsh_private_lock_dir)" || return $?
193
+
194
+ if [[ ! -d "$dir" ]] && ! mkdir -p -- "$dir" 2>/dev/null; then
195
+ if _dsh_private_lock_degrade 'lock-dir' \
196
+ 'dsh::lock: cannot create lock directory: %1$s' "$dir"; then
197
+ return 0
198
+ fi
199
+ dsh::core::fail "$DSH_RC_TRANSIENT" 'dsh::lock: cannot create lock directory: %1$s' "$dir"
200
+ return $?
201
+ fi
202
+ if [[ ! -w "$dir" ]]; then
203
+ if _dsh_private_lock_degrade 'lock-dir' \
204
+ 'dsh::lock: lock directory is not writable: %1$s' "$dir"; then
205
+ return 0
206
+ fi
207
+ dsh::core::fail "$DSH_RC_TRANSIENT" 'dsh::lock: lock directory is not writable: %1$s' "$dir"
208
+ return $?
209
+ fi
210
+
211
+ local lock_file="$dir/${name}.lock"
212
+ # Private-prefixed name: --into rejects this prefix, so a nameref can never
213
+ # resolve back to this local variable and shadow the caller's target.
214
+ local _dsh_private_lock_fd=''
215
+ if ! exec {_dsh_private_lock_fd}>"$lock_file" 2>/dev/null; then
216
+ if _dsh_private_lock_degrade 'lock-open' \
217
+ 'dsh::lock: cannot open lock file: %1$s' "$lock_file"; then
218
+ return 0
219
+ fi
220
+ dsh::core::fail "$DSH_RC_TRANSIENT" 'dsh::lock: cannot open lock file: %1$s' "$lock_file"
221
+ return $?
222
+ fi
223
+
224
+ local -a args=()
225
+ if (( _dsh_private_lock_opt_timeout > 0 )); then
226
+ args=(-w "$_dsh_private_lock_opt_timeout" "$_dsh_private_lock_fd")
227
+ else
228
+ args=(-n "$_dsh_private_lock_fd")
229
+ fi
230
+ # Capture $? from the command itself: an `if` without else reports 0 when
231
+ # its condition fails, which would turn EWOULDBLOCK into a transient error.
232
+ "$flock_bin" "${args[@]}"
233
+ local rc=$?
234
+ if (( rc == 0 )); then
235
+ _dsh_private_lock_fds+=("$_dsh_private_lock_fd")
236
+ if [[ -n "$_dsh_private_lock_opt_into" ]]; then
237
+ local -n _dsh_private_lock_ref="$_dsh_private_lock_opt_into"
238
+ _dsh_private_lock_ref="$_dsh_private_lock_fd"
239
+ fi
240
+ return 0
241
+ fi
242
+
243
+ exec {_dsh_private_lock_fd}>&- 2>/dev/null || true
244
+ if (( rc == 1 )); then
245
+ if (( _dsh_private_lock_opt_timeout > 0 )); then
246
+ dsh::core::fail "$DSH_RC_TIMEOUT" 'dsh::lock::acquire: timed out waiting for: %1$s' "$name"
247
+ else
248
+ dsh::core::fail "$DSH_RC_LOCK_BUSY" 'dsh::lock::acquire: already held by another process: %1$s' "$name"
249
+ fi
250
+ else
251
+ dsh::core::fail "$DSH_RC_TRANSIENT" 'dsh::lock: flock failed for: %1$s' "$name"
252
+ fi
253
+ return $?
254
+ }
255
+
256
+ # Public: close a lock fd. Closing an already-closed fd is harmless.
257
+ dsh::lock::release() {
258
+ if [[ "${1:-}" == "--help" ]]; then
259
+ _dsh_private_lock_help release
260
+ return $?
261
+ fi
262
+ if (( $# != 1 )); then
263
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock::release: expected <fd> or --all'
264
+ return $?
265
+ fi
266
+ local fd="$1" item
267
+ local -a kept=()
268
+ if [[ "$fd" == "--all" ]]; then
269
+ for item in "${_dsh_private_lock_fds[@]}"; do
270
+ exec {item}>&- 2>/dev/null || true
271
+ done
272
+ _dsh_private_lock_fds=()
273
+ return 0
274
+ fi
275
+ if [[ ! "$fd" =~ ^[0-9]+$ ]]; then
276
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock::release: invalid fd: %1$s' "$fd"
277
+ return $?
278
+ fi
279
+ exec {fd}>&- 2>/dev/null || true
280
+ for item in "${_dsh_private_lock_fds[@]}"; do
281
+ [[ "$item" == "$fd" ]] || kept+=("$item")
282
+ done
283
+ _dsh_private_lock_fds=("${kept[@]}")
284
+ return 0
285
+ }
286
+
287
+ # Public: run a command under a lock and always release afterwards.
288
+ # The command's own status becomes this function's status.
289
+ dsh::lock::with_lock() {
290
+ if [[ "${1:-}" == "--help" ]]; then
291
+ _dsh_private_lock_help with_lock
292
+ return $?
293
+ fi
294
+ if (( $# < 1 )); then
295
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock::with_lock: missing <name>'
296
+ return $?
297
+ fi
298
+ local name="$1"
299
+ shift
300
+ if [[ ! "$name" =~ ^[A-Za-z0-9_.-]+$ ]]; then
301
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock::acquire: invalid name: %1$s' "$name"
302
+ return $?
303
+ fi
304
+
305
+ local -a acquire_args=()
306
+ while (( $# )); do
307
+ case "$1" in
308
+ --dir|--timeout)
309
+ if (( $# < 2 )); then
310
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock: %1$s requires a value' "$1"
311
+ return $?
312
+ fi
313
+ acquire_args+=("$1" "$2")
314
+ shift 2
315
+ ;;
316
+ --into)
317
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock::with_lock: --into is not supported'
318
+ return $?
319
+ ;;
320
+ --)
321
+ shift
322
+ break
323
+ ;;
324
+ *)
325
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock: unknown option: %1$s' "$1"
326
+ return $?
327
+ ;;
328
+ esac
329
+ done
330
+ if (( $# == 0 )); then
331
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock::with_lock: missing command after --'
332
+ return $?
333
+ fi
334
+ local -a command=("$@")
335
+
336
+ local fd=''
337
+ dsh::lock::acquire "$name" "${acquire_args[@]}" --into fd || return $?
338
+ local rc=0
339
+ "${command[@]}" || rc=$?
340
+ dsh::lock::release "$fd" || true
341
+ return "$rc"
342
+ }
343
+
344
+ # Public: remove lock files older than <days> (default 7) after checking
345
+ # with flock -n that nobody holds them. The probe/remove pair is not atomic:
346
+ # run this only when no concurrent holders exist. Dry-run prints candidates.
347
+ dsh::lock::prune_stale() {
348
+ if [[ "${1:-}" == "--help" ]]; then
349
+ _dsh_private_lock_help prune_stale
350
+ return $?
351
+ fi
352
+ local days=7
353
+ if (( $# > 0 )) && [[ "$1" =~ ^[0-9]+$ ]]; then
354
+ days="$1"
355
+ shift
356
+ fi
357
+ local dry=0 dir_override=''
358
+ while (( $# )); do
359
+ case "$1" in
360
+ --dry-run)
361
+ dry=1
362
+ shift
363
+ ;;
364
+ --dir)
365
+ if (( $# < 2 )); then
366
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock::prune_stale: --dir requires a value'
367
+ return $?
368
+ fi
369
+ dir_override="$2"
370
+ shift 2
371
+ ;;
372
+ *)
373
+ dsh::core::fail "$DSH_RC_USAGE" 'dsh::lock::prune_stale: unknown option: %1$s' "$1"
374
+ return $?
375
+ ;;
376
+ esac
377
+ done
378
+
379
+ local flock_bin
380
+ flock_bin="$(_dsh_private_lock_resolve_flock)" || return $?
381
+ if [[ -z "$flock_bin" ]]; then
382
+ return 0
383
+ fi
384
+ _dsh_private_lock_opt_dir="$dir_override"
385
+ local dir
386
+ dir="$(_dsh_private_lock_dir)" || return $?
387
+ if [[ ! -d "$dir" ]]; then
388
+ printf '%s\n' 0
389
+ return 0
390
+ fi
391
+
392
+ local -a entries=()
393
+ local entry
394
+ while IFS= read -r -d '' entry; do
395
+ entries+=("$entry")
396
+ done < <(find "$dir" -mindepth 1 -maxdepth 1 -type f -name '*.lock' -mtime "+$days" -print0 2>/dev/null)
397
+
398
+ if (( dry )) || dsh::core::dry_run; then
399
+ for entry in "${entries[@]}"; do
400
+ printf '%s\n' "$entry"
401
+ done
402
+ return 0
403
+ fi
404
+
405
+ local removed=0
406
+ for entry in "${entries[@]}"; do
407
+ if "$flock_bin" -n "$entry" true 2>/dev/null; then
408
+ if rm -f -- "$entry" 2>/dev/null; then
409
+ removed=$((removed + 1))
410
+ fi
411
+ fi
412
+ done
413
+ printf '%s\n' "$removed"
414
+ }