blob: f9b14bf5e51e4a9b7853fc04072114b12f59f630 [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
11import sys, os, time, difflib, optparse
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
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000021 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öwise064b412004-08-29 16:34:40 +000025 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. Kuchlinge236b382004-08-09 17:27:55 +000026 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 Hettingera33d1772003-06-08 23:04:17 +000029
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000030 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 Hettingera33d1772003-06-08 23:04:17 +000035
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000036 n = options.lines
37 fromfile, tofile = args
Raymond Hettingera33d1772003-06-08 23:04:17 +000038
Alexander Belopolskya2637722012-06-22 12:46:19 -040039 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 Hettingera33d1772003-06-08 23:04:17 +000045
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000046 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öwise064b412004-08-29 16:34:40 +000050 elif options.m:
51 diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000052 else:
53 diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
54
55 sys.stdout.writelines(diff)
56
57if __name__ == '__main__':
58 main()