blob: a5468f6732efd0880624a742c0584da5160409af [file] [log] [blame]
Guido van Rossum83b85181998-05-06 17:43:30 +00001#! /usr/bin/env python
2
Tim Peters5e824c32001-08-12 22:25:01 +00003# Module ndiff version 1.7.0
Tim Peters2f1aeb92000-12-09 05:03:22 +00004# Released to the public domain 08-Dec-2000,
5# by Tim Peters (tim.one@home.com).
Guido van Rossum83b85181998-05-06 17:43:30 +00006
Guido van Rossuma3433e81999-03-27 13:34:01 +00007# Provided as-is; use at your own risk; no warranty; no promises; enjoy!
Guido van Rossum83b85181998-05-06 17:43:30 +00008
Tim Peters5e824c32001-08-12 22:25:01 +00009# ndiff.py is now simply a front-end to the difflib.ndiff() function.
10# Originally, it contained the difflib.SequenceMatcher class as well.
11# This completes the raiding of reusable code from this formerly
12# self-contained script.
13
Guido van Rossuma3433e81999-03-27 13:34:01 +000014"""ndiff [-q] file1 file2
Guido van Rossum02ef28b1999-03-28 17:55:32 +000015 or
16ndiff (-r1 | -r2) < ndiff_output > file1_or_file2
Guido van Rossuma3433e81999-03-27 13:34:01 +000017
18Print a human-friendly file difference report to stdout. Both inter-
Guido van Rossum02ef28b1999-03-28 17:55:32 +000019and intra-line differences are noted. In the second form, recreate file1
20(-r1) or file2 (-r2) on stdout, from an ndiff report on stdin.
Guido van Rossuma3433e81999-03-27 13:34:01 +000021
Guido van Rossum02ef28b1999-03-28 17:55:32 +000022In the first form, if -q ("quiet") is not specified, the first two lines
23of output are
Guido van Rossuma3433e81999-03-27 13:34:01 +000024
25-: file1
26+: file2
27
28Each remaining line begins with a two-letter code:
29
30 "- " line unique to file1
31 "+ " line unique to file2
32 " " line common to both files
33 "? " line not present in either input file
34
35Lines beginning with "? " attempt to guide the eye to intraline
Tim Peters0d430e22000-11-01 02:51:27 +000036differences, and were not present in either input file. These lines can be
37confusing if the source files contain tab characters.
Guido van Rossuma3433e81999-03-27 13:34:01 +000038
39The first file can be recovered by retaining only lines that begin with
Guido van Rossum02ef28b1999-03-28 17:55:32 +000040" " or "- ", and deleting those 2-character prefixes; use ndiff with -r1.
Guido van Rossuma3433e81999-03-27 13:34:01 +000041
Tim Peters0d430e22000-11-01 02:51:27 +000042The second file can be recovered similarly, but by retaining only " " and
43"+ " lines; use ndiff with -r2; or, on Unix, the second file can be
Guido van Rossum02ef28b1999-03-28 17:55:32 +000044recovered by piping the output through
45
Guido van Rossuma3433e81999-03-27 13:34:01 +000046 sed -n '/^[+ ] /s/^..//p'
Guido van Rossuma3433e81999-03-27 13:34:01 +000047"""
48
Tim Peters5e824c32001-08-12 22:25:01 +000049__version__ = 1, 7, 0
Guido van Rossum83b85181998-05-06 17:43:30 +000050
Tim Peters5e824c32001-08-12 22:25:01 +000051import difflib, sys
Tim Peters0d430e22000-11-01 02:51:27 +000052
Guido van Rossum02ef28b1999-03-28 17:55:32 +000053def fail(msg):
Guido van Rossum02ef28b1999-03-28 17:55:32 +000054 out = sys.stderr.write
55 out(msg + "\n\n")
56 out(__doc__)
57 return 0
58
Guido van Rossum83b85181998-05-06 17:43:30 +000059# open a file & return the file object; gripe and return 0 if it
60# couldn't be opened
61def fopen(fname):
62 try:
63 return open(fname, 'r')
64 except IOError, detail:
Guido van Rossum02ef28b1999-03-28 17:55:32 +000065 return fail("couldn't open " + fname + ": " + str(detail))
Guido van Rossum83b85181998-05-06 17:43:30 +000066
67# open two files & spray the diff to stdout; return false iff a problem
68def fcompare(f1name, f2name):
69 f1 = fopen(f1name)
70 f2 = fopen(f2name)
71 if not f1 or not f2:
72 return 0
73
74 a = f1.readlines(); f1.close()
75 b = f2.readlines(); f2.close()
76
Tim Peters5e824c32001-08-12 22:25:01 +000077 diff = difflib.ndiff(a, b)
78 sys.stdout.writelines(diff)
Guido van Rossum83b85181998-05-06 17:43:30 +000079
80 return 1
81
Guido van Rossuma3433e81999-03-27 13:34:01 +000082# crack args (sys.argv[1:] is normal) & compare;
83# return false iff a problem
84
85def main(args):
86 import getopt
87 try:
Guido van Rossum02ef28b1999-03-28 17:55:32 +000088 opts, args = getopt.getopt(args, "qr:")
Guido van Rossuma3433e81999-03-27 13:34:01 +000089 except getopt.error, detail:
Guido van Rossum02ef28b1999-03-28 17:55:32 +000090 return fail(str(detail))
Guido van Rossuma3433e81999-03-27 13:34:01 +000091 noisy = 1
Guido van Rossum02ef28b1999-03-28 17:55:32 +000092 qseen = rseen = 0
Guido van Rossuma3433e81999-03-27 13:34:01 +000093 for opt, val in opts:
94 if opt == "-q":
Guido van Rossum02ef28b1999-03-28 17:55:32 +000095 qseen = 1
Guido van Rossuma3433e81999-03-27 13:34:01 +000096 noisy = 0
Guido van Rossum02ef28b1999-03-28 17:55:32 +000097 elif opt == "-r":
98 rseen = 1
99 whichfile = val
100 if qseen and rseen:
101 return fail("can't specify both -q and -r")
102 if rseen:
103 if args:
104 return fail("no args allowed with -r option")
105 if whichfile in "12":
106 restore(whichfile)
107 return 1
108 return fail("-r value must be 1 or 2")
Guido van Rossuma3433e81999-03-27 13:34:01 +0000109 if len(args) != 2:
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000110 return fail("need 2 filename args")
Guido van Rossuma3433e81999-03-27 13:34:01 +0000111 f1name, f2name = args
112 if noisy:
113 print '-:', f1name
114 print '+:', f2name
Guido van Rossum83b85181998-05-06 17:43:30 +0000115 return fcompare(f1name, f2name)
116
Tim Peters5e824c32001-08-12 22:25:01 +0000117# read ndiff output from stdin, and print file1 (which=='1') or
118# file2 (which=='2') to stdout
119
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000120def restore(which):
Tim Peters5e824c32001-08-12 22:25:01 +0000121 restored = difflib.restore(sys.stdin.readlines(), which)
122 sys.stdout.writelines(restored)
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000123
Guido van Rossum83b85181998-05-06 17:43:30 +0000124if __name__ == '__main__':
Guido van Rossuma3433e81999-03-27 13:34:01 +0000125 args = sys.argv[1:]
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000126 if "-profile" in args:
Guido van Rossum83b85181998-05-06 17:43:30 +0000127 import profile, pstats
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000128 args.remove("-profile")
Guido van Rossum83b85181998-05-06 17:43:30 +0000129 statf = "ndiff.pro"
Guido van Rossuma3433e81999-03-27 13:34:01 +0000130 profile.run("main(args)", statf)
Guido van Rossum83b85181998-05-06 17:43:30 +0000131 stats = pstats.Stats(statf)
132 stats.strip_dirs().sort_stats('time').print_stats()
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000133 else:
134 main(args)