Update to 2.7.3
[profile/ivi/python.git] / Tools / scripts / diff.py
1 #!/usr/bin/env python
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, time, difflib, optparse
12
13 def main():
14
15     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')
19     parser.add_option("-m", action="store_true", default=False, help='Produce HTML side by side diff (can use -c and -l in conjunction)')
20     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()
23
24     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")
29
30     n = options.lines
31     fromfile, tofile = args
32
33     fromdate = time.ctime(os.stat(fromfile).st_mtime)
34     todate = time.ctime(os.stat(tofile).st_mtime)
35     fromlines = open(fromfile, 'U').readlines()
36     tolines = open(tofile, 'U').readlines()
37
38     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)
42     elif options.m:
43         diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
44     else:
45         diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
46
47     sys.stdout.writelines(diff)
48
49 if __name__ == '__main__':
50     main()