blob: 7d3ab7addb98b21ce8daa2c49e547c1752e25e8b [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"""
6import sys, os, re, optparse
7import 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 +000010from autotest_lib.tko import display, frontend, db, query_lib
11from autotest_lib.client.bin import kernel_versions
12
13
14# First do all the options parsing
15parser = optparse.OptionParser()
16parser.add_option('-C', '--columns', action='store', dest='columns',
mbligh28afa732008-09-30 20:29:46 +000017 default='*', help="""\
18By default or when using the -c flag:
mblighbe630eb2008-08-01 16:41:48 +000019kernel hostname test label machine_group reason tag user status
mbligh28afa732008-09-30 20:29:46 +000020
21OR
22
23When using the -w flag:
24test_idx test_name reason test_started_time test_finished_time job_tag job_name job_owner job_queued_time job_started_time job_finished_time hostname platform kernel status""")
mblighbe630eb2008-08-01 16:41:48 +000025
mbligh8dd5c6c2008-09-04 16:46:34 +000026parser.add_option('-c', '--condition', action='store', dest='old_condition',
mblighb82b44f2008-12-09 23:34:06 +000027 help=("The WHERE condition for the query written in the 'old style' "
mbligh8dd5c6c2008-09-04 16:46:34 +000028 "condition syntax for the original tko"))
29parser.add_option('-w', '--where', action='store', dest='new_condition',
30 help=("The WHERE condition for the query witten in the 'new style' "
mbligh28afa732008-09-30 20:29:46 +000031 "condition syntax for new tko (see "
32 "http://autotest.kernel.org/wiki/TkoHowTo for more info)"))
mblighbe630eb2008-08-01 16:41:48 +000033parser.add_option('-s', '--separator', action='store', default = ' | ',
34 dest='separator', help = 'output separator')
35parser.add_option('-n', '--nocount', action='store_true', default=False,
36 help='Do not display line counts before each line')
37parser.add_option('-l', '--logpath', action='store_true', default=False,
38 help='Reformats the the tag column into a URL \
39 like http://autotest/results/[tag]. \
40 This will append the tag column if it isn\'t provided.')
mbligh789da1e2009-04-01 18:35:12 +000041parser.add_option('--host-label', action='store', dest='host_label',
42 help=('Return results only for machines currently '
43 'in the specified label'))
mblighbe630eb2008-08-01 16:41:48 +000044
45(options, args) = parser.parse_args()
46
mbligh8dd5c6c2008-09-04 16:46:34 +000047if options.old_condition and options.new_condition:
48 msg = 'You cannot specify WHERE clauses in both the old and new style.'
49 parser.error(msg)
50elif options.old_condition:
51 where = query_lib.parse_scrub_and_gen_condition(
52 options.old_condition, frontend.test_view_field_dict)
53 view = 'test_view'
54 tag = 'tag'
55elif options.new_condition:
56 where = options.new_condition.replace('%', '%%')
57 view = 'test_view_2'
58 tag = 'job_tag'
59else:
mblighdeb758e2008-08-11 19:56:23 +000060 parser.error('You must specify at least one condition.')
61
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
mbligh8dd5c6c2008-09-04 16:46:34 +000070if options.old_condition:
71 columns = [frontend.test_view_field_dict.get(field, field)
72 for field in columns]
mblighbe630eb2008-08-01 16:41:48 +000073
mbligh789da1e2009-04-01 18:35:12 +000074if options.host_label:
75 database = database_connection.DatabaseConnection("AUTOTEST_WEB")
76 database.connect()
77 sql = ("SELECT hostname FROM labels JOIN hosts_labels "
78 "ON labels.id=hosts_labels.label_id JOIN hosts "
79 "ON hosts_labels.host_id=hosts.id WHERE name=%s")
80 results = database.execute(sql, options.host_label)
81 hosts = [row[0] for row in results]
82 where += " AND hostname IN ('" + "','".join(hosts) + "')"
83
mblighbe630eb2008-08-01 16:41:48 +000084# Grab the data
85db = db.db()
86count = 0
mbligh8dd5c6c2008-09-04 16:46:34 +000087for row in db.select(','.join(columns), view, where):
mblighbe630eb2008-08-01 16:41:48 +000088 values = [str(x) for x in row]
89 if options.logpath:
90 values[tag_index] = url_prefix + values[tag_index]
91 if not options.nocount:
92 print '[%d] ' % count,
93 count += 1
94 print options.separator.join(values)