test_push: only run one bash command in a subprocess call

Previously, automated_script runs several bash commands in one
subprocess call. If one fails, the subprocess call will continue to run.
Change to run only one bash command in a subprocess call.

BUG=None
TEST=Test locally

Change-Id: Ifcafdac872fdc78b96f25aba4baced9d72c049f3
Reviewed-on: https://chromium-review.googlesource.com/413344
Reviewed-by: Shuqian Zhao <shuqianz@chromium.org>
Tested-by: Shuqian Zhao <shuqianz@chromium.org>
diff --git a/site_utils/lib/infra.py b/site_utils/lib/infra.py
index 4c6fb69..4fb8ffb 100644
--- a/site_utils/lib/infra.py
+++ b/site_utils/lib/infra.py
@@ -40,18 +40,25 @@
     @param cmd: The command to run.
     @param stream_output: If True, streams the stdout of the process.
 
-    @returns: The output of cmd.
+    @returns: The output of cmd, will be stdout and stderr.
     @raises CalledProcessError: If there was a non-0 return code.
     """
-    if not stream_output:
-        return subprocess.check_output(cmd, shell=True)
-
+    print 'Running command: %s' % cmd
     proc = subprocess.Popen(
-        cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
-    while proc.poll() is None:
-        print proc.stdout.readline().rstrip('\n')
-    if proc.returncode !=0:
-        raise subprocess.CalledProcessError(proc.returncode, cmd)
+        cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    if stream_output:
+        output = ''
+        for newline in iter(proc.stdout.readline, ''):
+            output += newline
+            print newline.rstrip(os.linesep)
+    else:
+        output = proc.communicate()[0]
+
+    return_code = proc.wait()
+    if return_code !=0:
+        print "ERROR: '%s' failed with error:\n%s" % (cmd, output)
+        raise subprocess.CalledProcessError(return_code, cmd, output[:1024])
+    return output
 
 
 _host_objects = {}