]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """ Command line interface to difflib.py providing diffs in four formats:\r |
2 | \r | |
3 | * ndiff: lists every line and highlights interline changes.\r | |
4 | * context: highlights clusters of changes in a before/after format.\r | |
5 | * unified: highlights clusters of changes in an inline format.\r | |
6 | * html: generates side by side comparison with change highlights.\r | |
7 | \r | |
8 | """\r | |
9 | \r | |
10 | import sys, os, time, difflib, optparse\r | |
11 | \r | |
12 | def main():\r | |
13 | \r | |
14 | usage = "usage: %prog [options] fromfile tofile"\r | |
15 | parser = optparse.OptionParser(usage)\r | |
16 | parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')\r | |
17 | parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')\r | |
18 | parser.add_option("-m", action="store_true", default=False, help='Produce HTML side by side diff (can use -c and -l in conjunction)')\r | |
19 | parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')\r | |
20 | parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')\r | |
21 | (options, args) = parser.parse_args()\r | |
22 | \r | |
23 | if len(args) == 0:\r | |
24 | parser.print_help()\r | |
25 | sys.exit(1)\r | |
26 | if len(args) != 2:\r | |
27 | parser.error("need to specify both a fromfile and tofile")\r | |
28 | \r | |
29 | n = options.lines\r | |
30 | fromfile, tofile = args\r | |
31 | \r | |
32 | fromdate = time.ctime(os.stat(fromfile).st_mtime)\r | |
33 | todate = time.ctime(os.stat(tofile).st_mtime)\r | |
34 | fromlines = open(fromfile, 'U').readlines()\r | |
35 | tolines = open(tofile, 'U').readlines()\r | |
36 | \r | |
37 | if options.u:\r | |
38 | diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)\r | |
39 | elif options.n:\r | |
40 | diff = difflib.ndiff(fromlines, tolines)\r | |
41 | elif options.m:\r | |
42 | diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)\r | |
43 | else:\r | |
44 | diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)\r | |
45 | \r | |
46 | sys.stdout.writelines(diff)\r | |
47 | \r | |
48 | if __name__ == '__main__':\r | |
49 | main()\r |