1#!/usr/bin/python
2"""
3Selects all rows and columns that satisfy the condition specified
4and prints the matrix.
5"""
6import optparse
7import common
8from autotest_lib.cli import rpc
9from autotest_lib.database import database_connection
10
11
12# First do all the options parsing
13parser = optparse.OptionParser()
14parser.add_option(
15        '-C', '--columns', action='store', dest='columns',
16        default='test_name,reason,test_started_time,test_finished_time,job_tag,'
17                'job_name,hostname,platform,kernel,status',
18        help='Comma-separated list of column names to display')
19parser.add_option('-w', '--where', action='store', dest='condition',
20            help=("The WHERE condition for the query witten in the 'new style' "
21                  "condition syntax for new tko (see "
22                  "http://autotest.kernel.org/wiki/TkoHowTo for more info)"))
23parser.add_option(
24        '--test-attribute-field', action='append', default=[],
25        help='Specifies a test attribute to include as a field.  The attribute '
26             'value will be available as a field named attribute_<attribute '
27             'name>.  This option may be specified multiple times.  Filtering '
28             'must be done slightly differently -- see '
29             'http://autotest.kernel.org/wiki/TkoHowTo#attribute_filtering '
30             'for more details.')
31parser.add_option('--test-label-field', action='append', default=[],
32                  help='Specifies a test label to include as a field.  See '
33                       '--attribute-field for more details')
34parser.add_option('--iteration-result-field', action='append', default=[],
35                  help='Specifies an iteration result to include as a field.  '
36                       'See --attribute-field for more details.  Note that '
37                       'this causes the rows returned to represent iterations '
38                       'rather than plain test results.')
39parser.add_option('--machine-label-field', action='append', default=[],
40                  help='Specifies a machine label to include as a field.  See '
41                       '--attribute-field for more details')
42parser.add_option('--job-keyval-field', action='append', default=[],
43                  help='Specifies a job keyval to include as a field. See '
44                       '--attribute-field for more details')
45parser.add_option('--iteration-attribute-field', action='append', default=[],
46                  help='Specifies an iteration attribute to include as a '
47                       'field. See --attribute-field for more details.  Note '
48                       'that this causes the rows returned to represent '
49                       'iterations rather than plain test results.')
50parser.add_option('-s', '--separator', action='store', default = ' | ',
51            dest='separator', help = 'output separator')
52parser.add_option('-n', '--nocount', action='store_true', default=False,
53                  help='Do not display line counts before each line')
54parser.add_option('-l', '--logpath', action='store_true', default=False,
55                  help='Reformats the the tag column into a URL \
56                        like http://autotest/results/[tag]. \
57                        This will append the tag column if it isn\'t provided.')
58parser.add_option('--host-label', action='store', dest='host_label',
59                  help=('Return results only for machines currently '
60                        'in the specified label'))
61
62(options, args) = parser.parse_args()
63
64if not options.condition:
65    parser.error('You must specify a condition.')
66
67where = options.condition.replace('%', '%%')
68tag = 'job_tag'
69
70columns = options.columns.split(',')
71
72url_prefix = rpc.get_autotest_server() + '/results/'
73if options.logpath:
74    if tag not in columns:
75        columns.append(tag)
76    tag_index=columns.index(tag)
77
78if options.host_label:
79    database = database_connection.DatabaseConnection("AUTOTEST_WEB")
80    database.connect()
81    sql = ("SELECT hostname FROM afe_labels JOIN afe_hosts_labels "
82           "ON afe_labels.id=afe_hosts_labels.label_id JOIN afe_hosts "
83           "ON afe_hosts_labels.host_id=afe_hosts.id WHERE name=%s")
84    results = database.execute(sql, options.host_label)
85    hosts = [row[0] for row in results]
86    where += " AND hostname IN ('" + "','".join(hosts) + "')"
87
88# Grab the data
89tko = rpc.tko_comm()
90count = 0
91test_views = tko.run(
92        'get_test_views', extra_where=where,
93        test_attribute_fields=options.test_attribute_field,
94        test_label_fields=options.test_label_field,
95        iteration_result_fields=options.iteration_result_field,
96        machine_label_fields=options.machine_label_field,
97        job_keyval_fields=options.job_keyval_field,
98        iteration_attribute_fields=options.iteration_attribute_field)
99for test_view in test_views:
100    values = [str(test_view[column]) for column in columns]
101    if options.logpath:
102        values[tag_index] = url_prefix + values[tag_index]
103    if not options.nocount:
104        print '[%d] ' % count,
105        count += 1
106    print options.separator.join(values)
107