regressdiff 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python
  2. #
  3. # Walk through a pair of textfiles looking for where they begin to differ.
  4. # May be useful for comparing logs when regression tests break.
  5. #
  6. # This file is Copyright (c) 2010-2019 by the GPSD project
  7. # BSD terms apply: see the file COPYING in the distribution root for details.
  8. #
  9. # This code runs compatibly under Python 2 and 3.x for x >= 2.
  10. # Preserve this property!
  11. from __future__ import absolute_import, print_function, division
  12. import sys
  13. class BufferedFile(object):
  14. def __init__(self, name):
  15. self.file = open(name)
  16. self.linebuffer = []
  17. self.lineno = 0
  18. def readline(self):
  19. self.lineno += 1
  20. if self.linebuffer:
  21. return self.linebuffer.pop()
  22. else:
  23. return self.file.readline()
  24. def pushback(self, line):
  25. self.lineno -= 1
  26. self.linebuffer.append(line)
  27. def peek(self):
  28. return self.linebuffer[-1]
  29. def eatspan(f1, f2):
  30. consumed = 0
  31. while True:
  32. line1 = f1.readline()
  33. line2 = f2.readline()
  34. if line1 and line2 and line1 == line2:
  35. consumed += 1
  36. continue
  37. f1.pushback(line1)
  38. f2.pushback(line2)
  39. return consumed
  40. if __name__ == "__main__":
  41. f1 = BufferedFile(sys.argv[1])
  42. f2 = BufferedFile(sys.argv[2])
  43. eaten = eatspan(f1, f2)
  44. print("First %d lines match" % eaten)
  45. print(f1.peek())
  46. print(f2.peek())