Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | Greps and returns the first svn log entry containing a line matching the regular |
| 5 | expression pattern passed as the only arg. |
| 6 | |
| 7 | Example: |
| 8 | |
| 9 | svn log -v | grep-svn-log.py '^ D.+why_are_you_missing.h$' |
| 10 | """ |
| 11 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 12 | import fileinput |
| 13 | import re |
| 14 | import sys |
| 15 | import StringIO |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 16 | |
| 17 | # Separator string for "svn log -v" output. |
| 18 | separator = '-' * 72 |
| 19 | |
| 20 | usage = """Usage: grep-svn-log.py line-pattern |
| 21 | Example: |
| 22 | svn log -v | grep-svn-log.py '^ D.+why_are_you_missing.h'""" |
| 23 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 25 | class Log(StringIO.StringIO): |
| 26 | """Simple facade to keep track of the log content.""" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 27 | |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 28 | def __init__(self): |
| 29 | self.reset() |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 31 | def add_line(self, a_line): |
| 32 | """Add a line to the content, if there is a previous line, commit it.""" |
| 33 | global separator |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | if self.prev_line is not None: |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 35 | print >> self, self.prev_line |
| 36 | self.prev_line = a_line |
| 37 | self.separator_added = (a_line == separator) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 39 | def del_line(self): |
| 40 | """Forget about the previous line, do not commit it.""" |
| 41 | self.prev_line = None |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 43 | def reset(self): |
| 44 | """Forget about the previous lines entered.""" |
| 45 | StringIO.StringIO.__init__(self) |
| 46 | self.prev_line = None |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 48 | def finish(self): |
| 49 | """Call this when you're finished with populating content.""" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | if self.prev_line is not None: |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 51 | print >> self, self.prev_line |
| 52 | self.prev_line = None |
| 53 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 55 | def grep(regexp): |
| 56 | # The log content to be written out once a match is found. |
| 57 | log = Log() |
| 58 | |
| 59 | LOOKING_FOR_MATCH = 0 |
| 60 | FOUND_LINE_MATCH = 1 |
| 61 | state = LOOKING_FOR_MATCH |
| 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | while True: |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 64 | line = sys.stdin.readline() |
| 65 | if not line: |
| 66 | return |
| 67 | line = line.splitlines()[0] |
| 68 | if state == FOUND_LINE_MATCH: |
| 69 | # At this state, we keep on accumulating lines until the separator |
| 70 | # is encountered. At which point, we can return the log content. |
| 71 | if line == separator: |
Johnny Chen | 641e95d | 2012-03-05 18:25:29 +0000 | [diff] [blame] | 72 | log.finish() |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 73 | print log.getvalue() |
| 74 | return |
| 75 | log.add_line(line) |
| 76 | |
| 77 | elif state == LOOKING_FOR_MATCH: |
| 78 | if line == separator: |
| 79 | log.reset() |
| 80 | log.add_line(line) |
| 81 | # Update next state if necessary. |
| 82 | if regexp.search(line): |
| 83 | state = FOUND_LINE_MATCH |
| 84 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 86 | def main(): |
| 87 | if len(sys.argv) != 2: |
| 88 | print usage |
| 89 | sys.exit(0) |
| 90 | |
| 91 | regexp = re.compile(sys.argv[1]) |
| 92 | grep(regexp) |
Johnny Chen | 641e95d | 2012-03-05 18:25:29 +0000 | [diff] [blame] | 93 | sys.stdin.close() |
Johnny Chen | e79a8d3 | 2011-11-04 01:05:29 +0000 | [diff] [blame] | 94 | |
| 95 | if __name__ == '__main__': |
| 96 | main() |