diff 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
line wrap: on
line diff
--- a/mx/outputparser.py	Sun Jan 27 23:09:56 2013 +0100
+++ b/mx/outputparser.py	Wed Jan 30 11:03:32 2013 +0100
@@ -23,69 +23,42 @@
 #
 # ----------------------------------------------------------------------------------------------------
 
-import mx
-import commands
-import subprocess
-
 class OutputParser:
     
-    def __init__(self, nonZeroIsFatal=True):
+    def __init__(self):
         self.matchers = []
-        self.nonZeroIsFatal = nonZeroIsFatal
         
     def addMatcher(self, matcher):
         self.matchers.append(matcher)
     
-    def parse(self, vm, cmd, cwd=None, vmbuild=None):
-        ret = []
-        
-        def parseLine(line):
-            anyMatch = False
-            for matcher in self.matchers:
-                parsed = matcher.parse(line.strip())
-                if parsed:
-                    anyMatch = True
-                    if len(ret) is 0 or (matcher.startNewLine and len(ret[len(ret)-1]) > 0):
-                        ret.append({})
-                    ret[len(ret)-1].update(parsed)
-            if anyMatch :
-                mx.log('>' + line.rstrip())
-            else :
-                mx.log( line.rstrip())
-        
-        retcode = commands.vm(cmd, vm, nonZeroIsFatal=self.nonZeroIsFatal, out=parseLine, err=subprocess.STDOUT, cwd=cwd, vmbuild=vmbuild)
-        return {'parsed' : ret, 'retcode' : retcode}
+    def parse(self, output):
+        records = []
+        for matcher in self.matchers:
+            record = matcher.parse(output)
+            if record:
+                records.append(record)
+        return records
 
 class Matcher:
     
-    def __init__(self, regex, valuesToParse, startNewLine=False):
-        assert isinstance(valuesToParse, dict)
+    def __init__(self, regex, valuesTemplate):
+        assert isinstance(valuesTemplate, dict)
         self.regex = regex
-        self.valuesToParse = valuesToParse
-        self.startNewLine = startNewLine
+        self.valuesTemplate = valuesTemplate
         
-    def parse(self, line):
-        match = self.regex.search(line)
+    def parse(self, text):
+        match = self.regex.search(text)
         if not match:
             return False
-        ret = {}
-        for key, value in self.valuesToParse.items():
-            ret[self.parsestr(match, key)] = self.parsestr(match, value)
-        return ret
+        values = {}
+        for key, value in self.valuesTemplate.items():
+            values[self.get_value_or_const(match, key)] = self.get_value_or_const(match, value)
+                    
+        return values
+    
         
-    def parsestr(self, match, key):
-        if isinstance(key, tuple):
-            if len(key) != 2:
-                raise Exception('Tuple arguments must have a length of 2')
-            tup1, tup2 = key
-            # check if key is a function
-            if hasattr(tup1, '__call__'):
-                return tup1(self.parsestr(match, tup2))
-            elif hasattr(tup2, '__call__'):
-                return tup2(self.parsestr(match, tup1))
-            else:
-                raise Exception('Tuple must contain a function pointer')
-        elif key.startswith('const:'):
-            return key.split(':')[1]
+    def get_value_or_const(self, match, name):
+        if name.startswith('const:'):
+            return name.split(':')[1]
         else:
-            return match.group(key)
+            return match.group(name)