feat(cli): add pack and unpack commands for .songpack container with Section 12 security

This commit is contained in:
2026-11-02 12:22:07 +01:00
parent e73c4ec328
commit 6d7d88e799
3 changed files with 34 additions and 21 deletions

View File

@ -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."""

View File

@ -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: