import difflib import getopt import sys def usage(): print """ %s is the simple file comparer. python %s [-h|--help] [-w] --out=outfile infile1 infile2 It takes 2 files then it creates nice HTML overview. Well, it's only wrapper over Python's difflib with nicer layout design. I wish kompare has this feature. Optional parameter -w switches whitespace checking ON. Anyway: License? Who cares... Warranties? Are you kidding? Beer time! contact: Petr Vanek """ % (sys.argv[0], sys.argv[0]) sys.exit(1) try: opts, args = getopt.getopt(sys.argv[1:], "hw", ["help", "out="]) except getopt.GetoptError: usage() ws = False for o, a in opts: if o in ("-h", "--help"): usage() if o in ('-w'): ws = True if o in ("--out"): outfile = a #"/home/pvanek/pydiff.py/diff.html" try: fname1 = args[0] #"bp_acc2.txt" fname2 = args[1] #"bp_uat2.txt" except: usage() print "Diff 2 HTML: %s vs. %s." % (fname1, fname2) print "Output saved in: %s" % outfile f1 = file(fname1, "r") f2 = file(fname2, "r") content1 = f1.readlines() content2 = f2.readlines() f1.close() f2.close() if ws: wsfunc = None print 'Whitespace checking ON' else: wsfunc = difflib.IS_CHARACTER_JUNK print 'Whitespace checking OFF' html = difflib.HtmlDiff(tabsize=4, charjunk=wsfunc) tab = html.make_table(fromlines=content1, tolines=content2, context=True, numlines=0) tab = tab.replace('', '%s%s' % (fname1, fname2)) output = """

Diff 2 HTML: %s vs. %s

%s """ f = file(outfile, "w") f.write(output % (fname1, fname2, tab)) f.close() try: print 'Opening the %s in web browser' % outfile import webbrowser webbrowser.open(outfile) print 'OK' except: print 'Failed'