|
|
@@ -0,0 +1,112 @@
|
|
|
+import argparse
|
|
|
+import subprocess
|
|
|
+import sys
|
|
|
+import time
|
|
|
+from pathlib import Path
|
|
|
+
|
|
|
+import yaml
|
|
|
+
|
|
|
+from validate_desired_state import validate_state
|
|
|
+
|
|
|
+
|
|
|
+def required_ports(state):
|
|
|
+ routing = state["routing"]
|
|
|
+ chain = routing["mod_host_chains"]["guitar"]
|
|
|
+ mix = routing["mix"]
|
|
|
+ return (
|
|
|
+ {chain["source_port"]},
|
|
|
+ {chain["mix_input"], mix["output_port"]},
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+def list_ports(direction, runner):
|
|
|
+ result = runner(
|
|
|
+ ["pw-link", direction],
|
|
|
+ check=True,
|
|
|
+ capture_output=True,
|
|
|
+ text=True,
|
|
|
+ timeout=2,
|
|
|
+ )
|
|
|
+ return {line.strip() for line in result.stdout.splitlines() if line.strip()}
|
|
|
+
|
|
|
+
|
|
|
+def wait_for_required_ports(
|
|
|
+ state,
|
|
|
+ timeout,
|
|
|
+ runner=subprocess.run,
|
|
|
+ sleeper=time.sleep,
|
|
|
+ clock=time.monotonic,
|
|
|
+):
|
|
|
+ required_outputs, required_inputs = required_ports(state)
|
|
|
+ deadline = clock() + timeout
|
|
|
+ last_error = None
|
|
|
+
|
|
|
+ while True:
|
|
|
+ try:
|
|
|
+ outputs = list_ports("-o", runner)
|
|
|
+ inputs = list_ports("-i", runner)
|
|
|
+ last_error = None
|
|
|
+ missing_outputs = required_outputs - outputs
|
|
|
+ missing_inputs = required_inputs - inputs
|
|
|
+ if not missing_outputs and not missing_inputs:
|
|
|
+ return
|
|
|
+ except (OSError, subprocess.CalledProcessError, subprocess.TimeoutExpired) as error:
|
|
|
+ last_error = str(error)
|
|
|
+ missing_outputs = required_outputs
|
|
|
+ missing_inputs = required_inputs
|
|
|
+
|
|
|
+ if clock() >= deadline:
|
|
|
+ details = (
|
|
|
+ f"missing outputs={sorted(missing_outputs)}, "
|
|
|
+ f"missing inputs={sorted(missing_inputs)}"
|
|
|
+ )
|
|
|
+ if last_error:
|
|
|
+ details += f"; last pw-link error: {last_error}"
|
|
|
+ raise RuntimeError(
|
|
|
+ "PipeWire ports required by mod-host did not become ready before "
|
|
|
+ f"the {timeout}s timeout ({details})"
|
|
|
+ )
|
|
|
+ sleeper(min(0.5, max(0.0, deadline - clock())))
|
|
|
+
|
|
|
+
|
|
|
+def main(argv=None):
|
|
|
+ root = Path(__file__).resolve().parents[1]
|
|
|
+ parser = argparse.ArgumentParser(
|
|
|
+ description="Wait until PipeWire exposes the hardware and mixer ports mod-host needs."
|
|
|
+ )
|
|
|
+ parser.add_argument("--state", type=Path, default=root / "config" / "desired-state.yaml")
|
|
|
+ parser.add_argument("--timeout", type=float)
|
|
|
+ args = parser.parse_args(argv)
|
|
|
+
|
|
|
+ try:
|
|
|
+ with args.state.open(encoding="utf-8") as stream:
|
|
|
+ state = yaml.safe_load(stream)
|
|
|
+ errors = validate_state(state)
|
|
|
+ if errors:
|
|
|
+ raise ValueError("invalid desired state: " + "; ".join(errors))
|
|
|
+ timeout = (
|
|
|
+ args.timeout
|
|
|
+ if args.timeout is not None
|
|
|
+ else state["deployment"]["mod_host"]["port_wait_timeout_seconds"]
|
|
|
+ )
|
|
|
+ if timeout <= 0:
|
|
|
+ raise ValueError("--timeout must be positive")
|
|
|
+ wait_for_required_ports(state, timeout)
|
|
|
+ except (
|
|
|
+ OSError,
|
|
|
+ RuntimeError,
|
|
|
+ ValueError,
|
|
|
+ TypeError,
|
|
|
+ KeyError,
|
|
|
+ yaml.YAMLError,
|
|
|
+ subprocess.CalledProcessError,
|
|
|
+ ) as error:
|
|
|
+ print(f"ERROR: {error}", file=sys.stderr)
|
|
|
+ return 1
|
|
|
+
|
|
|
+ print("Required PipeWire ports are ready for mod-host.")
|
|
|
+ return 0
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ raise SystemExit(main())
|