blob: 9efb078fb8f32db9eb3c8d6633a9eee498988e13 [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
12
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000013def main():
Raymond Hettingera33d1772003-06-08 23:04:17 +000014
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000015 usage = "usage: %prog [options] fromfile tofile"
16 parser = optparse.OptionParser(usage)
17 parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
18 parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
Martin v. Löwise064b412004-08-29 16:34:40 +000019 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 +000020 parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
21 parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
22 (options, args) = parser.parse_args()
Raymond Hettingera33d1772003-06-08 23:04:17 +000023
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000024 if len(args) == 0:
25 parser.print_help()
26 sys.exit(1)
27 if len(args) != 2:
28 parser.error("need to specify both a fromfile and tofile")
Raymond Hettingera33d1772003-06-08 23:04:17 +000029
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000030 n = options.lines
31 fromfile, tofile = args
Raymond Hettingera33d1772003-06-08 23:04:17 +000032
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000033 fromdate = time.ctime(os.stat(fromfile).st_mtime)
34 todate = time.ctime(os.stat(tofile).st_mtime)
Tim Peters130e37f2004-10-03 19:03:19 +000035 fromlines = open(fromfile, 'U').readlines()
36 tolines = open(tofile, 'U').readlines()
Raymond Hettingera33d1772003-06-08 23:04:17 +000037
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000038 if options.u:
39 diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
40 elif options.n:
41 diff = difflib.ndiff(fromlines, tolines)
Martin v. Löwise064b412004-08-29 16:34:40 +000042 elif options.m:
43 diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000044 else:
45 diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
46
47 sys.stdout.writelines(diff)
48
49if __name__ == '__main__':
50 main()