mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | """ |
| 3 | Selects all rows and columns that satisfy the condition specified |
| 4 | and prints the matrix. |
| 5 | """ |
| 6 | import sys, os, re, optparse |
| 7 | import common |
| 8 | from autotest_lib.cli import rpc |
mbligh | 789da1e | 2009-04-01 18:35:12 +0000 | [diff] [blame^] | 9 | from autotest_lib.database import database_connection |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 10 | from autotest_lib.tko import display, frontend, db, query_lib |
| 11 | from autotest_lib.client.bin import kernel_versions |
| 12 | |
| 13 | |
| 14 | # First do all the options parsing |
| 15 | parser = optparse.OptionParser() |
| 16 | parser.add_option('-C', '--columns', action='store', dest='columns', |
mbligh | 28afa73 | 2008-09-30 20:29:46 +0000 | [diff] [blame] | 17 | default='*', help="""\ |
| 18 | By default or when using the -c flag: |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 19 | kernel hostname test label machine_group reason tag user status |
mbligh | 28afa73 | 2008-09-30 20:29:46 +0000 | [diff] [blame] | 20 | |
| 21 | OR |
| 22 | |
| 23 | When using the -w flag: |
| 24 | test_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""") |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 25 | |
mbligh | 8dd5c6c | 2008-09-04 16:46:34 +0000 | [diff] [blame] | 26 | parser.add_option('-c', '--condition', action='store', dest='old_condition', |
mbligh | b82b44f | 2008-12-09 23:34:06 +0000 | [diff] [blame] | 27 | help=("The WHERE condition for the query written in the 'old style' " |
mbligh | 8dd5c6c | 2008-09-04 16:46:34 +0000 | [diff] [blame] | 28 | "condition syntax for the original tko")) |
| 29 | parser.add_option('-w', '--where', action='store', dest='new_condition', |
| 30 | help=("The WHERE condition for the query witten in the 'new style' " |
mbligh | 28afa73 | 2008-09-30 20:29:46 +0000 | [diff] [blame] | 31 | "condition syntax for new tko (see " |
| 32 | "http://autotest.kernel.org/wiki/TkoHowTo for more info)")) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 33 | parser.add_option('-s', '--separator', action='store', default = ' | ', |
| 34 | dest='separator', help = 'output separator') |
| 35 | parser.add_option('-n', '--nocount', action='store_true', default=False, |
| 36 | help='Do not display line counts before each line') |
| 37 | parser.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.') |
mbligh | 789da1e | 2009-04-01 18:35:12 +0000 | [diff] [blame^] | 41 | parser.add_option('--host-label', action='store', dest='host_label', |
| 42 | help=('Return results only for machines currently ' |
| 43 | 'in the specified label')) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 44 | |
| 45 | (options, args) = parser.parse_args() |
| 46 | |
mbligh | 8dd5c6c | 2008-09-04 16:46:34 +0000 | [diff] [blame] | 47 | if 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) |
| 50 | elif 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' |
| 55 | elif options.new_condition: |
| 56 | where = options.new_condition.replace('%', '%%') |
| 57 | view = 'test_view_2' |
| 58 | tag = 'job_tag' |
| 59 | else: |
mbligh | deb758e | 2008-08-11 19:56:23 +0000 | [diff] [blame] | 60 | parser.error('You must specify at least one condition.') |
| 61 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 62 | columns = options.columns.split(',') |
| 63 | |
| 64 | url_prefix = rpc.get_autotest_server() + '/results/' |
| 65 | if options.logpath: |
mbligh | 8dd5c6c | 2008-09-04 16:46:34 +0000 | [diff] [blame] | 66 | if tag not in columns: |
| 67 | columns.append(tag) |
| 68 | tag_index=columns.index(tag) |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 69 | |
mbligh | 8dd5c6c | 2008-09-04 16:46:34 +0000 | [diff] [blame] | 70 | if options.old_condition: |
| 71 | columns = [frontend.test_view_field_dict.get(field, field) |
| 72 | for field in columns] |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 73 | |
mbligh | 789da1e | 2009-04-01 18:35:12 +0000 | [diff] [blame^] | 74 | if 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 | |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 84 | # Grab the data |
| 85 | db = db.db() |
| 86 | count = 0 |
mbligh | 8dd5c6c | 2008-09-04 16:46:34 +0000 | [diff] [blame] | 87 | for row in db.select(','.join(columns), view, where): |
mbligh | be630eb | 2008-08-01 16:41:48 +0000 | [diff] [blame] | 88 | 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) |