parse_log.py 1.91 KB
Newer Older
1
import re, subprocess
2
3
4
5
6
7

class Result:
    def __init__(self, commit, times):
        self.commit = commit
        self.times = times

8
9
10
11
12
PARSE_NOT  = 0
PARSE_RAW  = 1
PARSE_FULL = 2

def parse(file, parse_times = PARSE_FULL):
13
14
    '''[file] should be a file-like object, an iterator over the lines.
       yields a list of Result objects.'''
15
    commit_re = re.compile("^# ([a-z0-9]+)$")
Ralf Jung's avatar
Ralf Jung committed
16
    time_re = re.compile("^([a-zA-Z0-9_/-]+) \((real|user): ([0-9.]+).* mem: ([0-9]+) ko\)$")
17
18
19
20
21
22
23
    commit = None
    times = None
    for line in file:
        # next commit?
        m = commit_re.match(line)
        if m is not None:
            # previous commit, if any, is done now
24
            if commit is not None:
25
26
27
                yield Result(commit, times)
            # start recording next commit
            commit = m.group(1)
28
29
            if parse_times != PARSE_NOT:
                times = [] if parse_times == PARSE_RAW else {} # reset the recorded times
30
31
32
33
            continue
        # next file time?
        m = time_re.match(line)
        if m is not None:
34
            if times is not None:
35
36
37
38
39
40
                if parse_times == PARSE_RAW:
                    times.append(line)
                else:
                    name = m.group(1)
                    time = float(m.group(2))
                    times[name] = time
41
            continue
42
        # nothing else we know about, ignore
43
    # end of file. previous commit, if any, is done now.
44
    if commit is not None:
45
        yield Result(commit, times)
46
47
48
49
50
51
52
53
54

def parse_git_commits(commits):
    '''Returns an iterable of SHA1s'''
    if commits.find('..') >= 0:
        # a range of commits
        commits = subprocess.check_output(["git", "rev-list", commits])
    else:
        # a single commit
        commits = subprocess.check_output(["git", "rev-parse", commits])
55
56
57
58
    output = commits.decode("utf-8").strip()
    if not output: # empty output
        return []
    return reversed(output.split('\n'))