]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
vstart_runner: reuse code in LocalRemoteProcess 40412/head
authorRishabh Dave <ridave@redhat.com>
Tue, 8 Dec 2020 08:40:43 +0000 (14:10 +0530)
committerRishabh Dave <ridave@redhat.com>
Thu, 25 Mar 2021 14:58:05 +0000 (20:28 +0530)
Reduce duplication in LocalRemoteProcess.wait() and
LocalRemoteProcess.finished() by moving the common code to a separate
method and then using that separate method instead.

Signed-off-by: Rishabh Dave <ridave@redhat.com>
qa/tasks/vstart_runner.py

index 982dd6ed4ed01dc23cf265e49efce42f233aeb68..827602629ab2ac47c638df69e0f4031191a86494 100644 (file)
@@ -182,23 +182,15 @@ class LocalRemoteProcess(object):
         self.check_status = check_status
         self.exitstatus = self.returncode = None
 
-    def wait(self):
-        if self.finished:
-            # Avoid calling communicate() on a dead process because it'll
-            # give you stick about std* already being closed
-            if self.check_status and self.exitstatus != 0:
-                raise CommandFailedError(self.args, self.exitstatus)
-            else:
-                return
-
-        out, err = self.subproc.communicate()
-        out, err = rm_nonascii_chars(out), rm_nonascii_chars(err)
+    def _write_stdout(self, out):
         if isinstance(self.stdout, StringIO):
             self.stdout.write(out.decode(errors='ignore'))
         elif self.stdout is None:
             pass
         else:
             self.stdout.write(out)
+
+    def _write_stderr(self, err):
         if isinstance(self.stderr, StringIO):
             self.stderr.write(err.decode(errors='ignore'))
         elif self.stderr is None:
@@ -206,6 +198,20 @@ class LocalRemoteProcess(object):
         else:
             self.stderr.write(err)
 
+    def wait(self):
+        if self.finished:
+            # Avoid calling communicate() on a dead process because it'll
+            # give you stick about std* already being closed
+            if self.check_status and self.exitstatus != 0:
+                raise CommandFailedError(self.args, self.exitstatus)
+            else:
+                return
+
+        out, err = self.subproc.communicate()
+        out, err = rm_nonascii_chars(out), rm_nonascii_chars(err)
+        self._write_stdout(out)
+        self._write_stderr(err)
+
         self.exitstatus = self.returncode = self.subproc.returncode
 
         if self.exitstatus != 0:
@@ -222,19 +228,11 @@ class LocalRemoteProcess(object):
 
         if self.subproc.poll() is not None:
             out, err = self.subproc.communicate()
-            if isinstance(self.stdout, StringIO):
-                self.stdout.write(out.decode(errors='ignore'))
-            elif self.stdout is None:
-                pass
-            else:
-                self.stdout.write(out)
-            if isinstance(self.stderr, StringIO):
-                self.stderr.write(err.decode(errors='ignore'))
-            elif self.stderr is None:
-                pass
-            else:
-                self.stderr.write(err)
+            self._write_stdout(out)
+            self._write_stderr(err)
+
             self.exitstatus = self.returncode = self.subproc.returncode
+
             return True
         else:
             return False