Bug 1026181 - Make mach terminal formatter work outside mach context, r=wlach
☠☠ backed out by 6a1dd10c12c2 ☠ ☠
authorJames Graham <james@hoppipolla.co.uk>
Mon, 23 Jun 2014 16:20:42 +0100
changeset 190136 1566b80f6c2bfe87c71d596e138afbe2b0fa55f4
parent 190135 a43d41c1645a59ef27a1cd2e3587ed4e8e58296d
child 190137 31bf8c4b3e19d65153fd4ee22ab2a18865b82f94
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerswlach
bugs1026181
milestone33.0a1
Bug 1026181 - Make mach terminal formatter work outside mach context, r=wlach
testing/mozbase/mozlog/mozlog/structured/commandline.py
testing/mozbase/mozlog/mozlog/structured/formatters/machformatter.py
testing/mozbase/mozlog/setup.py
--- a/testing/mozbase/mozlog/mozlog/structured/commandline.py
+++ b/testing/mozbase/mozlog/mozlog/structured/commandline.py
@@ -57,37 +57,47 @@ def add_logging_group(parser):
     else:
         group = parser.add_argument_group(group_name,
                                           group_description)
         for name, (cls, help_str) in log_formatters.iteritems():
             group.add_argument("--log-" + name, action="append", type=log_file,
                                help=help_str)
 
 
-def setup_logging(suite, args, defaults):
+def setup_logging(suite, args, defaults=None):
     """
     Configure a structuredlogger based on command line arguments.
 
     The created structuredlogger will also be set as the default logger, and
     can be retrieved with :py:func:`get_default_logger`.
 
     :param suite: The name of the testsuite being run
     :param args: A dictionary of {argument_name:value} produced from
                  parsing the command line arguments for the application
     :param defaults: A dictionary of {formatter name: output stream} to apply
-                     when there is no logging supplied on the command line.
+                     when there is no logging supplied on the command line. If
+                     this isn't supplied, reasonable defaults are chosen
+                     (coloured mach formatting if stdout is a terminal, or raw
+                     logs otherwise).
 
     :rtype: StructuredLogger
     """
     logger = StructuredLogger(suite)
     prefix = "log_"
     found = False
     found_stdout_logger = False
     if not hasattr(args, 'iteritems'):
         args = vars(args)
+
+    if defaults is None:
+        if sys.__stdout__.isatty():
+            defaults = {"mach_terminal": sys.stdout}
+        else:
+            defaults = {"raw": sys.stdout}
+
     for name, values in args.iteritems():
         if name.startswith(prefix) and values is not None:
             for value in values:
                 found = True
                 if isinstance(value, str):
                     value = log_file(value)
                 if value == sys.stdout:
                     found_stdout_logger = True
--- a/testing/mozbase/mozlog/mozlog/structured/formatters/machformatter.py
+++ b/testing/mozbase/mozlog/mozlog/structured/formatters/machformatter.py
@@ -1,14 +1,16 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 import time
 
+import blessings
+
 import base
 
 
 def format_seconds(total):
     """Format number of seconds to MM:SS.DD form."""
     minutes, seconds = divmod(total, 60)
     return '%2d:%05.2f' % (minutes, seconds)
 
@@ -129,16 +131,18 @@ class MachTerminalFormatter(BaseMachForm
     :param write_interval: bool indicating whether to include the interval since the
                            last message
     :param write_times: bool indicating whether to include the time since the testrun
                         started.
     :param terminal: Terminal object from mach.
     """
     def __init__(self, start_time=None, write_interval=False, write_times=True,
                  terminal=None):
+        if terminal is None:
+            terminal = blessings.Terminal()
         self.terminal = terminal
         BaseMachFormatter.__init__(self,
                                    start_time=start_time,
                                    write_interval=write_interval,
                                    write_times=write_times)
 
     def __call__(self, data):
         s = BaseMachFormatter.__call__(self, data)
--- a/testing/mozbase/mozlog/setup.py
+++ b/testing/mozbase/mozlog/setup.py
@@ -12,16 +12,17 @@ setup(name=PACKAGE_NAME,
       description="Robust log handling specialized for logging in the Mozilla universe",
       long_description="see http://mozbase.readthedocs.org/",
       author='Mozilla Automation and Testing Team',
       author_email='tools@lists.mozilla.org',
       url='https://wiki.mozilla.org/Auto-tools/Projects/Mozbase',
       license='MPL 1.1/GPL 2.0/LGPL 2.1',
       packages=find_packages(),
       zip_safe=False,
+      install_requires=["blessings>=1.3"],
       tests_require=['mozfile'],
       platforms =['Any'],
       classifiers=['Development Status :: 4 - Beta',
                    'Environment :: Console',
                    'Intended Audience :: Developers',
                    'License :: OSI Approved :: Mozilla Public License 1.1 (MPL 1.1)',
                    'Operating System :: OS Independent',
                    'Topic :: Software Development :: Libraries :: Python Modules',