| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
import os |
| 4 |
from time import sleep |
| 5 |
import subprocess |
| 6 |
import atexit |
| 7 |
|
| 8 |
class Process(object): |
| 9 |
processes = [] |
| 10 |
@classmethod |
| 11 |
def new(clazz, *args, **kw): |
| 12 |
clazz.processes.append(subprocess.Popen(*args, **kw)) |
| 13 |
return clazz.processes[-1] |
| 14 |
|
| 15 |
@classmethod |
| 16 |
def atexit(clazz): |
| 17 |
for p in reversed(clazz.processes): |
| 18 |
print "child %s" % p.pid |
| 19 |
if False: |
| 20 |
slp = subprocess.Popen(['/usr/bin/sleep', '10000']) |
| 21 |
print "wait on %d" % slp.pid |
| 22 |
slp.wait() |
| 23 |
if len(clazz.processes) == 0: |
| 24 |
return |
| 25 |
for p in reversed(clazz.processes): |
| 26 |
print "kill %s" % p.pid |
| 27 |
try: |
| 28 |
p.kill() |
| 29 |
except: |
| 30 |
pass |
| 31 |
if not any(p.poll() for p in clazz.processes): |
| 32 |
return |
| 33 |
sleep(1) |
| 34 |
for p in reversed(clazz.processes): |
| 35 |
if not p.poll(): |
| 36 |
print "terminate %s" % p.pid |
| 37 |
try: |
| 38 |
p.terminate() |
| 39 |
except: |
| 40 |
pass |
| 41 |
|
| 42 |
atexit.register(Process.atexit) |
| 43 |
|
| 44 |
def which(prog): |
| 45 |
for path_element in os.environ['PATH'].split(':'): |
| 46 |
candidate = os.path.join(path_element, prog) |
| 47 |
if os.path.exists(candidate): |
| 48 |
return candidate |
| 49 |
return None |
| 50 |
|
| 51 |
client_executable = which('remote-viewer') |
| 52 |
if not client_executable: |
| 53 |
raise SystemExit('missing remote-viewer in path') |
| 54 |
|
| 55 |
def launch_xspice(port): |
| 56 |
basedir = '/tmp/xspice_test_audio' |
| 57 |
if not os.path.exists(basedir): |
| 58 |
os.mkdir(basedir) |
| 59 |
assert(os.path.exists(basedir)) |
| 60 |
xspice = Process.new(['../scripts/Xspice', '--port', str(port), '--auto', '--audio-fifo-dir', basedir, '--disable-ticketing', ':15.0']) |
| 61 |
xspice.audio_fifo_dir = basedir |
| 62 |
return xspice |
| 63 |
|
| 64 |
def launch_client(port): |
| 65 |
client = Process.new([client_executable, 'spice://localhost:%s' % port]) |
| 66 |
return client |
| 67 |
|
| 68 |
if __name__ == '__main__': |
| 69 |
launch_xspice(port=8000) |