Reduce code duplication and move common functionality in a factory
function.
@@ -147,7 +147,13 @@ class PythonRunner(ActionRunner):
stdout = StringIO()
stderr = StringIO()
- def read_and_store_stdout(stream, buff):
+ def make_read_and_store_stream_func(store_line_func):
+ """
+ Factory function which returns a function for reading from a stream (stdout / stderr).
+
+ This function writes read data into a buffer and stores it in a database.
+ """
+ def read_and_store_stream(stream, buff):
try:
while not stream.closed:
line = stream.readline()
@@ -160,31 +166,16 @@ class PythonRunner(ActionRunner):
if ACTION_OUTPUT_RESULT_DELIMITER in line:
continue
- store_execution_stdout_line(execution_db=self.execution,
- action_db=self.action,
+ store_line_func(execution_db=self.execution, action_db=self.action,
line=line)
except RuntimeError:
# process was terminated abruptly
pass
- def read_and_store_stderr(stream, buff):
- try:
- while not stream.closed:
- line = stream.readline()
- if not line:
- break
+ return read_and_store_stream
- buff.write(line)
-
- if ACTION_OUTPUT_RESULT_DELIMITER in line:
- continue
-
- store_execution_stderr_line(execution_db=self.execution,
- action_db=self.action,
- line=line)
- except RuntimeError:
- # process was terminated abruptly
- pass
+ read_and_store_stdout = make_read_and_store_stream_func(store_execution_stdout_line)
+ read_and_store_stderr = make_read_and_store_stream_func(store_execution_stderr_line)
command_string = list2cmdline(args)
LOG.debug('Running command: PATH=%s PYTHONPATH=%s %s' % (env['PATH'], env['PYTHONPATH'],
