Support connecting to existing pipes (and not touching them)

This commit is contained in:
Kristóf Tóth 2019-07-30 14:44:05 +02:00
parent acd8fde005
commit 499ce8af76
4 changed files with 52 additions and 14 deletions

View File

@ -5,11 +5,18 @@ from .pipe_writer_server import PipeWriterServer
class PipeIOServer(PipeReaderServer, PipeWriterServer): class PipeIOServer(PipeReaderServer, PipeWriterServer):
# pylint: disable=abstract-method # pylint: disable=abstract-method
def __init__(self, in_pipe, out_pipe, permissions=DEFAULT_PERMISSIONS): def __init__(
self,
in_pipe,
out_pipe,
permissions=DEFAULT_PERMISSIONS,
manage_pipes=True
):
super().__init__( super().__init__(
in_pipe=in_pipe, in_pipe=in_pipe,
out_pipe=out_pipe, out_pipe=out_pipe,
permissions=permissions permissions=permissions,
manage_pipes=manage_pipes
) )
def _io_threads(self): def _io_threads(self):

View File

@ -4,11 +4,19 @@ from .pipe_reader_thread import PipeReaderThread
class PipeReaderServer(PipeIOThread): class PipeReaderServer(PipeIOThread):
def __init__(self, in_pipe, permissions=DEFAULT_PERMISSIONS, **kwargs): def __init__(
self,
in_pipe,
permissions=DEFAULT_PERMISSIONS,
manage_pipes=True,
**kwargs
):
super().__init__(**kwargs) super().__init__(**kwargs)
self._in_pipe = in_pipe
Pipe(self.in_pipe).recreate(permissions)
self._reader_thread = None self._reader_thread = None
self._manage_pipes = manage_pipes
self._in_pipe = in_pipe
if self._manage_pipes:
Pipe(self.in_pipe).recreate(permissions)
@property @property
def in_pipe(self): def in_pipe(self):
@ -27,4 +35,5 @@ class PipeReaderServer(PipeIOThread):
def stop(self): def stop(self):
super().stop() super().stop()
Pipe(self.in_pipe).remove() if self._manage_pipes:
Pipe(self.in_pipe).remove()

View File

@ -4,11 +4,19 @@ from .pipe_writer_thread import PipeWriterThread
class PipeWriterServer(PipeIOThread): class PipeWriterServer(PipeIOThread):
def __init__(self, out_pipe, permissions=DEFAULT_PERMISSIONS, **kwargs): def __init__(
self,
out_pipe,
permissions=DEFAULT_PERMISSIONS,
manage_pipes=True,
**kwargs
):
super().__init__(**kwargs) super().__init__(**kwargs)
self._out_pipe = out_pipe
Pipe(self.out_pipe).recreate(permissions)
self._writer_thread = None self._writer_thread = None
self._manage_pipes = manage_pipes
self._out_pipe = out_pipe
if self._manage_pipes:
Pipe(self.out_pipe).recreate(permissions)
@property @property
def out_pipe(self): def out_pipe(self):
@ -26,4 +34,5 @@ class PipeWriterServer(PipeIOThread):
def stop(self): def stop(self):
super().stop() super().stop()
Pipe(self.out_pipe).remove() if self._manage_pipes:
Pipe(self.out_pipe).remove()

View File

@ -64,10 +64,23 @@ class IOPipes:
return self.out_pipe.readline().rstrip(b'\n') return self.out_pipe.readline().rstrip(b'\n')
def test_run_creates_pipes(io_pipes): def pipes_exist(*paths):
for path in (io_pipes.in_pipe_path, io_pipes.out_pipe_path): predicate = lambda path: exists(path) and S_ISFIFO(stat(path).st_mode)
assert exists(path) return all(predicate(path) for path in paths)
assert S_ISFIFO(stat(path).st_mode)
def test_manage_pipes():
pipe_io = PipeIOServer(*get_test_init_params(), manage_pipes=True)
assert pipes_exist(pipe_io.in_pipe, pipe_io.out_pipe)
pipe_io.stop()
assert not pipes_exist(pipe_io.in_pipe, pipe_io.out_pipe)
def test_no_manage_pipes():
pipe_io = PipeIOServer(*get_test_init_params(), manage_pipes=False)
assert not pipes_exist(pipe_io.in_pipe, pipe_io.out_pipe)
pipe_io.stop()
assert not pipes_exist(pipe_io.in_pipe, pipe_io.out_pipe)
def test_stop(): def test_stop():