| 1 | n/a | #!/usr/bin/env python3 |
|---|
| 2 | n/a | """ Command line interface to difflib.py providing diffs in four formats: |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | * ndiff: lists every line and highlights interline changes. |
|---|
| 5 | n/a | * context: highlights clusters of changes in a before/after format. |
|---|
| 6 | n/a | * unified: highlights clusters of changes in an inline format. |
|---|
| 7 | n/a | * html: generates side by side comparison with change highlights. |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | """ |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | import sys, os, difflib, argparse |
|---|
| 12 | n/a | from datetime import datetime, timezone |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | def file_mtime(path): |
|---|
| 15 | n/a | t = datetime.fromtimestamp(os.stat(path).st_mtime, |
|---|
| 16 | n/a | timezone.utc) |
|---|
| 17 | n/a | return t.astimezone().isoformat() |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def main(): |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | parser = argparse.ArgumentParser() |
|---|
| 22 | n/a | parser.add_argument('-c', action='store_true', default=False, |
|---|
| 23 | n/a | help='Produce a context format diff (default)') |
|---|
| 24 | n/a | parser.add_argument('-u', action='store_true', default=False, |
|---|
| 25 | n/a | help='Produce a unified format diff') |
|---|
| 26 | n/a | parser.add_argument('-m', action='store_true', default=False, |
|---|
| 27 | n/a | help='Produce HTML side by side diff ' |
|---|
| 28 | n/a | '(can use -c and -l in conjunction)') |
|---|
| 29 | n/a | parser.add_argument('-n', action='store_true', default=False, |
|---|
| 30 | n/a | help='Produce a ndiff format diff') |
|---|
| 31 | n/a | parser.add_argument('-l', '--lines', type=int, default=3, |
|---|
| 32 | n/a | help='Set number of context lines (default 3)') |
|---|
| 33 | n/a | parser.add_argument('fromfile') |
|---|
| 34 | n/a | parser.add_argument('tofile') |
|---|
| 35 | n/a | options = parser.parse_args() |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | n = options.lines |
|---|
| 38 | n/a | fromfile = options.fromfile |
|---|
| 39 | n/a | tofile = options.tofile |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | fromdate = file_mtime(fromfile) |
|---|
| 42 | n/a | todate = file_mtime(tofile) |
|---|
| 43 | n/a | with open(fromfile) as ff: |
|---|
| 44 | n/a | fromlines = ff.readlines() |
|---|
| 45 | n/a | with open(tofile) as tf: |
|---|
| 46 | n/a | tolines = tf.readlines() |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | if options.u: |
|---|
| 49 | n/a | diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
|---|
| 50 | n/a | elif options.n: |
|---|
| 51 | n/a | diff = difflib.ndiff(fromlines, tolines) |
|---|
| 52 | n/a | elif options.m: |
|---|
| 53 | n/a | diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n) |
|---|
| 54 | n/a | else: |
|---|
| 55 | n/a | diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | sys.stdout.writelines(diff) |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | if __name__ == '__main__': |
|---|
| 60 | n/a | main() |
|---|