From 339b76bc2557d84e01fac368464edb39d647dc94 Mon Sep 17 00:00:00 2001
From: Luiz Carvalho <lucarval@redhat.com>
Date: Jun 02 2016 13:28:57 +0000
Subject: Log container-build task results


Signed-off-by: Luiz Carvalho <lucarval@redhat.com>

---

diff --git a/src/pyrpkg/__init__.py b/src/pyrpkg/__init__.py
index 218fdc3..6fa6ec5 100644
--- a/src/pyrpkg/__init__.py
+++ b/src/pyrpkg/__init__.py
@@ -40,7 +40,7 @@ from pyrpkg.errors import HashtypeMixingError, rpkgError, rpkgAuthError, \
 from .gitignore import GitIgnore
 from pyrpkg.lookaside import CGILookasideCache
 from pyrpkg.sources import SourcesFile
-from pyrpkg.utils import cached_property, warn_deprecated
+from pyrpkg.utils import cached_property, warn_deprecated, log_result
 
 from osbs.api import OSBS
 from osbs.conf import Configuration
@@ -2596,7 +2596,16 @@ class Commands(object):
             self.log.info('Task info: %s/taskinfo?taskID=%s' % (self.kojiweburl,
                                                                 task_id))
             if not nowait:
-                koji_task_watcher(self.kojisession, [task_id])
+                rv = koji_task_watcher(self.kojisession, [task_id])
+                if rv == 0:
+                    result = self.kojisession.getTaskResult(task_id)
+                    try:
+                        result["koji_builds"] = ["%s/buildinfo?buildID=%s" % (self.kojiweburl, build_id)
+                                                 for build_id in result.get("koji_builds", [])]
+                    except TypeError:
+                        pass
+                    log_result(self.log.info, result)
+
         finally:
             (self.build_client, self.kojiconfig) = koji_session_backup
             self.load_kojisession()
diff --git a/src/pyrpkg/utils.py b/src/pyrpkg/utils.py
index 1d95218..c6af23a 100644
--- a/src/pyrpkg/utils.py
+++ b/src/pyrpkg/utils.py
@@ -61,3 +61,20 @@ def warn_deprecated(clsname, oldname, newname):
         "%s.%s is deprecated and will be removed eventually.\n    Please "
         "use %s.%s instead." % (clsname, oldname, clsname, newname),
         DeprecationWarning, stacklevel=3)
+
+
+def _log_value(log_func, value, level, indent, suffix=''):
+    offset = ' ' * level * indent
+    log_func(''.join([offset, str(value), suffix]))
+
+
+def log_result(log_func, result, level=0, indent=2):
+    if isinstance(result, list):
+        for item in result:
+            log_result(log_func, item, level)
+    elif isinstance(result, dict):
+        for key, value in result.items():
+            _log_value(log_func, key, level, indent, ':')
+            log_result(log_func, value, level+1)
+    else:
+        _log_value(log_func, result, level, indent)
diff --git a/test/test_utils.py b/test/test_utils.py
index f9aa14a..9f98b69 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -8,7 +8,7 @@ import mock
 old_path = list(sys.path)
 src_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../src')
 sys.path.insert(0, src_path)
-from pyrpkg.utils import cached_property, warn_deprecated
+from pyrpkg.utils import cached_property, warn_deprecated, log_result
 sys.path = old_path
 
 
@@ -157,3 +157,50 @@ class DeprecationUtilsTestCase(unittest.TestCase):
         warnings.simplefilter('error', DeprecationWarning)
         self.assertRaises(DeprecationWarning, foo.old_method)
         self.assertEqual(len(written_lines), 1)
+
+
+class LogResultTestCase(unittest.TestCase):
+    def setUp(self):
+        self.logs = []
+        def info(msg):
+            self.logs.append(msg)
+        self.log_func = info
+
+    def test_dict_result(self):
+        obj = {'spam': 'maps'}
+        expected = [
+            'spam:',
+            '  maps',
+        ]
+        log_result(self.log_func, obj)
+        self.assertEqual(self.logs, expected)
+
+    def test_list_result(self):
+        obj = ['eggs', 'bacon', 'hash']
+        expected = [
+            'eggs',
+            'bacon',
+            'hash',
+        ]
+        log_result(self.log_func, obj)
+        self.assertEqual(self.logs, expected)
+
+    def test_str_result(self):
+        obj = 'spam'
+        expected = [
+            'spam',
+        ]
+        log_result(self.log_func, obj)
+        self.assertEqual(self.logs, expected)
+
+    def test_complex_result(self):
+        obj = {'breakfast': ['eggs', 'bacon', {'spam': 'maps'}]}
+        expected = [
+            'breakfast:',
+            '  eggs',
+            '  bacon',
+            '  spam:',
+            '    maps',
+        ]
+        log_result(self.log_func, obj)
+        self.assertEqual(self.logs, expected)

