comparison mx/outputparser.py @ 7563:3aab15f42934

moved execution of a benchmark out of OutputParser
author Doug Simon <doug.simon@oracle.com>
date Wed, 30 Jan 2013 11:03:32 +0100
parents 063ea022532c
children c420a487b10f
comparison
equal deleted inserted replaced
7562:1c09bcebd61f 7563:3aab15f42934
21 # or visit www.oracle.com if you need additional information or have any 21 # or visit www.oracle.com if you need additional information or have any
22 # questions. 22 # questions.
23 # 23 #
24 # ---------------------------------------------------------------------------------------------------- 24 # ----------------------------------------------------------------------------------------------------
25 25
26 import mx
27 import commands
28 import subprocess
29
30 class OutputParser: 26 class OutputParser:
31 27
32 def __init__(self, nonZeroIsFatal=True): 28 def __init__(self):
33 self.matchers = [] 29 self.matchers = []
34 self.nonZeroIsFatal = nonZeroIsFatal
35 30
36 def addMatcher(self, matcher): 31 def addMatcher(self, matcher):
37 self.matchers.append(matcher) 32 self.matchers.append(matcher)
38 33
39 def parse(self, vm, cmd, cwd=None, vmbuild=None): 34 def parse(self, output):
40 ret = [] 35 records = []
41 36 for matcher in self.matchers:
42 def parseLine(line): 37 record = matcher.parse(output)
43 anyMatch = False 38 if record:
44 for matcher in self.matchers: 39 records.append(record)
45 parsed = matcher.parse(line.strip()) 40 return records
46 if parsed:
47 anyMatch = True
48 if len(ret) is 0 or (matcher.startNewLine and len(ret[len(ret)-1]) > 0):
49 ret.append({})
50 ret[len(ret)-1].update(parsed)
51 if anyMatch :
52 mx.log('>' + line.rstrip())
53 else :
54 mx.log( line.rstrip())
55
56 retcode = commands.vm(cmd, vm, nonZeroIsFatal=self.nonZeroIsFatal, out=parseLine, err=subprocess.STDOUT, cwd=cwd, vmbuild=vmbuild)
57 return {'parsed' : ret, 'retcode' : retcode}
58 41
59 class Matcher: 42 class Matcher:
60 43
61 def __init__(self, regex, valuesToParse, startNewLine=False): 44 def __init__(self, regex, valuesTemplate):
62 assert isinstance(valuesToParse, dict) 45 assert isinstance(valuesTemplate, dict)
63 self.regex = regex 46 self.regex = regex
64 self.valuesToParse = valuesToParse 47 self.valuesTemplate = valuesTemplate
65 self.startNewLine = startNewLine
66 48
67 def parse(self, line): 49 def parse(self, text):
68 match = self.regex.search(line) 50 match = self.regex.search(text)
69 if not match: 51 if not match:
70 return False 52 return False
71 ret = {} 53 values = {}
72 for key, value in self.valuesToParse.items(): 54 for key, value in self.valuesTemplate.items():
73 ret[self.parsestr(match, key)] = self.parsestr(match, value) 55 values[self.get_value_or_const(match, key)] = self.get_value_or_const(match, value)
74 return ret 56
57 return values
58
75 59
76 def parsestr(self, match, key): 60 def get_value_or_const(self, match, name):
77 if isinstance(key, tuple): 61 if name.startswith('const:'):
78 if len(key) != 2: 62 return name.split(':')[1]
79 raise Exception('Tuple arguments must have a length of 2')
80 tup1, tup2 = key
81 # check if key is a function
82 if hasattr(tup1, '__call__'):
83 return tup1(self.parsestr(match, tup2))
84 elif hasattr(tup2, '__call__'):
85 return tup2(self.parsestr(match, tup1))
86 else:
87 raise Exception('Tuple must contain a function pointer')
88 elif key.startswith('const:'):
89 return key.split(':')[1]
90 else: 63 else:
91 return match.group(key) 64 return match.group(name)