blob: 0843b895432776ac6753f63c0e39c42c5b716981 [file] [log] [blame]
mbligh2b672532007-11-05 19:24:51 +00001"""
2This library provides a bunch of miscellaneous parameter parsing,
3sql generating and list cleanup library functions that are used
4by both the reporting cli and web interface.
5"""
6
Prathmesh Prabhu99583e52017-02-03 16:09:17 -08007import os
8import re
9import sys
mbligh2b672532007-11-05 19:24:51 +000010
Prathmesh Prabhu99583e52017-02-03 16:09:17 -080011import common
Prathmesh Prabhu99583e52017-02-03 16:09:17 -080012from autotest_lib.tko import display
13from autotest_lib.tko import frontend
mbligh2b672532007-11-05 19:24:51 +000014
mbligh12eebfa2008-01-03 02:01:53 +000015def parse_scrub_and_gen_condition(condition, valid_field_dict):
jadmanski0afbb632008-06-06 21:10:57 +000016 me = parse_scrub_and_gen_condition # shorten the name
17 compare_ops = {'=':'=', '<>':'<>', '==':'=', '!=':'<>', '>':'>',
18 '<':'<', '>=':'>=', '<=':'<=', '~':'LIKE', '#':'REGEXP'}
mbligh12eebfa2008-01-03 02:01:53 +000019
jadmanski0afbb632008-06-06 21:10:57 +000020 # strip white space
21 condition = condition.strip()
mbligh12eebfa2008-01-03 02:01:53 +000022
jadmanski0afbb632008-06-06 21:10:57 +000023 # OR
24 match = re.match(r'^(.+)[|](.+)$', condition)
25 if match:
jadmanski0afbb632008-06-06 21:10:57 +000026 (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)
mbligh12eebfa2008-01-03 02:01:53 +000030
jadmanski0afbb632008-06-06 21:10:57 +000031 # AND
32 match = re.match(r'^(.+)[&](.+)$', condition)
33 if match:
jadmanski0afbb632008-06-06 21:10:57 +000034 (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)
mbligh12eebfa2008-01-03 02:01:53 +000038
jadmanski0afbb632008-06-06 21:10:57 +000039 # '<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])
mbligh12eebfa2008-01-03 02:01:53 +000058
jadmanski0afbb632008-06-06 21:10:57 +000059 raise "Could not parse '%s' (%s)" % (condition, regex)