Add tsp/scripts/publish_cmp.py 04/83904/11
authorAleksander Mistewicz <a.mistewicz@samsung.com>
Fri, 12 Aug 2016 11:05:09 +0000 (13:05 +0200)
committerAleksander Mistewicz <a.mistewicz@samsung.com>
Tue, 6 Dec 2016 15:37:14 +0000 (16:37 +0100)
"Unclear" is printed in case:
 * number of tests run is different
 * prerelease's result is not 'PASS', but snapshot's is
"OK" is printed otherwise.

Change-Id: Ic01e7652808f62e5d3c63925d1ea4326a2eeef2c
Signed-off-by: Aleksander Mistewicz <a.mistewicz@samsung.com>
tsp/scripts/publish_cmp.py [new file with mode: 0755]

diff --git a/tsp/scripts/publish_cmp.py b/tsp/scripts/publish_cmp.py
new file mode 100755 (executable)
index 0000000..15dac31
--- /dev/null
@@ -0,0 +1,96 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+##
+# @author Aleksander Mistewicz <a.mistewicz@samsung.com>
+
+import argparse
+import logging
+import json
+
+__version__ = "0.0.1"
+__license__ = "APACHE-2.0"
+__author__ = "Aleksander Mistewicz"
+__author_email__ = "a.mistewicz@samsung.com"
+
+USAGE = "%prog <opts> <snapshot> <prerelease>"
+
+AGENT = "%s/%s" % (__name__, __version__)
+
+def compare(snap, pre):
+    with open(snap, 'r') as snap_f:
+        snap_json = json.load(snap_f)
+
+    with open(pre, 'r') as pre_f:
+        pre_json = json.load(pre_f)
+
+    snap_tests = snap_json['tests']
+    pre_tests = pre_json['tests']
+
+    if len(snap_tests) != len(pre_tests):
+        logging.warn("Number of tests run does not match (%d %d)" % (len(snap_tests), len(pre_tests)))
+        print "Unclear"
+        return
+
+    for i in xrange(0, len(snap_tests)):
+        snap_status = snap_tests[i]['status']
+        pre_status = pre_tests[i]['status']
+        logging.info("[%s] " % pre_tests[i]['test']\
+                + "Snapshot: %s " % snap_status\
+                + "Prerelease: %s" % pre_status)
+        if pre_status != 'PASS' and snap_status != pre_status:
+            print "Unclear"
+            return
+    print "OK"
+
+def parse_arguments():
+    """parse_arguments() -> args
+
+    Parse any command-line options given returning both
+    the parsed options and arguments.
+    """
+
+    parser = argparse.ArgumentParser(description="Comparator of avocado test results in JSON format")
+
+    parser.add_argument("snap", metavar='<snap>', type=str,
+            help='Path to json format of results of test run on snapshot')
+
+    parser.add_argument("pre", metavar='<pre>', type=str,
+            help='Path to json format of results of test run on prerelease')
+
+    parser.add_argument("-l", "--log",
+            action="store", dest="loglevel",
+            help="Verbosity level")
+
+    args = parser.parse_args()
+
+    return args
+
+def main():
+    args = parse_arguments()
+    if args.loglevel:
+        numeric_level = getattr(logging, args.loglevel.upper(), None)
+        if not isinstance(numeric_level, int):
+            raise ValueError('Invalid log level: %s' % args.loglevel)
+        logging.basicConfig(format='%(asctime)s %(message)s',level=numeric_level)
+    logging.debug("Begin")
+    if args.snap and args.pre:
+        compare(args.snap, args.pre)
+    logging.debug("End")
+
+if __name__ == '__main__':
+    main()