diff --git a/python-api/gstswitch/exception.py b/python-api/gstswitch/exception.py index f4e90a8..25c6e78 100644 --- a/python-api/gstswitch/exception.py +++ b/python-api/gstswitch/exception.py @@ -1,5 +1,10 @@ -class Error(Exception): - """docstring for Error""" +class PathError(Exception): + """docstring for PathError""" + def __init__(self): + pass + + +class ServerProcessError(object): + """docstring for ServerProcessError""" def __init__(self): - super(Error, self).__init__() pass diff --git a/python-api/gstswitch/server.py b/python-api/gstswitch/server.py index 5ed6dd9..61d3e98 100644 --- a/python-api/gstswitch/server.py +++ b/python-api/gstswitch/server.py @@ -3,6 +3,7 @@ import signal import subprocess +from errno import * from exception import * from time import sleep @@ -40,7 +41,8 @@ def run(self, gst_option=''): self.gst_option_string = gst_option print "Starting server" self.proc = self._run_process() - self.pid = self.proc.pid + if self.proc: + self.pid = self.proc.pid # TODO: Sleep time may vary sleep(self.SLEEP_TIME) @@ -59,6 +61,7 @@ def _run_process(self): self.audio_port, self.control_port, self.record_file) + cmd = " ".join(cmd.split()) proc = self._start_process(cmd) return proc @@ -67,16 +70,23 @@ def _start_process(self, cmd): :param cmd: The command which needs to be excecuted :returns: process created + :raises IOError: Fail to open /dev/null (os.devnull) + :raises PathError: Unable to find gst-switch-srv at path specified + :raises ServerProcessError: Creating the gst-switch-srv gives a OS based error. """ print 'Creating process %s' % (cmd) try: - tempf = open(os.devnull, 'w') - process = subprocess.Popen(cmd.split(), stdout=tempf, stderr=tempf, bufsize=-1, shell=False) - except IOError as e: - print "I/O error: os.devnull: {0}".format(e.strerror) + with open(os.devnull, 'w') as tempf: + process = subprocess.Popen(cmd.split(), stdout=tempf, stderr=tempf, bufsize=-1, shell=False) + print cmd + return process + except IOError: + print "cannot open os.devnull" except OSError as e: - print "OS error: {0}: {1}".format(cmd, e.strerror) - return process + if e.errno == ENOENT: + raise PathError + else: + raise ServerProcessError def terminate(self): """Terminates the server diff --git a/python-api/gstswitch/test.py b/python-api/gstswitch/test.py index bafd42b..8df17bd 100755 --- a/python-api/gstswitch/test.py +++ b/python-api/gstswitch/test.py @@ -57,7 +57,7 @@ # time.sleep(0.1) sources.terminate() - output.terminate() +# output.terminate() s.terminate() finally: diff --git a/python-api/gstswitch/test_server.py b/python-api/gstswitch/test_server.py new file mode 100644 index 0000000..c978542 --- /dev/null +++ b/python-api/gstswitch/test_server.py @@ -0,0 +1,19 @@ +from server import Server +import pytest +from exception import * + + +class TestRun(object): + + def test_invalid_path(self): + path = '/usr/' + s = Server(path=path) + with pytest.raises(PathError): + s.run() + + def test_normal(self): + path = '/home/hyades/gst/master/gstreamer/tools/' + s = Server(path=path) + s.run() + assert s.proc is not None + s.terminate()