mbligh | 51ee7c7 | 2008-11-24 17:10:14 +0000 | [diff] [blame] | 1 | import collections, re |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 2 | import common |
mbligh | 1b3b376 | 2008-09-25 02:46:34 +0000 | [diff] [blame] | 3 | from autotest_lib.client.common_lib import log |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 4 | |
| 5 | |
mbligh | 1b3b376 | 2008-09-25 02:46:34 +0000 | [diff] [blame] | 6 | statuses = log.job_statuses |
jadmanski | ba1fa66 | 2008-07-11 21:18:30 +0000 | [diff] [blame] | 7 | |
| 8 | |
| 9 | def 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 | |
| 15 | def 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 | |
mbligh | 51ee7c7 | 2008-11-24 17:10:14 +0000 | [diff] [blame] | 23 | DEFAULT_BLACKLIST = ('\r\x00',) |
| 24 | def 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 | |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 29 | class status_stack(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 30 | def __init__(self): |
jadmanski | ba1fa66 | 2008-07-11 21:18:30 +0000 | [diff] [blame] | 31 | self.status_stack = [statuses[-1]] |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 32 | |
| 33 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 34 | def current_status(self): |
| 35 | return self.status_stack[-1] |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 36 | |
| 37 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 38 | def update(self, new_status): |
jadmanski | ba1fa66 | 2008-07-11 21:18:30 +0000 | [diff] [blame] | 39 | if new_status not in statuses: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 40 | return |
jadmanski | ba1fa66 | 2008-07-11 21:18:30 +0000 | [diff] [blame] | 41 | if is_worse_than(new_status, self.current_status()): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 42 | self.status_stack[-1] = new_status |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 43 | |
| 44 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 45 | def start(self): |
jadmanski | ba1fa66 | 2008-07-11 21:18:30 +0000 | [diff] [blame] | 46 | self.status_stack.append(statuses[-1]) |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 47 | |
| 48 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 49 | def end(self): |
| 50 | result = self.status_stack.pop() |
| 51 | if len(self.status_stack) == 0: |
jadmanski | ba1fa66 | 2008-07-11 21:18:30 +0000 | [diff] [blame] | 52 | self.status_stack.append(statuses[-1]) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 53 | return result |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 54 | |
| 55 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 56 | def size(self): |
| 57 | return len(self.status_stack) - 1 |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | class line_buffer(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 61 | def __init__(self): |
| 62 | self.buffer = collections.deque() |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 63 | |
| 64 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 65 | def get(self): |
| 66 | return self.buffer.pop() |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 67 | |
| 68 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 69 | def put(self, line): |
| 70 | self.buffer.appendleft(line) |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 71 | |
| 72 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 73 | def put_multiple(self, lines): |
| 74 | self.buffer.extendleft(lines) |
jadmanski | 6e8bf75 | 2008-05-14 00:17:48 +0000 | [diff] [blame] | 75 | |
| 76 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 77 | def put_back(self, line): |
| 78 | self.buffer.append(line) |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 79 | |
| 80 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 81 | def size(self): |
| 82 | return len(self.buffer) |
mbligh | 2895ce5 | 2008-04-17 15:40:51 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def parser(version): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 86 | library = "autotest_lib.tko.parsers.version_%d" % version |
| 87 | module = __import__(library, globals(), locals(), ["parser"]) |
| 88 | return module.parser() |