1#!/usr/bin/python
2
3import sys, optparse
4import common
5from autotest_lib.cli import rpc
6
7
8def parse_options():
9    usage = "usage: %prog [options] job_id"
10    parser = optparse.OptionParser(usage=usage)
11    parser.add_option("-m", "--machine", dest="machine")
12    parser.add_option("-t", "--test", dest="test")
13    parser.add_option("-T", "--type", dest="type", type="choice",
14                      choices=["all", "test", "iteration", "attr", "perf"])
15    parser.add_option("-k", "--key", dest="key")
16    parser.set_defaults(type="all")
17    options, args = parser.parse_args()
18    options.show_test_keyvals = options.type in ("all", "test")
19    options.show_attr_keyvals = options.type in ("all", "iteration", "attr")
20    options.show_perf_keyvals = options.type in ("all", "iteration", "perf")
21    options.show_iter_keyvals = (
22        options.show_perf_keyvals or options.show_attr_keyvals)
23    return parser, options, args
24
25
26def print_keyvals(keyval, format_string, options):
27    for key, value in keyval.iteritems():
28        if not options.key or key == options.key:
29            print format_string % (key, value)
30
31
32def print_views(test_views, options):
33    for view in test_views:
34        if not options.machine:
35            print "Machine: %s" % view["hostname"]
36        if not options.test:
37            print "Test: %s" % view["test_name"]
38        if options.show_test_keyvals:
39            print "Test Attributes:"
40            print_keyvals(view["attributes"], "\t%s = %s", options)
41        if options.show_iter_keyvals:
42            print "Iteration Attributes:"
43            for i, iteration in enumerate(view["iterations"]):
44                print "\tIteration #%d:" % (i + 1)
45                if options.show_attr_keyvals:
46                    print_keyvals(iteration["attr"], "\t\t%s(attr) = %s",
47                                  options)
48                if options.show_perf_keyvals:
49                    print_keyvals(iteration["perf"], "\t\t%s(perf) = %s",
50                                  options)
51        print
52
53
54def main():
55    parser, options, args = parse_options()
56    if not args:
57        parser.print_help()
58        return
59
60    query_filter = {}
61    if options.machine:
62        query_filter["hostname"] = options.machine
63    if options.test:
64        query_filter["test_name"] = options.test
65
66    comm = rpc.tko_comm()
67    test_views = []
68    for job_id in args:
69        query_filter["job_tag__startswith"] = "%s-" % job_id
70        test_views += comm.run("get_detailed_test_views", **query_filter)
71    print_views(test_views, options)
72
73
74if __name__ == "__main__":
75    main()
76