Cleanup diff

Doesn't do --color anymore.  That will go into a new hb-diff-colorize
tool.
This commit is contained in:
Behdad Esfahbod 2012-05-08 22:44:21 +02:00
parent 7d22135b4c
commit 9155e4ffe0
2 changed files with 21 additions and 21 deletions

View File

@ -3,12 +3,8 @@
from hb_test_tools import * from hb_test_tools import *
import sys, os import sys, os
colors = Colors.Auto (sys.argv) if len (sys.argv) < 3:
print "usage: %s file1 file2..." % sys.argv[0]
if len (sys.argv) != 3:
print "usage: %s [--color] file1 file2" % sys.argv[0]
sys.exit (1) sys.exit (1)
files = (FileHelpers.open_file_or_stdin (f) for f in sys.argv[1:3]) ZipDiffer.diff_files (FileHelpers.open_file_or_stdin (f) for f in sys.argv[1:])
FancyDiffer.diff_files (*files, colors=colors)

View File

@ -3,6 +3,9 @@
import sys, os, re, difflib, unicodedata, errno import sys, os, re, difflib, unicodedata, errno
from itertools import * from itertools import *
diff_symbols = "-+=*&^%$#@!~/"
diff_colors = ['red', 'green', 'blue']
class Colors: class Colors:
class Null: class Null:
red = '' red = ''
@ -89,20 +92,20 @@ class FancyDiffer:
return [' ', oo[0], '\n'] return [' ', oo[0], '\n']
return ['-', oo[0], '\n', '+', oo[1], '\n'] return ['-', oo[0], '\n', '+', oo[1], '\n']
class ZipDiffer:
@staticmethod @staticmethod
def diff_files (f1, f2, colors=Colors.Null): def diff_files (files, symbols=diff_symbols):
files = tuple (files) # in case it's a generator, copy it
try: try:
for (l1,l2) in izip (f1, f2): for lines in izip_longest (*files):
if l1 == l2: if all (lines[0] == line for line in lines[1:]):
sys.stdout.writelines ([" ", l1]) sys.stdout.writelines ([" ", lines[0]])
continue continue
sys.stdout.writelines (FancyDiffer.diff_lines (l1, l2, colors)) for i, l in enumerate (lines):
# print out residues if l:
for l in f1: sys.stdout.writelines ([symbols[i], l])
sys.stdout.writelines (["-", colors.red, l, colors.end])
for l in f2:
sys.stdout.writelines (["-", colors.green, l, colors.end])
except IOError as e: except IOError as e:
if e.errno != errno.EPIPE: if e.errno != errno.EPIPE:
print >> sys.stderr, "%s: %s: %s" % (sys.argv[0], e.filename, e.strerror) print >> sys.stderr, "%s: %s: %s" % (sys.argv[0], e.filename, e.strerror)
@ -112,9 +115,9 @@ class FancyDiffer:
class DiffFilters: class DiffFilters:
@staticmethod @staticmethod
def filter_failures (f): def filter_failures (f, symbols=diff_symbols):
for l in f: for l in f:
if l[0] in '-+': if l[0] in symbols:
# TODO retain all lines of the failure # TODO retain all lines of the failure
yield l yield l
@ -146,12 +149,13 @@ class UtilMains:
@staticmethod @staticmethod
def process_multiple_files (callback, mnemonic = "FILE"): def process_multiple_files (callback, mnemonic = "FILE"):
if len (sys.argv) == 1: if "--help" in sys.argv:
print "Usage: %s %s..." % (sys.argv[0], mnemonic) print "Usage: %s %s..." % (sys.argv[0], mnemonic)
sys.exit (1) sys.exit (1)
try: try:
for s in sys.argv[1:]: files = sys.argv[1:] if len (sys.argv) > 1 else ['-']
for s in files:
callback (FileHelpers.open_file_or_stdin (s)) callback (FileHelpers.open_file_or_stdin (s))
except IOError as e: except IOError as e:
if e.errno != errno.EPIPE: if e.errno != errno.EPIPE: