Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 1 | """ Command line interface to difflib.py providing diffs in four formats: |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 2 | |
| 3 | * ndiff: lists every line and highlights interline changes. |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 4 | * context: highlights clusters of changes in a before/after format. |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 5 | * unified: highlights clusters of changes in an inline format. |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 6 | * html: generates side by side comparison with change highlights. |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 7 | |
| 8 | """ |
| 9 | |
| 10 | import sys, os, time, difflib, optparse |
| 11 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 12 | def main(): |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 13 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 14 | usage = "usage: %prog [options] fromfile tofile" |
| 15 | parser = optparse.OptionParser(usage) |
| 16 | parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)') |
| 17 | parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff') |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 18 | parser.add_option("-m", action="store_true", default=False, help='Produce HTML side by side diff (can use -c and -l in conjunction)') |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 19 | parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff') |
| 20 | parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)') |
| 21 | (options, args) = parser.parse_args() |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 22 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 23 | if len(args) == 0: |
| 24 | parser.print_help() |
| 25 | sys.exit(1) |
| 26 | if len(args) != 2: |
| 27 | parser.error("need to specify both a fromfile and tofile") |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 28 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 29 | n = options.lines |
| 30 | fromfile, tofile = args |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 31 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 32 | fromdate = time.ctime(os.stat(fromfile).st_mtime) |
| 33 | todate = time.ctime(os.stat(tofile).st_mtime) |
Tim Peters | 130e37f | 2004-10-03 19:03:19 +0000 | [diff] [blame] | 34 | fromlines = open(fromfile, 'U').readlines() |
| 35 | tolines = open(tofile, 'U').readlines() |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 36 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 37 | if options.u: |
| 38 | diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
| 39 | elif options.n: |
| 40 | diff = difflib.ndiff(fromlines, tolines) |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 41 | elif options.m: |
| 42 | diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n) |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 43 | else: |
| 44 | diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
| 45 | |
| 46 | sys.stdout.writelines(diff) |
| 47 | |
| 48 | if __name__ == '__main__': |
| 49 | main() |