blob: 9720a431557aec647ed1e8aeb9c43ad77eeb98f5 [file] [log] [blame]
Éric Araujoa0e92a82011-07-26 18:01:08 +02001#!/usr/bin/env python3
Martin v. Löwise064b412004-08-29 16:34:40 +00002""" Command line interface to difflib.py providing diffs in four formats:
Raymond Hettingera33d1772003-06-08 23:04:17 +00003
4* ndiff: lists every line and highlights interline changes.
Martin v. Löwise064b412004-08-29 16:34:40 +00005* context: highlights clusters of changes in a before/after format.
Raymond Hettingera33d1772003-06-08 23:04:17 +00006* unified: highlights clusters of changes in an inline format.
Martin v. Löwise064b412004-08-29 16:34:40 +00007* html: generates side by side comparison with change highlights.
Raymond Hettingera33d1772003-06-08 23:04:17 +00008
9"""
10
Serhiy Storchaka99233412014-07-15 13:23:58 +030011import sys, os, time, difflib, argparse
Alexander Belopolskya2637722012-06-22 12:46:19 -040012from datetime import datetime, timezone
13
14def file_mtime(path):
15 t = datetime.fromtimestamp(os.stat(path).st_mtime,
16 timezone.utc)
17 return t.astimezone().isoformat()
Raymond Hettingera33d1772003-06-08 23:04:17 +000018
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000019def main():
Raymond Hettingera33d1772003-06-08 23:04:17 +000020
Serhiy Storchaka99233412014-07-15 13:23:58 +030021 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 Hettingera33d1772003-06-08 23:04:17 +000036
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000037 n = options.lines
Serhiy Storchaka99233412014-07-15 13:23:58 +030038 fromfile = options.fromfile
39 tofile = options.tofile
Raymond Hettingera33d1772003-06-08 23:04:17 +000040
Alexander Belopolskya2637722012-06-22 12:46:19 -040041 fromdate = file_mtime(fromfile)
42 todate = file_mtime(tofile)
Serhiy Storchaka6787a382013-11-23 22:12:06 +020043 with open(fromfile) as ff:
Alexander Belopolskya2637722012-06-22 12:46:19 -040044 fromlines = ff.readlines()
Serhiy Storchaka6787a382013-11-23 22:12:06 +020045 with open(tofile) as tf:
Alexander Belopolskya2637722012-06-22 12:46:19 -040046 tolines = tf.readlines()
Raymond Hettingera33d1772003-06-08 23:04:17 +000047
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000048 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öwise064b412004-08-29 16:34:40 +000052 elif options.m:
53 diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000054 else:
55 diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
56
57 sys.stdout.writelines(diff)
58
59if __name__ == '__main__':
60 main()