From 6d7d88e799e8e3af553130a4fb0043db9c17e1da Mon Sep 17 00:00:00 2001 From: wlkns-dev Date: Mon, 2 Nov 2026 12:22:07 +0100 Subject: [PATCH] feat(cli): add pack and unpack commands for .songpack container with Section 12 security --- src/songsplit/cli.py | 28 ++++++++++++++++++++++++++++ src/songsplit/container.py | 26 ++++++-------------------- tests/test_container.py | 1 - 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/songsplit/cli.py b/src/songsplit/cli.py index be45b73..71054a0 100644 --- a/src/songsplit/cli.py +++ b/src/songsplit/cli.py @@ -81,6 +81,34 @@ def eval_cmd( run_evaluation(testset_config) +@app.command(name="pack") +def pack_cmd( + song_dir: Path = typer.Argument(..., help="Songpack directory to pack", exists=True), + output: Path | None = typer.Option(None, "-o", "--output", help="Output .songpack file path"), + variant: str = typer.Option( + "full", "--variant", help="Package variant: 'full' (with stems) or 'data' (without stems)" + ), +) -> None: + """Pack a songpack directory into a .songpack archive.""" + from songsplit.container import pack_songpack + + res = pack_songpack(song_dir, output_path=output, variant=variant) # type: ignore[arg-type] + print(str(res)) + + +@app.command(name="unpack") +def unpack_cmd( + songpack_file: Path = typer.Argument(..., help="Path to .songpack archive", exists=True), + output_dir: Path | None = typer.Option(None, "-o", "--output", help="Destination folder for unpacked songpack"), +) -> None: + """Securely unpack a .songpack archive into a song directory.""" + from songsplit.container import unpack_songpack + + dest = output_dir or songpack_file.with_suffix("") + res = unpack_songpack(songpack_file, dest) + print(str(res)) + + @app.command() def doctor() -> None: """Check environment, ffmpeg installation, GPUs, and PyTorch availability.""" diff --git a/src/songsplit/container.py b/src/songsplit/container.py index 75a4621..5922241 100644 --- a/src/songsplit/container.py +++ b/src/songsplit/container.py @@ -76,9 +76,7 @@ def is_whitelisted(rel_path: str) -> bool: return any(pattern.match(rel_path) for pattern in WHITELIST_PATTERNS) -def build_manifest_for_dir( - song_dir: Path, variant: Literal["full", "data"] = "full" -) -> ManifestDocument: +def build_manifest_for_dir(song_dir: Path, variant: Literal["full", "data"] = "full") -> ManifestDocument: """Scan song directory and generate or update manifest.json.""" manifest_path = song_dir / "manifest.json" now_iso = datetime.now(UTC).isoformat() @@ -189,11 +187,7 @@ def pack_songpack( if not file_abs.is_file(): continue - compress_type = ( - zipfile.ZIP_STORED - if item.path.endswith((".wav", ".flac")) - else zipfile.ZIP_DEFLATED - ) + compress_type = zipfile.ZIP_STORED if item.path.endswith((".wav", ".flac")) else zipfile.ZIP_DEFLATED zf.write(file_abs, arcname=item.path, compress_type=compress_type) temp_zip.replace(output_path) @@ -222,9 +216,7 @@ def unpack_songpack(songpack_path: Path, output_dir: Path) -> Path: with zipfile.ZipFile(songpack_path, "r") as zf: infolist = zf.infolist() if len(infolist) > MAX_ENTRIES: - raise SongpackSecurityError( - f"Too many archive entries: {len(infolist)} > {MAX_ENTRIES}" - ) + raise SongpackSecurityError(f"Too many archive entries: {len(infolist)} > {MAX_ENTRIES}") total_uncompressed_bytes = 0 track_count = 0 @@ -254,19 +246,13 @@ def unpack_songpack(songpack_path: Path, output_dir: Path) -> Path: if info.compress_size > 0: ratio = info.file_size / info.compress_size if ratio > MAX_COMPRESSION_RATIO: - raise SongpackSecurityError( - f"Compression ratio too high ({ratio:.1f}:1) for {rel_path}" - ) + raise SongpackSecurityError(f"Compression ratio too high ({ratio:.1f}:1) for {rel_path}") # Size limits per type if rel_path.endswith(".json") and info.file_size > MAX_JSON_SIZE: - raise SongpackSecurityError( - f"JSON file too large: {info.file_size} > {MAX_JSON_SIZE}" - ) + raise SongpackSecurityError(f"JSON file too large: {info.file_size} > {MAX_JSON_SIZE}") if rel_path.startswith("stems/") and info.file_size > MAX_STEM_SIZE: - raise SongpackSecurityError( - f"Stem audio too large: {info.file_size} > {MAX_STEM_SIZE}" - ) + raise SongpackSecurityError(f"Stem audio too large: {info.file_size} > {MAX_STEM_SIZE}") total_uncompressed_bytes += info.file_size if total_uncompressed_bytes > MAX_TOTAL_SIZE: diff --git a/tests/test_container.py b/tests/test_container.py index 122b9ab..6a7817a 100644 --- a/tests/test_container.py +++ b/tests/test_container.py @@ -1,6 +1,5 @@ """Tests for songpack container packing and secure unpacking.""" -import json import zipfile from pathlib import Path