summaryrefslogtreecommitdiff
path: root/tsp/scripts/publish_cmp.py
blob: 15dac317a2d5d3d85e300df71da078b5c5176898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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()