diff options
-rw-r--r-- | meta/lib/oeqa/utils/logparser.py | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/meta/lib/oeqa/utils/logparser.py b/meta/lib/oeqa/utils/logparser.py new file mode 100644 index 0000000000..87b50354cd --- /dev/null +++ b/meta/lib/oeqa/utils/logparser.py | |||
@@ -0,0 +1,125 @@ | |||
1 | #!/usr/bin/env python | ||
2 | |||
3 | import sys | ||
4 | import os | ||
5 | import re | ||
6 | import ftools | ||
7 | |||
8 | |||
9 | # A parser that can be used to identify weather a line is a test result or a section statement. | ||
10 | class Lparser(object): | ||
11 | |||
12 | def __init__(self, test_0_pass_regex, test_0_fail_regex, section_0_begin_regex=None, section_0_end_regex=None, **kwargs): | ||
13 | # Initialize the arguments dictionary | ||
14 | if kwargs: | ||
15 | self.args = kwargs | ||
16 | else: | ||
17 | self.args = {} | ||
18 | |||
19 | # Add the default args to the dictionary | ||
20 | self.args['test_0_pass_regex'] = test_0_pass_regex | ||
21 | self.args['test_0_fail_regex'] = test_0_fail_regex | ||
22 | if section_0_begin_regex: | ||
23 | self.args['section_0_begin_regex'] = section_0_begin_regex | ||
24 | if section_0_end_regex: | ||
25 | self.args['section_0_end_regex'] = section_0_end_regex | ||
26 | |||
27 | self.test_possible_status = ['pass', 'fail', 'error'] | ||
28 | self.section_possible_status = ['begin', 'end'] | ||
29 | |||
30 | self.initialized = False | ||
31 | |||
32 | |||
33 | # Initialize the parser with the current configuration | ||
34 | def init(self): | ||
35 | |||
36 | # extra arguments can be added by the user to define new test and section categories. They must follow a pre-defined pattern: <type>_<category_name>_<status>_regex | ||
37 | self.test_argument_pattern = "^test_(.+?)_(%s)_regex" % '|'.join(map(str, self.test_possible_status)) | ||
38 | self.section_argument_pattern = "^section_(.+?)_(%s)_regex" % '|'.join(map(str, self.section_possible_status)) | ||
39 | |||
40 | # Initialize the test and section regex dictionaries | ||
41 | self.test_regex = {} | ||
42 | self.section_regex ={} | ||
43 | |||
44 | for arg, value in self.args.items(): | ||
45 | if not value: | ||
46 | raise Exception('The value of provided argument %s is %s. Should have a valid value.' % (key, value)) | ||
47 | is_test = re.search(self.test_argument_pattern, arg) | ||
48 | is_section = re.search(self.section_argument_pattern, arg) | ||
49 | if is_test: | ||
50 | if not is_test.group(1) in self.test_regex: | ||
51 | self.test_regex[is_test.group(1)] = {} | ||
52 | self.test_regex[is_test.group(1)][is_test.group(2)] = re.compile(value) | ||
53 | elif is_section: | ||
54 | if not is_section.group(1) in self.section_regex: | ||
55 | self.section_regex[is_section.group(1)] = {} | ||
56 | self.section_regex[is_section.group(1)][is_section.group(2)] = re.compile(value) | ||
57 | else: | ||
58 | # TODO: Make these call a traceback instead of a simple exception.. | ||
59 | raise Exception("The provided argument name does not correspond to any valid type. Please give one of the following types:\nfor tests: %s\nfor sections: %s" % (self.test_argument_pattern, self.section_argument_pattern)) | ||
60 | |||
61 | self.initialized = True | ||
62 | |||
63 | # Parse a line and return a tuple containing the type of result (test/section) and its category, status and name | ||
64 | def parse_line(self, line): | ||
65 | if not self.initialized: | ||
66 | raise Exception("The parser is not initialized..") | ||
67 | |||
68 | for test_category, test_status_list in self.test_regex.items(): | ||
69 | for test_status, status_regex in test_status_list.items(): | ||
70 | test_name = status_regex.search(line) | ||
71 | if test_name: | ||
72 | return ['test', test_category, test_status, test_name.group(1)] | ||
73 | |||
74 | for section_category, section_status_list in self.section_regex.items(): | ||
75 | for section_status, status_regex in section_status_list.items(): | ||
76 | section_name = status_regex.search(line) | ||
77 | if section_name: | ||
78 | return ['section', section_category, section_status, section_name.group(1)] | ||
79 | return None | ||
80 | |||
81 | |||
82 | class Result(object): | ||
83 | |||
84 | def __init__(self): | ||
85 | self.result_dict = {} | ||
86 | |||
87 | def store(self, section, test, status): | ||
88 | if not section in self.result_dict: | ||
89 | self.result_dict[section] = [] | ||
90 | |||
91 | self.result_dict[section].append((test, status)) | ||
92 | |||
93 | # sort tests by the test name(the first element of the tuple), for each section. This can be helpful when using git to diff for changes by making sure they are always in the same order. | ||
94 | def sort_tests(self): | ||
95 | for package in self.result_dict: | ||
96 | sorted_results = sorted(self.result_dict[package], key=lambda tup: tup[0]) | ||
97 | self.result_dict[package] = sorted_results | ||
98 | |||
99 | # Log the results as files. The file name is the section name and the contents are the tests in that section. | ||
100 | def log_as_files(self, target_dir, test_status): | ||
101 | status_regex = re.compile('|'.join(map(str, test_status))) | ||
102 | if not type(test_status) == type([]): | ||
103 | raise Exception("test_status should be a list. Got " + str(test_status) + " instead.") | ||
104 | if not os.path.exists(target_dir): | ||
105 | raise Exception("Target directory does not exist: %s" % target_dir) | ||
106 | |||
107 | for section, test_results in self.result_dict.items(): | ||
108 | prefix = '' | ||
109 | for x in test_status: | ||
110 | prefix +=x+'.' | ||
111 | if (section != ''): | ||
112 | prefix += section | ||
113 | section_file = os.path.join(target_dir, prefix) | ||
114 | # purge the file contents if it exists | ||
115 | open(section_file, 'w').close() | ||
116 | for test_result in test_results: | ||
117 | (test_name, status) = test_result | ||
118 | # we log only the tests with status in the test_status list | ||
119 | match_status = status_regex.search(status) | ||
120 | if match_status: | ||
121 | ftools.append_file(section_file, status + ": " + test_name) | ||
122 | |||
123 | # Not yet implemented! | ||
124 | def log_to_lava(self): | ||
125 | pass | ||