Add python-eventlet package to MOS 9.0 repository
[packages/trusty/python-eventlet.git] / tests / parse_results.py
1 import sys
2 import os
3 import traceback
4 try:
5     import sqlite3
6 except ImportError:
7     import pysqlite2.dbapi2 as sqlite3
8 import re
9 import glob
10
11
12 def parse_stdout(s):
13     argv = re.search('^===ARGV=(.*?)$', s, re.M).group(1)
14     argv = argv.split()
15     testname = argv[-1]
16     del argv[-1]
17     hub = None
18     reactor = None
19     while argv:
20         if argv[0] == '--hub':
21             hub = argv[1]
22             del argv[0]
23             del argv[0]
24         elif argv[0] == '--reactor':
25             reactor = argv[1]
26             del argv[0]
27             del argv[0]
28         else:
29             del argv[0]
30     if reactor is not None:
31         hub += '/%s' % reactor
32     return testname, hub
33
34 unittest_delim = '----------------------------------------------------------------------'
35
36
37 def parse_unittest_output(s):
38     s = s[s.rindex(unittest_delim) + len(unittest_delim):]
39     num = int(re.search('^Ran (\d+) test.*?$', s, re.M).group(1))
40     ok = re.search('^OK$', s, re.M)
41     error, fail, timeout = 0, 0, 0
42     failed_match = re.search(r'^FAILED \((?:failures=(?P<f>\d+))?,? ?(?:errors=(?P<e>\d+))?\)$', s, re.M)
43     ok_match = re.search('^OK$', s, re.M)
44     if failed_match:
45         assert not ok_match, (ok_match, s)
46         fail = failed_match.group('f')
47         error = failed_match.group('e')
48         fail = int(fail or '0')
49         error = int(error or '0')
50     else:
51         assert ok_match, repr(s)
52     timeout_match = re.search('^===disabled because of timeout: (\d+)$', s, re.M)
53     if timeout_match:
54         timeout = int(timeout_match.group(1))
55     return num, error, fail, timeout
56
57
58 def main(db):
59     c = sqlite3.connect(db)
60     c.execute('''create table if not exists parsed_command_record
61               (id integer not null unique,
62                testname text,
63                hub text,
64                runs integer,
65                errors integer,
66                fails integer,
67                timeouts integer,
68                error_names text,
69                fail_names text,
70                timeout_names text)''')
71     c.commit()
72
73     parse_error = 0
74
75     SQL = ('select command_record.id, command, stdout, exitcode from command_record '
76            'where not exists (select * from parsed_command_record where '
77            'parsed_command_record.id=command_record.id)')
78     for row in c.execute(SQL).fetchall():
79         id, command, stdout, exitcode = row
80         try:
81             testname, hub = parse_stdout(stdout)
82             if unittest_delim in stdout:
83                 runs, errors, fails, timeouts = parse_unittest_output(stdout)
84             else:
85                 if exitcode == 0:
86                     runs, errors, fails, timeouts = 1, 0, 0, 0
87                 if exitcode == 7:
88                     runs, errors, fails, timeouts = 0, 0, 0, 1
89                 elif exitcode:
90                     runs, errors, fails, timeouts = 1, 1, 0, 0
91         except Exception:
92             parse_error += 1
93             sys.stderr.write('Failed to parse id=%s\n' % id)
94             print(repr(stdout))
95             traceback.print_exc()
96         else:
97             print(id, hub, testname, runs, errors, fails, timeouts)
98             c.execute('insert into parsed_command_record '
99                       '(id, testname, hub, runs, errors, fails, timeouts) '
100                       'values (?, ?, ?, ?, ?, ?, ?)',
101                       (id, testname, hub, runs, errors, fails, timeouts))
102             c.commit()
103
104 if __name__ == '__main__':
105     if not sys.argv[1:]:
106         latest_db = sorted(glob.glob('results.*.db'), key=lambda f: os.stat(f).st_mtime)[-1]
107         print(latest_db)
108         sys.argv.append(latest_db)
109     for db in sys.argv[1:]:
110         main(db)
111     execfile('generate_report.py')