parse_log.py 1.62 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
def parse(file, parse_times = True):
    '''[file] should be a file-like object, an iterator over the lines.
       yields a list of Result objects.'''
11 12 13 14 15 16 17 18 19
    commit_re = re.compile("^# ([a-z0-9]+)$")
    time_re = re.compile("^([a-zA-Z0-9_/-]+) \(user: ([0-9.]+) mem: ([0-9]+) ko\)$")
    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
20
            if commit is not None:
21 22 23
                yield Result(commit, times)
            # start recording next commit
            commit = m.group(1)
24 25
            if parse_times:
                times = {} # reset the recorded times
26 27 28 29
            continue
        # next file time?
        m = time_re.match(line)
        if m is not None:
30 31 32
            if times is not None:
                name = m.group(1)
                time = float(m.group(2))
33 34
                times[name] = time
            continue
35
        # nothing else we know about, ignore
36
    # end of file. previous commit, if any, is done now.
37
    if commit is not None:
38
        yield Result(commit, times)
39 40 41 42 43 44 45 46 47 48

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])
    return reversed(commits.decode("utf-8").strip().split('\n'))