Add python-eventlet 0.16.1
[packages/trusty/python-eventlet.git] / eventlet / 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(
43         r'^FAILED \((?:failures=(?P<f>\d+))?,? ?(?:errors=(?P<e>\d+))?\)$', s, re.M)
44     ok_match = re.search('^OK$', s, re.M)
45     if failed_match:
46         assert not ok_match, (ok_match, s)
47         fail = failed_match.group('f')
48         error = failed_match.group('e')
49         fail = int(fail or '0')
50         error = int(error or '0')
51     else:
52         assert ok_match, repr(s)
53     timeout_match = re.search('^===disabled because of timeout: (\d+)$', s, re.M)
54     if timeout_match:
55         timeout = int(timeout_match.group(1))
56     return num, error, fail, timeout
57
58
59 def main(db):
60     c = sqlite3.connect(db)
61     c.execute('''create table if not exists parsed_command_record
62               (id integer not null unique,
63                testname text,
64                hub text,
65                runs integer,
66                errors integer,
67                fails integer,
68                timeouts integer,
69                error_names text,
70                fail_names text,
71                timeout_names text)''')
72     c.commit()
73
74     parse_error = 0
75
76     SQL = ('select command_record.id, command, stdout, exitcode from command_record '
77            'where not exists (select * from parsed_command_record where '
78            'parsed_command_record.id=command_record.id)')
79     for row in c.execute(SQL).fetchall():
80         id, command, stdout, exitcode = row
81         try:
82             testname, hub = parse_stdout(stdout)
83             if unittest_delim in stdout:
84                 runs, errors, fails, timeouts = parse_unittest_output(stdout)
85             else:
86                 if exitcode == 0:
87                     runs, errors, fails, timeouts = 1, 0, 0, 0
88                 if exitcode == 7:
89                     runs, errors, fails, timeouts = 0, 0, 0, 1
90                 elif exitcode:
91                     runs, errors, fails, timeouts = 1, 1, 0, 0
92         except Exception:
93             parse_error += 1
94             sys.stderr.write('Failed to parse id=%s\n' % id)
95             print(repr(stdout))
96             traceback.print_exc()
97         else:
98             print(id, hub, testname, runs, errors, fails, timeouts)
99             c.execute('insert into parsed_command_record '
100                       '(id, testname, hub, runs, errors, fails, timeouts) '
101                       'values (?, ?, ?, ?, ?, ?, ?)',
102                       (id, testname, hub, runs, errors, fails, timeouts))
103             c.commit()
104
105 if __name__ == '__main__':
106     if not sys.argv[1:]:
107         latest_db = sorted(glob.glob('results.*.db'), key=lambda f: os.stat(f).st_mtime)[-1]
108         print(latest_db)
109         sys.argv.append(latest_db)
110     for db in sys.argv[1:]:
111         main(db)
112     execfile('generate_report.py')