Disable output buffering in devtools scripts.

Rather than adding .flush() calls manually after each printout, this disables
output buffering in devtools scripts altogether.

R=qsr@chromium.org

Review URL: https://codereview.chromium.org/1964903002 .

Cr-Mirrored-From: https://github.com/domokit/mojo
Cr-Mirrored-Commit: 12b1499ab574fdcbdcc013fdb39d2d9beea8793e
diff --git a/devtoolslib/android_shell.py b/devtoolslib/android_shell.py
index daa2443..ca7d5c1 100644
--- a/devtoolslib/android_shell.py
+++ b/devtoolslib/android_shell.py
@@ -395,7 +395,6 @@
           host_port = self._forward_host_port_to_device(0, device_port)
           print ('Dart observatory available at the host at http://127.0.0.1:%d'
                  % host_port)
-          sys.stdout.flush()
 
     logcat_watch_thread = threading.Thread(
         target=_forward_observatories_as_needed)
diff --git a/devtoolslib/utils.py b/devtoolslib/utils.py
index 7bf2e1c..085d225 100644
--- a/devtoolslib/utils.py
+++ b/devtoolslib/utils.py
@@ -4,6 +4,9 @@
 
 """Python utils."""
 
+import os
+import sys
+
 
 def overrides(parent_class):
   """Inherits the docstring from the method of the same name in the indicated
@@ -14,3 +17,11 @@
     method.__doc__ = getattr(parent_class, method.__name__).__doc__
     return method
   return overriding
+
+
+def disable_output_buffering():
+  """Disables the buffering of the stdout. Devtools command line scripts should
+  do so, so that their stdout is consistent when not directly attached to a
+  terminal (e.g. because another script runs devtools in a subprocess).
+  """
+  sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
diff --git a/mojo_benchmark b/mojo_benchmark
index e01ff62..0c1dcd9 100755
--- a/mojo_benchmark
+++ b/mojo_benchmark
@@ -15,6 +15,7 @@
 from devtoolslib import perf_dashboard
 from devtoolslib import shell_arguments
 from devtoolslib import shell_config
+from devtoolslib.utils import disable_output_buffering
 
 
 _DESCRIPTION = """Runner for Mojo application benchmarks.
@@ -145,6 +146,7 @@
 
 
 def main():
+  disable_output_buffering()
   parser = argparse.ArgumentParser(
       formatter_class=argparse.RawDescriptionHelpFormatter,
       description=_DESCRIPTION)
diff --git a/mojo_run b/mojo_run
index 43f83be..72fc047 100755
--- a/mojo_run
+++ b/mojo_run
@@ -9,6 +9,7 @@
 
 from devtoolslib import shell_arguments
 from devtoolslib import shell_config
+from devtoolslib.utils import disable_output_buffering
 
 _DESCRIPTION = """Runner for Mojo applications.
 
@@ -48,6 +49,7 @@
 
 
 def main():
+  disable_output_buffering()
   logging.basicConfig()
 
   parser = argparse.ArgumentParser(
diff --git a/mojo_test b/mojo_test
index 59f5310..eca37f3 100755
--- a/mojo_test
+++ b/mojo_test
@@ -17,6 +17,7 @@
 from devtoolslib import apptest_gtest
 from devtoolslib import shell_arguments
 from devtoolslib import shell_config
+from devtoolslib.utils import disable_output_buffering
 
 _DESCRIPTION = """Runner for Mojo application tests.
 
@@ -59,6 +60,7 @@
 
 
 def main():
+  disable_output_buffering()
   parser = argparse.ArgumentParser(
       formatter_class=argparse.RawDescriptionHelpFormatter,
       description=_DESCRIPTION)
@@ -102,7 +104,6 @@
 
     _logger.info("Will start: %s" % test_name)
     print "Running %s...." % test_name,
-    sys.stdout.flush()
 
     if test_type == "dart":
       apptest_result = apptest_dart.run_dart_apptest(shell, shell_args, test,