Add print-to-a-file option when executing a command in functest_utils
authorjose.lausuch <jose.lausuch@ericsson.com>
Thu, 1 Sep 2016 13:03:18 +0000 (15:03 +0200)
committerJose Lausuch <jose.lausuch@ericsson.com>
Thu, 1 Sep 2016 13:07:31 +0000 (13:07 +0000)
Change-Id: I8b91979380d0f4794d5ceaf8edb2a9d80cd7170f
Signed-off-by: jose.lausuch <jose.lausuch@ericsson.com>
utils/functest_utils.py

index 8c5dd83..644ad1a 100644 (file)
@@ -294,7 +294,7 @@ def get_ci_envvars():
 
 
 def execute_command(cmd, exit_on_error=True, info=False, error_msg="",
-                    verbose=True):
+                    verbose=True, output_file=None):
     if not error_msg:
         error_msg = ("The command '%s' failed." % cmd)
     msg_exec = ("Executing command: '%s'" % cmd)
@@ -305,10 +305,17 @@ def execute_command(cmd, exit_on_error=True, info=False, error_msg="",
             logger.debug(msg_exec)
     p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT)
+    if output_file:
+        f = open(output_file, "w")
     for line in iter(p.stdout.readline, b''):
-        line = line.replace('\n', '')
-        print line
-        sys.stdout.flush()
+        if output_file:
+            f.write(line)
+        else:
+            line = line.replace('\n', '')
+            print line
+            sys.stdout.flush()
+    if output_file:
+        f.close()
     p.stdout.close()
     returncode = p.wait()
     if returncode != 0: