diff.py
c:\python23\tools\scripts\diff.py
00001""" Command line interface to difflib.py providing diffs in four formats: n 00001""" Command line interface to difflib.py providing diffs in three formats:
00002 00002
00003* ndiff:    lists every line and highlights interline changes. 00003* ndiff:    lists every line and highlights interline changes.
00004* context:  highlights clusters of changes in a before/after format 00004* context:  highlights clusters of changes in a before/after format
00005* unified:  highlights clusters of changes in an inline format. 00005* unified:  highlights clusters of changes in an inline format.
00006* html:     generates side by side differences with user configurable markup. n 00006 
00007""" 00007"""
00008 00008
00009import sys, os, time, difflib, optparse 00009import sys, os, time, difflib, optparse
00010 00010
00011usage = "usage: %prog [options] fromfile tofile" 00011usage = "usage: %prog [options] fromfile tofile"
00012parser = optparse.OptionParser(usage) 00012parser = optparse.OptionParser(usage)
00013parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)') 00013parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
00014parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff') 00014parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
00015parser.add_option("-m", action="store_true", default=False, help='Produce HTML side by side diff') n
00016parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff') 00015parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
00017parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)') 00016parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
00018(options, args) = parser.parse_args() 00017(options, args) = parser.parse_args()
00019 00018
00020if len(args) == 0: 00019if len(args) == 0:
00021    parser.print_help() 00020    parser.print_help()
00022    sys.exit(1) 00021    sys.exit(1)
00023if len(args) != 2: 00022if len(args) != 2:
00024    parser.error("need to specify both a fromfile and tofile") 00023    parser.error("need to specify both a fromfile and tofile")
00025 00024
00026n = options.lines 00025n = options.lines
00027fromfile, tofile = args 00026fromfile, tofile = args
00028 00027
00029fromdate = time.ctime(os.stat(fromfile).st_mtime) 00028fromdate = time.ctime(os.stat(fromfile).st_mtime)
00030todate = time.ctime(os.stat(tofile).st_mtime) 00029todate = time.ctime(os.stat(tofile).st_mtime)
00031fromlines = open(fromfile).readlines() 00030fromlines = open(fromfile).readlines()
00032tolines = open(tofile).readlines() 00031tolines = open(tofile).readlines()
00033 00032
00034if options.u: 00033if options.u:
00035    diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) 00034    diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
00036elif options.n: 00035elif options.n:
00037    diff = difflib.ndiff(fromlines, tolines) 00036    diff = difflib.ndiff(fromlines, tolines)
00038elif options.m: t
00039    diff = difflib.HtmlDiff(fromfile,tofile).make_file(fromlines,tolines,options.c,n)
00040else: 00037else:
00041    diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) 00038    diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
00042 00039
00043sys.stdout.writelines(diff) 00040sys.stdout.writelines(diff)
00044 00041

Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)able top