|
8 | 8 |
|
9 | 9 | import sys, os, time, difflib, optparse |
10 | 10 |
|
11 | | -usage = "usage: %prog [options] fromfile tofile" |
12 | | -parser = optparse.OptionParser(usage) |
13 | | -parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)') |
14 | | -parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff') |
15 | | -parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff') |
16 | | -parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)') |
17 | | -(options, args) = parser.parse_args() |
18 | | - |
19 | | -if len(args) == 0: |
20 | | - parser.print_help() |
21 | | - sys.exit(1) |
22 | | -if len(args) != 2: |
23 | | - parser.error("need to specify both a fromfile and tofile") |
24 | | - |
25 | | -n = options.lines |
26 | | -fromfile, tofile = args |
27 | | - |
28 | | -fromdate = time.ctime(os.stat(fromfile).st_mtime) |
29 | | -todate = time.ctime(os.stat(tofile).st_mtime) |
30 | | -fromlines = open(fromfile).readlines() |
31 | | -tolines = open(tofile).readlines() |
32 | | - |
33 | | -if options.u: |
34 | | - diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
35 | | -elif options.n: |
36 | | - diff = difflib.ndiff(fromlines, tolines) |
37 | | -else: |
38 | | - diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
39 | | - |
40 | | -sys.stdout.writelines(diff) |
| 11 | +def main(): |
| 12 | + |
| 13 | + usage = "usage: %prog [options] fromfile tofile" |
| 14 | + parser = optparse.OptionParser(usage) |
| 15 | + parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)') |
| 16 | + parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff') |
| 17 | + parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff') |
| 18 | + parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)') |
| 19 | + (options, args) = parser.parse_args() |
| 20 | + |
| 21 | + if len(args) == 0: |
| 22 | + parser.print_help() |
| 23 | + sys.exit(1) |
| 24 | + if len(args) != 2: |
| 25 | + parser.error("need to specify both a fromfile and tofile") |
| 26 | + |
| 27 | + n = options.lines |
| 28 | + fromfile, tofile = args |
| 29 | + |
| 30 | + fromdate = time.ctime(os.stat(fromfile).st_mtime) |
| 31 | + todate = time.ctime(os.stat(tofile).st_mtime) |
| 32 | + fromlines = open(fromfile).readlines() |
| 33 | + tolines = open(tofile).readlines() |
| 34 | + |
| 35 | + if options.u: |
| 36 | + diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
| 37 | + elif options.n: |
| 38 | + diff = difflib.ndiff(fromlines, tolines) |
| 39 | + else: |
| 40 | + diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) |
| 41 | + |
| 42 | + sys.stdout.writelines(diff) |
| 43 | + |
| 44 | +if __name__ == '__main__': |
| 45 | + main() |
0 commit comments