blob: 163ace77f7e644093604edf7443a6789b459e93b [file] [log] [blame]
mblighbe630eb2008-08-01 16:41:48 +00001#!/usr/bin/python
2"""
3Selects all rows and columns that satisfy the condition specified
4and prints the matrix.
5"""
showardd9e341e2010-01-12 18:56:42 +00006import optparse
mblighbe630eb2008-08-01 16:41:48 +00007import common
8from autotest_lib.cli import rpc
mbligh789da1e2009-04-01 18:35:12 +00009from autotest_lib.database import database_connection
mblighbe630eb2008-08-01 16:41:48 +000010
11
12# First do all the options parsing
13parser = optparse.OptionParser()
showardd9e341e2010-01-12 18:56:42 +000014parser.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',
mbligh8dd5c6c2008-09-04 16:46:34 +000020 help=("The WHERE condition for the query witten in the 'new style' "
mbligh28afa732008-09-30 20:29:46 +000021 "condition syntax for new tko (see "
showardd9e341e2010-01-12 18:56:42 +000022 "http://autotest.kernel.org/wiki/TkoHowTo for more info)"))
23parser.add_option(
24 '--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('--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-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')
mblighbe630eb2008-08-01 16:41:48 +000042parser.add_option('-s', '--separator', action='store', default = ' | ',
43 dest='separator', help = 'output separator')
44parser.add_option('-n', '--nocount', action='store_true', default=False,
45 help='Do not display line counts before each line')
46parser.add_option('-l', '--logpath', action='store_true', default=False,
47 help='Reformats the the tag column into a URL \
48 like http://autotest/results/[tag]. \
49 This will append the tag column if it isn\'t provided.')
mbligh789da1e2009-04-01 18:35:12 +000050parser.add_option('--host-label', action='store', dest='host_label',
51 help=('Return results only for machines currently '
52 'in the specified label'))
mblighbe630eb2008-08-01 16:41:48 +000053
54(options, args) = parser.parse_args()
55
showardd9e341e2010-01-12 18:56:42 +000056if not options.condition:
57 parser.error('You must specify a condition.')
58
59where = options.condition.replace('%', '%%')
60tag = 'job_tag'
mblighdeb758e2008-08-11 19:56:23 +000061
mblighbe630eb2008-08-01 16:41:48 +000062columns = options.columns.split(',')
63
64url_prefix = rpc.get_autotest_server() + '/results/'
65if options.logpath:
mbligh8dd5c6c2008-09-04 16:46:34 +000066 if tag not in columns:
67 columns.append(tag)
68 tag_index=columns.index(tag)
mblighbe630eb2008-08-01 16:41:48 +000069
mbligh789da1e2009-04-01 18:35:12 +000070if options.host_label:
71 database = database_connection.DatabaseConnection("AUTOTEST_WEB")
72 database.connect()
showardeab66ce2009-12-23 00:03:56 +000073 sql = ("SELECT hostname FROM afe_labels JOIN afe_hosts_labels "
74 "ON afe_labels.id=afe_hosts_labels.label_id JOIN afe_hosts "
75 "ON afe_hosts_labels.host_id=afe_hosts.id WHERE name=%s")
mbligh789da1e2009-04-01 18:35:12 +000076 results = database.execute(sql, options.host_label)
77 hosts = [row[0] for row in results]
78 where += " AND hostname IN ('" + "','".join(hosts) + "')"
79
mblighbe630eb2008-08-01 16:41:48 +000080# Grab the data
showardd9e341e2010-01-12 18:56:42 +000081tko = rpc.tko_comm()
mblighbe630eb2008-08-01 16:41:48 +000082count = 0
showardd9e341e2010-01-12 18:56:42 +000083test_views = tko.run('get_test_views', extra_where=where,
84 test_attribute_fields=options.attribute_field,
85 test_label_fields=options.label_field,
86 iteration_fields=options.iteration_field,
87 machine_label_fields=options.machine_label_field)
88for test_view in test_views:
89 values = [str(test_view[column]) for column in columns]
mblighbe630eb2008-08-01 16:41:48 +000090 if options.logpath:
91 values[tag_index] = url_prefix + values[tag_index]
92 if not options.nocount:
93 print '[%d] ' % count,
94 count += 1
95 print options.separator.join(values)