~joetalbott/uci-engine/user_auth

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# Ubuntu CI Engine
# Copyright 2014 Canonical Ltd.

# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import re
import subprocess


# test_name ... OK (0.1 secs)
pattern = re.compile('^(.*) \.\.\. .*\((.*) secs\)$')


def gen_timings(output):
    '''Filter out the test names and timing data into tuples.'''
    for line in output.split('\n'):
        try:
            test_name, seconds = re.match(pattern, line).groups()
        except AttributeError:
            continue
        seconds = float(seconds)
        yield test_name, seconds


def sort_timings(timings):
    '''Sort the timings based on the second value in the tuple, the time in
    seconds.'''
    return sorted(timings, cmp=lambda x, y: cmp(x[1], y[1]))


def run_tests(args, test_command=['./run-tests']):
    '''Call ./run-tests with any arguments passed to this program.

    :return: stderr
    '''
    cmd = test_command + args
    # Drop stderr. The text output from tests is not needed here.
    with open('/dev/null', 'a') as devnull:
        kwargs = {'stdout': subprocess.PIPE, 'stderr': devnull}
        p = subprocess.Popen(cmd, **kwargs)
    try:
        return p.communicate()[0]
    except KeyboardInterrupt:
        # Clean up the subprocess when we've interrupted this script.
        if p.pid:
            # Terminate.
            os.kill(p.pid, 15)
        return None


def print_timings(timings):
    for test_name, seconds in timings:
        print '%s: %s' % (test_name, seconds)


def print_total_timing(timings):
    total = sum([x[1] for x in timings])
    output = 'Total test time: %.2f seconds' % total
    print
    print '-' * len(output)
    print output


if __name__ == '__main__':
    output = run_tests(sys.argv[1:])
    if output is None:
        sys.exit(0)
    timings = gen_timings(output)
    sorted_timings = sort_timings(timings)
    print_timings(sorted_timings)
    print_total_timing(sorted_timings)