/
/
/
1"""Helpers/utils for the AirPlay Receiver plugin."""
2
3from __future__ import annotations
4
5import os
6import platform
7import shutil
8
9from music_assistant.helpers.process import check_output
10
11
12async def get_shairport_sync_binary() -> str:
13 """Find the shairport-sync binary (bundled or system-installed)."""
14
15 async def check_shairport_sync(shairport_path: str) -> str | None:
16 """Check if shairport-sync binary is valid."""
17 try:
18 returncode, _ = await check_output(shairport_path, "--version")
19 if returncode == 0:
20 return shairport_path
21 return None
22 except OSError:
23 return None
24
25 # First, check if bundled binary exists
26 base_path = os.path.join(os.path.dirname(__file__), "bin")
27 system = platform.system().lower().replace("darwin", "macos")
28 architecture = platform.machine().lower()
29
30 if shairport_binary := await check_shairport_sync(
31 os.path.join(base_path, f"shairport-sync-{system}-{architecture}")
32 ):
33 return shairport_binary
34
35 # If no bundled binary, check system PATH
36 if system_binary := shutil.which("shairport-sync"):
37 if shairport_binary := await check_shairport_sync(system_binary):
38 return shairport_binary
39
40 msg = (
41 f"Unable to locate shairport-sync for {system}/{architecture}. "
42 "Please install shairport-sync on your system or provide a bundled binary."
43 )
44 raise RuntimeError(msg)
45