summaryrefslogtreecommitdiff
path: root/cli/query_keyvals
blob: e8e75031632ddc65698c2b50d16376a0d116310a (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
#!/usr/bin/python

import sys, optparse
import common
from autotest_lib.cli import rpc


def parse_options():
    usage = "usage: %prog [options] job_id"
    parser = optparse.OptionParser(usage=usage)
    parser.add_option("-m", "--machine", dest="machine")
    parser.add_option("-t", "--test", dest="test")
    parser.add_option("-T", "--type", dest="type", type="choice",
                      choices=["all", "test", "iteration", "attr", "perf"])
    parser.add_option("-k", "--key", dest="key")
    parser.set_defaults(type="all")
    options, args = parser.parse_args()
    options.show_test_keyvals = options.type in ("all", "test")
    options.show_attr_keyvals = options.type in ("all", "iteration", "attr")
    options.show_perf_keyvals = options.type in ("all", "iteration", "perf")
    options.show_iter_keyvals = (
        options.show_perf_keyvals or options.show_attr_keyvals)
    return parser, options, args


def print_keyvals(keyval, format_string, options):
    for key, value in keyval.iteritems():
        if not options.key or key == options.key:
            print format_string % (key, value)


def print_views(test_views, options):
    for view in test_views:
        if not options.machine:
            print "Machine: %s" % view["hostname"]
        if not options.test:
            print "Test: %s" % view["test_name"]
        if options.show_test_keyvals:
            print "Test Attributes:"
            print_keyvals(view["attributes"], "\t%s = %s", options)
        if options.show_iter_keyvals:
            print "Iteration Attributes:"
            for i, iteration in enumerate(view["iterations"]):
                print "\tIteration #%d:" % (i + 1)
                if options.show_attr_keyvals:
                    print_keyvals(iteration["attr"], "\t\t%s(attr) = %s",
                                  options)
                if options.show_perf_keyvals:
                    print_keyvals(iteration["perf"], "\t\t%s(perf) = %s",
                                  options)
        print


def main():
    parser, options, args = parse_options()
    if not args:
        parser.print_help()
        return

    query_filter = {}
    if options.machine:
        query_filter["hostname"] = options.machine
    if options.test:
        query_filter["test_name"] = options.test

    comm = rpc.tko_comm()
    test_views = []
    for job_id in args:
        query_filter["job_tag__startswith"] = "%s-" % job_id
        test_views += comm.run("get_detailed_test_views", **query_filter)
    print_views(test_views, options)


if __name__ == "__main__":
    main()