É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 | |
Serhiy Storchaka | 9923341 | 2014-07-15 13:23:58 +0300 | [diff] [blame] | 11 | import sys, os, time, difflib, argparse |
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 | |
Serhiy Storchaka | 9923341 | 2014-07-15 13:23:58 +0300 | [diff] [blame] | 21 | parser = argparse.ArgumentParser() |
| 22 | parser.add_argument('-c', action='store_true', default=False, |
| 23 | help='Produce a context format diff (default)') |
| 24 | parser.add_argument('-u', action='store_true', default=False, |
| 25 | help='Produce a unified format diff') |
| 26 | parser.add_argument('-m', action='store_true', default=False, |
| 27 | help='Produce HTML side by side diff ' |
| 28 | '(can use -c and -l in conjunction)') |
| 29 | parser.add_argument('-n', action='store_true', default=False, |
| 30 | help='Produce a ndiff format diff') |
| 31 | parser.add_argument('-l', '--lines', type=int, default=3, |
| 32 | help='Set number of context lines (default 3)') |
| 33 | parser.add_argument('fromfile') |
| 34 | parser.add_argument('tofile') |
| 35 | options = parser.parse_args() |
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 | n = options.lines |
Serhiy Storchaka | 9923341 | 2014-07-15 13:23:58 +0300 | [diff] [blame] | 38 | fromfile = options.fromfile |
| 39 | tofile = options.tofile |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 40 | |
Alexander Belopolsky | a263772 | 2012-06-22 12:46:19 -0400 | [diff] [blame] | 41 | fromdate = file_mtime(fromfile) |
| 42 | todate = file_mtime(tofile) |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 43 | with open(fromfile) as ff: |
Alexander Belopolsky | a263772 | 2012-06-22 12:46:19 -0400 | [diff] [blame] | 44 | fromlines = ff.readlines() |
Serhiy Storchaka | 6787a38 | 2013-11-23 22:12:06 +0200 | [diff] [blame] | 45 | with open(tofile) as tf: |
Alexander Belopolsky | a263772 | 2012-06-22 12:46:19 -0400 | [diff] [blame] | 46 | tolines = tf.readlines() |
Raymond Hettinger | a33d177 | 2003-06-08 23:04:17 +0000 | [diff] [blame] | 47 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 48 | if options.u: |
| 49 | diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
| 50 | elif options.n: |
| 51 | diff = difflib.ndiff(fromlines, tolines) |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 52 | elif options.m: |
| 53 | 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] | 54 | else: |
| 55 | diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
| 56 | |
| 57 | sys.stdout.writelines(diff) |
| 58 | |
| 59 | if __name__ == '__main__': |
| 60 | main() |