Éric Araujo | a0e92a8 | 2011-07-26 18:01:08 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 2 | """ Command line interface to difflib.py providing diffs in four formats: |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 3 | |
| 4 | * ndiff: lists every line and highlights interline changes. |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 5 | * context: highlights clusters of changes in a before/after format. |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 6 | * unified: highlights clusters of changes in an inline format. |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 7 | * html: generates side by side comparison with change highlights. |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 8 | |
| 9 | """ |
| 10 | |
| 11 | import sys, os, time, difflib, optparse |
Alexander Belopolsky | a263772 | 2012-06-22 12:46:19 -0400 | [diff] [blame^] | 12 | from datetime import datetime, timezone |
| 13 | |
| 14 | def file_mtime(path): |
| 15 | t = datetime.fromtimestamp(os.stat(path).st_mtime, |
| 16 | timezone.utc) |
| 17 | return t.astimezone().isoformat() |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 18 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 19 | def main(): |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 20 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 21 | usage = "usage: %prog [options] fromfile tofile" |
| 22 | parser = optparse.OptionParser(usage) |
| 23 | parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)') |
| 24 | 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] | 25 | 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] | 26 | parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff') |
| 27 | parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)') |
| 28 | (options, args) = parser.parse_args() |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 29 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 30 | if len(args) == 0: |
| 31 | parser.print_help() |
| 32 | sys.exit(1) |
| 33 | if len(args) != 2: |
| 34 | parser.error("need to specify both a fromfile and tofile") |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 35 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 36 | n = options.lines |
| 37 | fromfile, tofile = args |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 38 | |
Alexander Belopolsky | a263772 | 2012-06-22 12:46:19 -0400 | [diff] [blame^] | 39 | fromdate = file_mtime(fromfile) |
| 40 | todate = file_mtime(tofile) |
| 41 | with open(fromfile, 'U') as ff: |
| 42 | fromlines = ff.readlines() |
| 43 | with open(tofile, 'U') as tf: |
| 44 | tolines = tf.readlines() |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 45 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 46 | if options.u: |
| 47 | diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
| 48 | elif options.n: |
| 49 | diff = difflib.ndiff(fromlines, tolines) |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 50 | elif options.m: |
| 51 | 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] | 52 | else: |
| 53 | diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
| 54 | |
| 55 | sys.stdout.writelines(diff) |
| 56 | |
| 57 | if __name__ == '__main__': |
| 58 | main() |