Mercurial > hg > truffle
annotate mx/outputparser.py @ 4215:a2caa019ba3a
Fix mx : commands' scripts mx_init hook should be called before parsing command line arguments.
Fix mx : call the mx_post_parse_cmd_line hook from commands' scripts
OutputParser : cosmetic changes to logged output, return the retcode along yith the parsed output
Add a new Test class representing a sanity check and/or a benchmark
Port dacapo command to use this class, begning work on benchmarks
author | Gilles Duboscq <gilles.m.duboscq@gmail.com> |
---|---|
date | Wed, 04 Jan 2012 13:52:46 +0100 |
parents | cb22fcb2e2fc |
children | 47f7d91d34cf |
rev | line source |
---|---|
4151 | 1 import mx |
2 import commands | |
3 | |
4 class OutputParser: | |
5 | |
6 def __init__(self, nonZeroIsFatal=True): | |
7 self.matchers = [] | |
8 self.nonZeroIsFatal = nonZeroIsFatal | |
9 | |
10 def addMatcher(self, matcher): | |
11 self.matchers.append(matcher) | |
12 | |
4215
a2caa019ba3a
Fix mx : commands' scripts mx_init hook should be called before parsing command line arguments.
Gilles Duboscq <gilles.m.duboscq@gmail.com>
parents:
4151
diff
changeset
|
13 def parse(self, vm, cmd, cwd=None): |
4151 | 14 ret = [{}] |
15 | |
16 def parseLine(line): | |
17 anyMatch = False | |
18 for matcher in self.matchers: | |
4215
a2caa019ba3a
Fix mx : commands' scripts mx_init hook should be called before parsing command line arguments.
Gilles Duboscq <gilles.m.duboscq@gmail.com>
parents:
4151
diff
changeset
|
19 parsed = matcher.parse(line.strip()) |
4151 | 20 if parsed: |
21 anyMatch = True | |
22 if matcher.startNewLine and len(ret[0]) > 0: | |
23 ret.append({}) | |
24 ret[len(ret)-1].update(parsed) | |
25 if anyMatch : | |
4215
a2caa019ba3a
Fix mx : commands' scripts mx_init hook should be called before parsing command line arguments.
Gilles Duboscq <gilles.m.duboscq@gmail.com>
parents:
4151
diff
changeset
|
26 mx.log('>' + line.rstrip()) |
4151 | 27 else : |
4215
a2caa019ba3a
Fix mx : commands' scripts mx_init hook should be called before parsing command line arguments.
Gilles Duboscq <gilles.m.duboscq@gmail.com>
parents:
4151
diff
changeset
|
28 mx.log( line.rstrip()) |
4151 | 29 |
4215
a2caa019ba3a
Fix mx : commands' scripts mx_init hook should be called before parsing command line arguments.
Gilles Duboscq <gilles.m.duboscq@gmail.com>
parents:
4151
diff
changeset
|
30 retcode = commands.vm(cmd, nonZeroIsFatal=self.nonZeroIsFatal, out=parseLine, err=parseLine, cwd=cwd) |
a2caa019ba3a
Fix mx : commands' scripts mx_init hook should be called before parsing command line arguments.
Gilles Duboscq <gilles.m.duboscq@gmail.com>
parents:
4151
diff
changeset
|
31 return {'parsed' : ret, 'retcode' : retcode} |
4151 | 32 |
33 class Matcher: | |
34 | |
35 def __init__(self, regex, valuesToParse, startNewLine=False): | |
36 assert isinstance(valuesToParse, dict) | |
37 self.regex = regex | |
38 self.valuesToParse = valuesToParse | |
39 self.startNewLine = startNewLine | |
40 | |
41 def parse(self, line): | |
42 match = self.regex.search(line) | |
43 if not match: | |
44 return False | |
45 ret = {} | |
46 for key, value in self.valuesToParse.items(): | |
47 ret[self.parsestr(match, key)] = self.parsestr(match, value) | |
48 return ret | |
49 | |
50 def parsestr(self, match, key): | |
51 if isinstance(key, tuple): | |
52 if len(key) != 2: | |
53 raise Exception('Tuple arguments must have a lenght of 2') | |
54 tup1, tup2 = key | |
55 # check if key is a function | |
56 if hasattr(tup1, '__call__'): | |
57 return tup1(self.parsestr(match, tup2)) | |
58 elif hasattr(tup2, '__call__'): | |
59 return tup2(self.parsestr(match, tup1)) | |
60 else: | |
61 raise Exception('Tuple must contain a function pointer') | |
62 elif key.startswith('const:'): | |
63 return key.split(':')[1] | |
64 else: | |
65 return match.group(key) |