File: regressdiff

package info (click to toggle)
gpsd 3.27-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,056 kB
  • sloc: ansic: 74,438; python: 16,521; sh: 890; cpp: 848; php: 225; makefile: 197; perl: 111; javascript: 26; xml: 11
file content (58 lines) | stat: -rwxr-xr-x 1,424 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
#
# Walk through a pair of textfiles looking for where they begin to differ.
# May be useful for comparing logs when regression tests break.
#
# This file is Copyright 2010 by the GPSD project
# SPDX-License-Identifier: BSD-2-clause
#
# This code runs compatibly under Python 2 and 3.x for x >= 2.
# Preserve this property!
from __future__ import absolute_import, print_function, division

import sys


class BufferedFile(object):
    def __init__(self, name):
        self.file = open(name)
        self.linebuffer = []
        self.lineno = 0

    def readline(self):
        self.lineno += 1
        if self.linebuffer:
            return self.linebuffer.pop()
        else:
            return self.file.readline()

    def pushback(self, line):
        self.lineno -= 1
        self.linebuffer.append(line)

    def peek(self):
        return self.linebuffer[-1]


def eatspan(f1, f2):
    consumed = 0
    while True:
        line1 = f1.readline()
        line2 = f2.readline()
        if line1 and line2 and line1 == line2:
            consumed += 1
            continue
        f1.pushback(line1)
        f2.pushback(line2)
        return consumed


if __name__ == "__main__":
    f1 = BufferedFile(sys.argv[1])
    f2 = BufferedFile(sys.argv[2])

    eaten = eatspan(f1, f2)
    print("First %d lines match" % eaten)
    print(f1.peek())
    print(f2.peek())
# vim: set expandtab shiftwidth=4