543f10e662
run-playlist.sh and import-track.sh had the same beets->#EXTM3U block, but import-track.sh wrote the file non-atomically. Move it to pipeline/lib/m3u.sh as regen_m3u <name> <out>, sourced by both. The helper always writes atomically (temp + rename), so a reader like Navidrome never sees a partial file and a stray wrong-owner leftover .m3u8 is replaced without a chown. Returns 0 always (empty result is a warning), so it's safe as a standalone call under set -e. Verified: tests/test_m3u.py (atomic write with tracks, no file when empty; 44 tests green) plus a live regen against the real library (techno -> 229 tracks, no temp leftover). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
38 lines
1.3 KiB
Bash
38 lines
1.3 KiB
Bash
#!/bin/bash
|
|
# Sourced helper: regenerate one playlist's .m3u8 from the beets library.
|
|
#
|
|
# regen_m3u <playlist_name> <out_path>
|
|
#
|
|
# Writes atomically (temp file in the same dir, then rename over the target):
|
|
# rename(2) is atomic for a reader like Navidrome, and needs write access only
|
|
# on the directory, so it also replaces a stray wrong-owner leftover .m3u8 (e.g.
|
|
# from a pre-container run) without a host-side chown.
|
|
#
|
|
# Relies on the caller's log() function and $LOG. Always returns 0 (an empty
|
|
# result is a warning, not a failure), so it is safe to call as a standalone
|
|
# statement under set -euo pipefail.
|
|
regen_m3u() {
|
|
local playlist_name="$1"
|
|
local out="$2"
|
|
mkdir -p "$(dirname "$out")"
|
|
|
|
local beet_output
|
|
# || true: beet ls exits non-zero on no match, which would trip set -e.
|
|
beet_output=$(beet ls -f '$path' "grouping:${playlist_name}" 2>>"$LOG" || true)
|
|
local track_count=0
|
|
[[ -n "$beet_output" ]] && track_count=$(printf '%s\n' "$beet_output" | wc -l)
|
|
|
|
if [[ "$track_count" -gt 0 ]]; then
|
|
local tmp="${out}.tmp.$$"
|
|
{
|
|
echo "#EXTM3U"
|
|
echo "$beet_output"
|
|
} > "$tmp"
|
|
mv "$tmp" "$out"
|
|
log "M3U updated with $track_count tracks"
|
|
else
|
|
log "WARNING: beet list returned no tracks for grouping:${playlist_name} — not updating M3U"
|
|
fi
|
|
return 0
|
|
}
|