ndiff.py 3.73 KB
Newer Older
1
#! /usr/bin/env python3
2

3
# Module ndiff version 1.7.0
4 5
# Released to the public domain 08-Dec-2000,
# by Tim Peters (tim.one@home.com).
6

Guido van Rossum's avatar
Guido van Rossum committed
7
# Provided as-is; use at your own risk; no warranty; no promises; enjoy!
8

9 10 11 12 13
# ndiff.py is now simply a front-end to the difflib.ndiff() function.
# Originally, it contained the difflib.SequenceMatcher class as well.
# This completes the raiding of reusable code from this formerly
# self-contained script.

Guido van Rossum's avatar
Guido van Rossum committed
14
"""ndiff [-q] file1 file2
Guido van Rossum's avatar
Guido van Rossum committed
15 16
    or
ndiff (-r1 | -r2) < ndiff_output > file1_or_file2
Guido van Rossum's avatar
Guido van Rossum committed
17 18

Print a human-friendly file difference report to stdout.  Both inter-
Guido van Rossum's avatar
Guido van Rossum committed
19 20
and intra-line differences are noted.  In the second form, recreate file1
(-r1) or file2 (-r2) on stdout, from an ndiff report on stdin.
Guido van Rossum's avatar
Guido van Rossum committed
21

Guido van Rossum's avatar
Guido van Rossum committed
22 23
In the first form, if -q ("quiet") is not specified, the first two lines
of output are
Guido van Rossum's avatar
Guido van Rossum committed
24 25 26 27 28 29 30 31 32 33 34 35

-: file1
+: file2

Each remaining line begins with a two-letter code:

    "- "    line unique to file1
    "+ "    line unique to file2
    "  "    line common to both files
    "? "    line not present in either input file

Lines beginning with "? " attempt to guide the eye to intraline
36 37
differences, and were not present in either input file.  These lines can be
confusing if the source files contain tab characters.
Guido van Rossum's avatar
Guido van Rossum committed
38 39

The first file can be recovered by retaining only lines that begin with
Guido van Rossum's avatar
Guido van Rossum committed
40
"  " or "- ", and deleting those 2-character prefixes; use ndiff with -r1.
Guido van Rossum's avatar
Guido van Rossum committed
41

42 43
The second file can be recovered similarly, but by retaining only "  " and
"+ " lines; use ndiff with -r2; or, on Unix, the second file can be
Guido van Rossum's avatar
Guido van Rossum committed
44 45
recovered by piping the output through

Guido van Rossum's avatar
Guido van Rossum committed
46 47 48
    sed -n '/^[+ ] /s/^..//p'
"""

49
__version__ = 1, 7, 0
50

51
import difflib, sys
52

Guido van Rossum's avatar
Guido van Rossum committed
53 54 55 56 57 58
def fail(msg):
    out = sys.stderr.write
    out(msg + "\n\n")
    out(__doc__)
    return 0

59 60 61 62
# open a file & return the file object; gripe and return 0 if it
# couldn't be opened
def fopen(fname):
    try:
63
        return open(fname)
64
    except IOError as detail:
Guido van Rossum's avatar
Guido van Rossum committed
65
        return fail("couldn't open " + fname + ": " + str(detail))
66 67 68 69 70 71 72 73 74 75

# open two files & spray the diff to stdout; return false iff a problem
def fcompare(f1name, f2name):
    f1 = fopen(f1name)
    f2 = fopen(f2name)
    if not f1 or not f2:
        return 0

    a = f1.readlines(); f1.close()
    b = f2.readlines(); f2.close()
76
    for line in difflib.ndiff(a, b):
77
        print(line, end=' ')
78 79 80

    return 1

Guido van Rossum's avatar
Guido van Rossum committed
81 82 83 84 85 86
# crack args (sys.argv[1:] is normal) & compare;
# return false iff a problem

def main(args):
    import getopt
    try:
Guido van Rossum's avatar
Guido van Rossum committed
87
        opts, args = getopt.getopt(args, "qr:")
88
    except getopt.error as detail:
Guido van Rossum's avatar
Guido van Rossum committed
89
        return fail(str(detail))
Guido van Rossum's avatar
Guido van Rossum committed
90
    noisy = 1
Guido van Rossum's avatar
Guido van Rossum committed
91
    qseen = rseen = 0
Guido van Rossum's avatar
Guido van Rossum committed
92 93
    for opt, val in opts:
        if opt == "-q":
Guido van Rossum's avatar
Guido van Rossum committed
94
            qseen = 1
Guido van Rossum's avatar
Guido van Rossum committed
95
            noisy = 0
Guido van Rossum's avatar
Guido van Rossum committed
96 97 98 99 100 101 102 103
        elif opt == "-r":
            rseen = 1
            whichfile = val
    if qseen and rseen:
        return fail("can't specify both -q and -r")
    if rseen:
        if args:
            return fail("no args allowed with -r option")
104
        if whichfile in ("1", "2"):
Guido van Rossum's avatar
Guido van Rossum committed
105 106 107
            restore(whichfile)
            return 1
        return fail("-r value must be 1 or 2")
Guido van Rossum's avatar
Guido van Rossum committed
108
    if len(args) != 2:
Guido van Rossum's avatar
Guido van Rossum committed
109
        return fail("need 2 filename args")
Guido van Rossum's avatar
Guido van Rossum committed
110 111
    f1name, f2name = args
    if noisy:
112 113
        print('-:', f1name)
        print('+:', f2name)
114 115
    return fcompare(f1name, f2name)

116 117 118
# read ndiff output from stdin, and print file1 (which=='1') or
# file2 (which=='2') to stdout

Guido van Rossum's avatar
Guido van Rossum committed
119
def restore(which):
120
    restored = difflib.restore(sys.stdin.readlines(), which)
121
    sys.stdout.writelines(restored)
Guido van Rossum's avatar
Guido van Rossum committed
122

123
if __name__ == '__main__':
Guido van Rossum's avatar
Guido van Rossum committed
124
    args = sys.argv[1:]
Guido van Rossum's avatar
Guido van Rossum committed
125
    if "-profile" in args:
126
        import profile, pstats
Guido van Rossum's avatar
Guido van Rossum committed
127
        args.remove("-profile")
128
        statf = "ndiff.pro"
Guido van Rossum's avatar
Guido van Rossum committed
129
        profile.run("main(args)", statf)
130 131
        stats = pstats.Stats(statf)
        stats.strip_dirs().sort_stats('time').print_stats()
Guido van Rossum's avatar
Guido van Rossum committed
132 133
    else:
        main(args)