Allow the user to specify parameters to send to output_func and termination_func in addition to the regular output string and exit status. The user specified parameters will be pre-pended to the other parameters. This is mainly meant to bypass Python's refusal to pickle instance methods. Signed-off-by: Michael Goldish <mgoldish@xxxxxxxxxx> --- client/tests/kvm/kvm_subprocess.py | 54 ++++++++++++++++++++++++++++++++--- 1 files changed, 49 insertions(+), 5 deletions(-) diff --git a/client/tests/kvm/kvm_subprocess.py b/client/tests/kvm/kvm_subprocess.py index c15e779..df1d562 100644 --- a/client/tests/kvm/kvm_subprocess.py +++ b/client/tests/kvm/kvm_subprocess.py @@ -464,7 +464,9 @@ class kvm_tail(kvm_spawn): """ def __init__(self, command=None, id=None, echo=False, linesep="\n", - termination_func=None, output_func=None, output_prefix=""): + termination_func=None, termination_params=(), + output_func=None, output_params=(), + output_prefix=""): """ Initialize the class and run command as a child process. @@ -479,10 +481,14 @@ class kvm_tail(kvm_spawn): child process by sendline(). @param termination_func: Function to call when the process exits. The function must accept a single exit status parameter. + @param termination_params: Parameters to send to termination_func + before the exit status. @param output_func: Function to call whenever a line of output is available from the STDOUT or STDERR streams of the process. The function must accept a single string parameter. The string does not include the final newline. + @param output_params: Parameters to send to output_func before the + output line. @param output_prefix: String to prepend to lines sent to output_func. """ # Add a reader and a close hook @@ -494,7 +500,9 @@ class kvm_tail(kvm_spawn): # Remember some attributes self.termination_func = termination_func + self.termination_params = termination_params self.output_func = output_func + self.output_params = output_params self.output_prefix = output_prefix # Start the thread in the background @@ -504,7 +512,9 @@ class kvm_tail(kvm_spawn): def __getinitargs__(self): return kvm_spawn.__getinitargs__(self) + (self.termination_func, + self.termination_params, self.output_func, + self.output_params, self.output_prefix) @@ -518,6 +528,16 @@ class kvm_tail(kvm_spawn): self.termination_func = termination_func + def set_termination_params(self, termination_params): + """ + Set the termination_params attribute. See __init__() for details. + + @param termination_params: Parameters to send to termination_func + before the exit status. + """ + self.termination_params = termination_params + + def set_output_func(self, output_func): """ Set the output_func attribute. See __init__() for details. @@ -528,6 +548,16 @@ class kvm_tail(kvm_spawn): self.output_func = output_func + def set_output_params(self, output_params): + """ + Set the output_params attribute. See __init__() for details. + + @param output_params: Parameters to send to output_func before the + output line. + """ + self.output_params = output_params + + def set_output_prefix(self, output_prefix): """ Set the output_prefix attribute. See __init__() for details. @@ -546,7 +576,8 @@ class kvm_tail(kvm_spawn): text = text.decode("utf-8", "replace") # Pass it to output_func try: - self.output_func(text) + params = self.output_params + (text,) + self.output_func(*params) except TypeError: pass @@ -587,7 +618,8 @@ class kvm_tail(kvm_spawn): return print_line("(Process terminated with status %s)" % status) try: - self.termination_func(status) + params = self.termination_params + (status,) + self.termination_func(*params) except TypeError: pass @@ -607,7 +639,9 @@ class kvm_expect(kvm_tail): """ def __init__(self, command=None, id=None, echo=False, linesep="\n", - termination_func=None, output_func=None, output_prefix=""): + termination_func=None, termination_params=(), + output_func=None, output_params=(), + output_prefix=""): """ Initialize the class and run command as a child process. @@ -622,10 +656,14 @@ class kvm_expect(kvm_tail): child process by sendline(). @param termination_func: Function to call when the process exits. The function must accept a single exit status parameter. + @param termination_params: Parameters to send to termination_func + before the exit status. @param output_func: Function to call whenever a line of output is available from the STDOUT or STDERR streams of the process. The function must accept a single string parameter. The string does not include the final newline. + @param output_params: Parameters to send to output_func before the + output line. @param output_prefix: String to prepend to lines sent to output_func. """ # Add a reader @@ -807,7 +845,9 @@ class kvm_shell_session(kvm_expect): """ def __init__(self, command=None, id=None, echo=False, linesep="\n", - termination_func=None, output_func=None, output_prefix="", + termination_func=None, termination_params=(), + output_func=None, output_params=(), + output_prefix="", prompt=r"[\#\$]\s*$", status_test_command="echo $?"): """ Initialize the class and run command as a child process. @@ -823,10 +863,14 @@ class kvm_shell_session(kvm_expect): child process by sendline(). @param termination_func: Function to call when the process exits. The function must accept a single exit status parameter. + @param termination_params: Parameters to send to termination_func + before the exit status. @param output_func: Function to call whenever a line of output is available from the STDOUT or STDERR streams of the process. The function must accept a single string parameter. The string does not include the final newline. + @param output_params: Parameters to send to output_func before the + output line. @param output_prefix: String to prepend to lines sent to output_func. @param prompt: Regular expression describing the shell's prompt line. @param status_test_command: Command to be used for getting the last -- 1.5.4.1 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html