diff.py |
| c:\python23\tools\scripts\diff.py |
---|
00001 | """ Command line interface to difflib.py providing diffs in four formats:
| n
| 00001 | """ Command line interface to difflib.py providing diffs in three formats:
|
00002 |
|
| 00002 |
|
00003 | * ndiff: lists every line and highlights interline changes.
|
| 00003 | * ndiff: lists every line and highlights interline changes.
|
00004 | * context: highlights clusters of changes in a before/after format
|
| 00004 | * context: highlights clusters of changes in a before/after format
|
00005 | * unified: highlights clusters of changes in an inline format.
|
| 00005 | * unified: highlights clusters of changes in an inline format.
|
00006 | * html: generates side by side differences with user configurable markup.
| n
| 00006 |
|
00007 | """
|
| 00007 | """
|
00008 |
|
| 00008 |
|
00009 | import sys, os, time, difflib, optparse
|
| 00009 | import sys, os, time, difflib, optparse
|
00010 |
|
| 00010 |
|
00011 | usage = "usage: %prog [options] fromfile tofile"
|
| 00011 | usage = "usage: %prog [options] fromfile tofile"
|
00012 | parser = optparse.OptionParser(usage)
|
| 00012 | parser = optparse.OptionParser(usage)
|
00013 | parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
|
| 00013 | parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
|
00014 | parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
|
| 00014 | parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
|
00015 | parser.add_option("-m", action="store_true", default=False, help='Produce HTML side by side diff')
| n
| |
|
00016 | parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
|
| 00015 | parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
|
00017 | parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
|
| 00016 | parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
|
00018 | (options, args) = parser.parse_args()
|
| 00017 | (options, args) = parser.parse_args()
|
00019 |
|
| 00018 |
|
00020 | if len(args) == 0:
|
| 00019 | if len(args) == 0:
|
00021 | parser.print_help()
|
| 00020 | parser.print_help()
|
00022 | sys.exit(1)
|
| 00021 | sys.exit(1)
|
00023 | if len(args) != 2:
|
| 00022 | if len(args) != 2:
|
00024 | parser.error("need to specify both a fromfile and tofile")
|
| 00023 | parser.error("need to specify both a fromfile and tofile")
|
00025 |
|
| 00024 |
|
00026 | n = options.lines
|
| 00025 | n = options.lines
|
00027 | fromfile, tofile = args
|
| 00026 | fromfile, tofile = args
|
00028 |
|
| 00027 |
|
00029 | fromdate = time.ctime(os.stat(fromfile).st_mtime)
|
| 00028 | fromdate = time.ctime(os.stat(fromfile).st_mtime)
|
00030 | todate = time.ctime(os.stat(tofile).st_mtime)
|
| 00029 | todate = time.ctime(os.stat(tofile).st_mtime)
|
00031 | fromlines = open(fromfile).readlines()
|
| 00030 | fromlines = open(fromfile).readlines()
|
00032 | tolines = open(tofile).readlines()
|
| 00031 | tolines = open(tofile).readlines()
|
00033 |
|
| 00032 |
|
00034 | if options.u:
|
| 00033 | if options.u:
|
00035 | diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
|
| 00034 | diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
|
00036 | elif options.n:
|
| 00035 | elif options.n:
|
00037 | diff = difflib.ndiff(fromlines, tolines)
|
| 00036 | diff = difflib.ndiff(fromlines, tolines)
|
00038 | elif options.m:
| t
| |
|
00039 | diff = difflib.HtmlDiff(fromfile,tofile).make_file(fromlines,tolines,options.c,n)
|
| |
|
00040 | else:
|
| 00037 | else:
|
00041 | diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
|
| 00038 | diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
|
00042 |
|
| 00039 |
|
00043 | sys.stdout.writelines(diff)
|
| 00040 | sys.stdout.writelines(diff)
|
00044 |
|
| 00041 |
|