blob: c60c8a83ece1342a89a9cfda68cfd670f159a717 [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:
Tim Peters130e37f2004-10-03 19:03:19 +000063 return open(fname, 'U')
Guido van Rossumb940e112007-01-10 16:19:56 +000064 except IOError as 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()
Tim Peters8a9c2842001-09-22 21:30:22 +000076 for line in difflib.ndiff(a, b):
Collin Winter6afaeb72007-08-03 17:06:41 +000077 print(line, end=' ')
Guido van Rossum83b85181998-05-06 17:43:30 +000078
79 return 1
80
Guido van Rossuma3433e81999-03-27 13:34:01 +000081# crack args (sys.argv[1:] is normal) & compare;
82# return false iff a problem
83
84def main(args):
85 import getopt
86 try:
Guido van Rossum02ef28b1999-03-28 17:55:32 +000087 opts, args = getopt.getopt(args, "qr:")
Guido van Rossumb940e112007-01-10 16:19:56 +000088 except getopt.error as detail:
Guido van Rossum02ef28b1999-03-28 17:55:32 +000089 return fail(str(detail))
Guido van Rossuma3433e81999-03-27 13:34:01 +000090 noisy = 1
Guido van Rossum02ef28b1999-03-28 17:55:32 +000091 qseen = rseen = 0
Guido van Rossuma3433e81999-03-27 13:34:01 +000092 for opt, val in opts:
93 if opt == "-q":
Guido van Rossum02ef28b1999-03-28 17:55:32 +000094 qseen = 1
Guido van Rossuma3433e81999-03-27 13:34:01 +000095 noisy = 0
Guido van Rossum02ef28b1999-03-28 17:55:32 +000096 elif opt == "-r":
97 rseen = 1
98 whichfile = val
99 if qseen and rseen:
100 return fail("can't specify both -q and -r")
101 if rseen:
102 if args:
103 return fail("no args allowed with -r option")
Andrew M. Kuchlinga2f77282003-05-13 17:56:07 +0000104 if whichfile in ("1", "2"):
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000105 restore(whichfile)
106 return 1
107 return fail("-r value must be 1 or 2")
Guido van Rossuma3433e81999-03-27 13:34:01 +0000108 if len(args) != 2:
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000109 return fail("need 2 filename args")
Guido van Rossuma3433e81999-03-27 13:34:01 +0000110 f1name, f2name = args
111 if noisy:
Collin Winter6afaeb72007-08-03 17:06:41 +0000112 print('-:', f1name)
113 print('+:', f2name)
Guido van Rossum83b85181998-05-06 17:43:30 +0000114 return fcompare(f1name, f2name)
115
Tim Peters5e824c32001-08-12 22:25:01 +0000116# read ndiff output from stdin, and print file1 (which=='1') or
117# file2 (which=='2') to stdout
118
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000119def restore(which):
Tim Peters5e824c32001-08-12 22:25:01 +0000120 restored = difflib.restore(sys.stdin.readlines(), which)
Tim Peters2c9aa5e2001-09-23 04:06:05 +0000121 sys.stdout.writelines(restored)
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000122
Guido van Rossum83b85181998-05-06 17:43:30 +0000123if __name__ == '__main__':
Guido van Rossuma3433e81999-03-27 13:34:01 +0000124 args = sys.argv[1:]
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000125 if "-profile" in args:
Guido van Rossum83b85181998-05-06 17:43:30 +0000126 import profile, pstats
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000127 args.remove("-profile")
Guido van Rossum83b85181998-05-06 17:43:30 +0000128 statf = "ndiff.pro"
Guido van Rossuma3433e81999-03-27 13:34:01 +0000129 profile.run("main(args)", statf)
Guido van Rossum83b85181998-05-06 17:43:30 +0000130 stats = pstats.Stats(statf)
131 stats.strip_dirs().sort_stats('time').print_stats()
Guido van Rossum02ef28b1999-03-28 17:55:32 +0000132 else:
133 main(args)