This is a simple script that runs a command, and then sends an OSC message to designated hosts.
from subprocess import run
from pythonosc import udp_client
import sys
args = sys.argv[1:]
info = """
sig_when_done hosts /addr args -- cmd args ...
hosts string:
a,b,c:4000/d:3000
expands to
a:4000 b:4000 c:4000 d:3000
"""
if len(args) == 0:
print(info)
exit()
hosts_string = args[0]
hosts_list = []
try:
for x in hosts_string.split("/"):
xs = x.split(":")
if len(xs) > 2:
raise ValueError(f"Too many :s in {x}")
if len(xs) == 1:
port = 2708
hosts = x
else:
hosts, port = xs
port = int(port)
if port < 1024 or port > 32000:
raise ValueError(f"Invalid port {port}")
hosts = hosts.split(",")
for host in hosts:
hosts_list.append((host,port))
except ValueError:
print(f"Invalid hosts string")
args = args[1:]
osc = []
if "--" in args:
i = args.index("--")
osc = args[:i]
cmd = args[i+1:]
else:
cmd = args
if len(osc) == 0:
addr = "/"
params = []
else:
addr, *params = osc
addr = "/"+addr.lstrip("/")
if len(cmd) == 0:
print(f"No command")
exit(1)
m = run(cmd)
print(f"Return code {m.returncode}")
print(f"For {cmd}")
for h in hosts_list:
ip,port = h
print(f"{addr=} {params=} to {ip}:{port}")
client = udp_client.SimpleUDPClient(ip,port)
client.send_message(addr,params)