blob: 1c35ac1e25242121725c311077680588667959be [file] [log] [blame]
mbligh51ee7c72008-11-24 17:10:14 +00001import collections, re
mbligh302482e2008-05-01 20:06:16 +00002import common
mbligh1b3b3762008-09-25 02:46:34 +00003from autotest_lib.client.common_lib import log
mbligh2895ce52008-04-17 15:40:51 +00004
5
mbligh1b3b3762008-09-25 02:46:34 +00006statuses = log.job_statuses
jadmanskiba1fa662008-07-11 21:18:30 +00007
8
9def is_worse_than(lhs, rhs):
10 """ Compare two statuses and return a boolean indicating if the LHS status
11 is worse than the RHS status."""
12 return (statuses.index(lhs) < statuses.index(rhs))
13
14
15def is_worse_than_or_equal_to(lhs, rhs):
16 """ Compare two statuses and return a boolean indicating if the LHS status
17 is worse than or equal to the RHS status."""
18 if lhs == rhs:
19 return True
20 return is_worse_than(lhs, rhs)
21
22
mbligh51ee7c72008-11-24 17:10:14 +000023DEFAULT_BLACKLIST = ('\r\x00',)
24def clean_raw_line(raw_line, blacklist=DEFAULT_BLACKLIST):
25 """Strip blacklisted characters from raw_line."""
26 return re.sub('|'.join(blacklist), '', raw_line)
27
28
mbligh2895ce52008-04-17 15:40:51 +000029class status_stack(object):
jadmanski0afbb632008-06-06 21:10:57 +000030 def __init__(self):
jadmanskiba1fa662008-07-11 21:18:30 +000031 self.status_stack = [statuses[-1]]
mbligh2895ce52008-04-17 15:40:51 +000032
33
jadmanski0afbb632008-06-06 21:10:57 +000034 def current_status(self):
35 return self.status_stack[-1]
mbligh2895ce52008-04-17 15:40:51 +000036
37
jadmanski0afbb632008-06-06 21:10:57 +000038 def update(self, new_status):
jadmanskiba1fa662008-07-11 21:18:30 +000039 if new_status not in statuses:
jadmanski0afbb632008-06-06 21:10:57 +000040 return
jadmanskiba1fa662008-07-11 21:18:30 +000041 if is_worse_than(new_status, self.current_status()):
jadmanski0afbb632008-06-06 21:10:57 +000042 self.status_stack[-1] = new_status
mbligh2895ce52008-04-17 15:40:51 +000043
44
jadmanski0afbb632008-06-06 21:10:57 +000045 def start(self):
jadmanskiba1fa662008-07-11 21:18:30 +000046 self.status_stack.append(statuses[-1])
mbligh2895ce52008-04-17 15:40:51 +000047
48
jadmanski0afbb632008-06-06 21:10:57 +000049 def end(self):
50 result = self.status_stack.pop()
51 if len(self.status_stack) == 0:
jadmanskiba1fa662008-07-11 21:18:30 +000052 self.status_stack.append(statuses[-1])
jadmanski0afbb632008-06-06 21:10:57 +000053 return result
mbligh2895ce52008-04-17 15:40:51 +000054
55
jadmanski0afbb632008-06-06 21:10:57 +000056 def size(self):
57 return len(self.status_stack) - 1
mbligh2895ce52008-04-17 15:40:51 +000058
59
60class line_buffer(object):
jadmanski0afbb632008-06-06 21:10:57 +000061 def __init__(self):
62 self.buffer = collections.deque()
mbligh2895ce52008-04-17 15:40:51 +000063
64
jadmanski0afbb632008-06-06 21:10:57 +000065 def get(self):
66 return self.buffer.pop()
mbligh2895ce52008-04-17 15:40:51 +000067
68
jadmanski0afbb632008-06-06 21:10:57 +000069 def put(self, line):
70 self.buffer.appendleft(line)
mbligh2895ce52008-04-17 15:40:51 +000071
72
jadmanski0afbb632008-06-06 21:10:57 +000073 def put_multiple(self, lines):
74 self.buffer.extendleft(lines)
jadmanski6e8bf752008-05-14 00:17:48 +000075
76
jadmanski0afbb632008-06-06 21:10:57 +000077 def put_back(self, line):
78 self.buffer.append(line)
mbligh2895ce52008-04-17 15:40:51 +000079
80
jadmanski0afbb632008-06-06 21:10:57 +000081 def size(self):
82 return len(self.buffer)
mbligh2895ce52008-04-17 15:40:51 +000083
84
85def parser(version):
jadmanski0afbb632008-06-06 21:10:57 +000086 library = "autotest_lib.tko.parsers.version_%d" % version
87 module = __import__(library, globals(), locals(), ["parser"])
88 return module.parser()