#!/usr/bin/env python import getopt import sys def usage(): print '''Print side-by-side diff in HTML output. Usage: pdiff.py [-F] [-C lines] [-W columnwidth] file1 file2 -C: the number of context lines shown (default is 5) -F: show the full files -W: the number of maximum characters in column''' sys.exit(-1) def main(): context = True numlines = 5 wrapcolumn = 40 # Handle command-line arguments try: optlist, args = getopt.getopt(sys.argv[1:], 'C:FW:') except getopt.GetoptError, strerror: print 'Error:', strerror usage() for (key,value) in optlist: if key == '-F': context = False numlines = 0 elif key == '-C': numlines = int(value) elif key == '-W': wrapcolumn = int(value) if len(args) != 2: print 'Error: specify two files to compare' usage() filename1, filename2 = args # Main process try: import difflib htmlDiff = difflib.HtmlDiff(wrapcolumn=wrapcolumn) except (ImportError, AttributeError): print 'Error: cannot load difflib.HtmlDiff. You need Python 2.4 or later.' sys.exit(-1) try: fromlines = open(filename1).readlines() tolines = open(filename2).readlines() except IOError, strerror: print strerror sys.exit(2) diff_html = htmlDiff.make_file(fromlines=fromlines, tolines=tolines, fromdesc=filename1, todesc=filename2, context=context, numlines=numlines) print diff_html if __name__ == "__main__": main()