summaryrefslogtreecommitdiffstats
path: root/lldb/utils/misc/grep-svn-log.py
blob: 86cc3ef1742f1f1d714008afdc732a2565b4fda0 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python

"""
Greps and returns the first svn log entry containing a line matching the regular
expression pattern passed as the only arg.

Example:

svn log -v | grep-svn-log.py '^   D.+why_are_you_missing.h$'
"""

import fileinput
import re
import sys
import StringIO

# Separator string for "svn log -v" output.
separator = '-' * 72

usage = """Usage: grep-svn-log.py line-pattern
Example:
    svn log -v | grep-svn-log.py '^   D.+why_are_you_missing.h'"""


class Log(StringIO.StringIO):
    """Simple facade to keep track of the log content."""

    def __init__(self):
        self.reset()

    def add_line(self, a_line):
        """Add a line to the content, if there is a previous line, commit it."""
        global separator
        if self.prev_line is not None:
            print >> self, self.prev_line
        self.prev_line = a_line
        self.separator_added = (a_line == separator)

    def del_line(self):
        """Forget about the previous line, do not commit it."""
        self.prev_line = None

    def reset(self):
        """Forget about the previous lines entered."""
        StringIO.StringIO.__init__(self)
        self.prev_line = None

    def finish(self):
        """Call this when you're finished with populating content."""
        if self.prev_line is not None:
            print >> self, self.prev_line
        self.prev_line = None


def grep(regexp):
    # The log content to be written out once a match is found.
    log = Log()

    LOOKING_FOR_MATCH = 0
    FOUND_LINE_MATCH = 1
    state = LOOKING_FOR_MATCH

    while True:
        line = sys.stdin.readline()
        if not line:
            return
        line = line.splitlines()[0]
        if state == FOUND_LINE_MATCH:
            # At this state, we keep on accumulating lines until the separator
            # is encountered.  At which point, we can return the log content.
            if line == separator:
                log.finish()
                print log.getvalue()
                return
            log.add_line(line)

        elif state == LOOKING_FOR_MATCH:
            if line == separator:
                log.reset()
            log.add_line(line)
            # Update next state if necessary.
            if regexp.search(line):
                state = FOUND_LINE_MATCH


def main():
    if len(sys.argv) != 2:
        print usage
        sys.exit(0)

    regexp = re.compile(sys.argv[1])
    grep(regexp)
    sys.stdin.close()

if __name__ == '__main__':
    main()
OpenPOWER on IntegriCloud