mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 1 | """ |
| 2 | This library provides a bunch of miscellaneous parameter parsing, |
| 3 | sql generating and list cleanup library functions that are used |
| 4 | by both the reporting cli and web interface. |
| 5 | """ |
| 6 | |
Prathmesh Prabhu | 99583e5 | 2017-02-03 16:09:17 -0800 | [diff] [blame] | 7 | import os |
| 8 | import re |
| 9 | import sys |
mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 10 | |
Prathmesh Prabhu | 99583e5 | 2017-02-03 16:09:17 -0800 | [diff] [blame] | 11 | import common |
Prathmesh Prabhu | 99583e5 | 2017-02-03 16:09:17 -0800 | [diff] [blame] | 12 | from autotest_lib.tko import display |
| 13 | from autotest_lib.tko import frontend |
mbligh | 2b67253 | 2007-11-05 19:24:51 +0000 | [diff] [blame] | 14 | |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 15 | def parse_scrub_and_gen_condition(condition, valid_field_dict): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 16 | me = parse_scrub_and_gen_condition # shorten the name |
| 17 | compare_ops = {'=':'=', '<>':'<>', '==':'=', '!=':'<>', '>':'>', |
| 18 | '<':'<', '>=':'>=', '<=':'<=', '~':'LIKE', '#':'REGEXP'} |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 19 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 20 | # strip white space |
| 21 | condition = condition.strip() |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 22 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 23 | # OR |
| 24 | match = re.match(r'^(.+)[|](.+)$', condition) |
| 25 | if match: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 26 | (a_sql, a_values) = me(match.group(1), valid_field_dict) |
| 27 | (b_sql, b_values) = me(match.group(2), valid_field_dict) |
| 28 | return (" (%s) OR (%s) " % (a_sql, b_sql), |
| 29 | a_values + b_values) |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 30 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 31 | # AND |
| 32 | match = re.match(r'^(.+)[&](.+)$', condition) |
| 33 | if match: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 34 | (a_sql, a_values) = me(match.group(1), valid_field_dict) |
| 35 | (b_sql, b_values) = me(match.group(2), valid_field_dict) |
| 36 | return (" (%s) AND (%s) " % (a_sql, b_sql), |
| 37 | a_values + b_values) |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 38 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 39 | # '<field> <op> <value>' where value can be quoted |
| 40 | # double quotes are escaped....i.e. '''' is the same as "'" |
| 41 | regex = r'^(%s)[ \t]*(%s)[ \t]*' + \ |
| 42 | r'(\'((\'\'|[^\'])*)\'|"((""|[^"])*)"|([^\'"].*))$' |
| 43 | regex = regex % ('|'.join(valid_field_dict.keys()), |
| 44 | '|'.join(compare_ops.keys())) |
| 45 | match = re.match(regex, condition) |
| 46 | if match: |
| 47 | field = valid_field_dict[match.group(1)] |
| 48 | op = compare_ops[match.group(2)] |
| 49 | if match.group(5): |
| 50 | val = match.group(4).replace("''", "'") |
| 51 | elif match.group(7): |
| 52 | val = match.group(6).replace('""', '"') |
| 53 | elif match.group(8): |
| 54 | val = match.group(8) |
| 55 | else: |
| 56 | raise "Internal error" |
| 57 | return ("%s %s %%s" % (field, op), [val]) |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 58 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 59 | raise "Could not parse '%s' (%s)" % (condition, regex) |