Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/env python3
      2 """ Command line interface to difflib.py providing diffs in four formats:
      3 
      4 * ndiff:    lists every line and highlights interline changes.
      5 * context:  highlights clusters of changes in a before/after format.
      6 * unified:  highlights clusters of changes in an inline format.
      7 * html:     generates side by side comparison with change highlights.
      8 
      9 """
     10 
     11 import sys, os, difflib, argparse
     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()
     18 
     19 def main():
     20 
     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()
     36 
     37     n = options.lines
     38     fromfile = options.fromfile
     39     tofile = options.tofile
     40 
     41     fromdate = file_mtime(fromfile)
     42     todate = file_mtime(tofile)
     43     with open(fromfile) as ff:
     44         fromlines = ff.readlines()
     45     with open(tofile) as tf:
     46         tolines = tf.readlines()
     47 
     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)
     52     elif options.m:
     53         diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
     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()
     61