1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | # Module ndiff version 1.7.0 |
---|
4 | n/a | # Released to the public domain 08-Dec-2000, |
---|
5 | n/a | # by Tim Peters (tim.one@home.com). |
---|
6 | n/a | |
---|
7 | n/a | # Provided as-is; use at your own risk; no warranty; no promises; enjoy! |
---|
8 | n/a | |
---|
9 | n/a | # ndiff.py is now simply a front-end to the difflib.ndiff() function. |
---|
10 | n/a | # Originally, it contained the difflib.SequenceMatcher class as well. |
---|
11 | n/a | # This completes the raiding of reusable code from this formerly |
---|
12 | n/a | # self-contained script. |
---|
13 | n/a | |
---|
14 | n/a | """ndiff [-q] file1 file2 |
---|
15 | n/a | or |
---|
16 | n/a | ndiff (-r1 | -r2) < ndiff_output > file1_or_file2 |
---|
17 | n/a | |
---|
18 | n/a | Print a human-friendly file difference report to stdout. Both inter- |
---|
19 | n/a | and intra-line differences are noted. In the second form, recreate file1 |
---|
20 | n/a | (-r1) or file2 (-r2) on stdout, from an ndiff report on stdin. |
---|
21 | n/a | |
---|
22 | n/a | In the first form, if -q ("quiet") is not specified, the first two lines |
---|
23 | n/a | of output are |
---|
24 | n/a | |
---|
25 | n/a | -: file1 |
---|
26 | n/a | +: file2 |
---|
27 | n/a | |
---|
28 | n/a | Each remaining line begins with a two-letter code: |
---|
29 | n/a | |
---|
30 | n/a | "- " line unique to file1 |
---|
31 | n/a | "+ " line unique to file2 |
---|
32 | n/a | " " line common to both files |
---|
33 | n/a | "? " line not present in either input file |
---|
34 | n/a | |
---|
35 | n/a | Lines beginning with "? " attempt to guide the eye to intraline |
---|
36 | n/a | differences, and were not present in either input file. These lines can be |
---|
37 | n/a | confusing if the source files contain tab characters. |
---|
38 | n/a | |
---|
39 | n/a | The first file can be recovered by retaining only lines that begin with |
---|
40 | n/a | " " or "- ", and deleting those 2-character prefixes; use ndiff with -r1. |
---|
41 | n/a | |
---|
42 | n/a | The second file can be recovered similarly, but by retaining only " " and |
---|
43 | n/a | "+ " lines; use ndiff with -r2; or, on Unix, the second file can be |
---|
44 | n/a | recovered by piping the output through |
---|
45 | n/a | |
---|
46 | n/a | sed -n '/^[+ ] /s/^..//p' |
---|
47 | n/a | """ |
---|
48 | n/a | |
---|
49 | n/a | __version__ = 1, 7, 0 |
---|
50 | n/a | |
---|
51 | n/a | import difflib, sys |
---|
52 | n/a | |
---|
53 | n/a | def fail(msg): |
---|
54 | n/a | out = sys.stderr.write |
---|
55 | n/a | out(msg + "\n\n") |
---|
56 | n/a | out(__doc__) |
---|
57 | n/a | return 0 |
---|
58 | n/a | |
---|
59 | n/a | # open a file & return the file object; gripe and return 0 if it |
---|
60 | n/a | # couldn't be opened |
---|
61 | n/a | def fopen(fname): |
---|
62 | n/a | try: |
---|
63 | n/a | return open(fname) |
---|
64 | n/a | except IOError as detail: |
---|
65 | n/a | return fail("couldn't open " + fname + ": " + str(detail)) |
---|
66 | n/a | |
---|
67 | n/a | # open two files & spray the diff to stdout; return false iff a problem |
---|
68 | n/a | def fcompare(f1name, f2name): |
---|
69 | n/a | f1 = fopen(f1name) |
---|
70 | n/a | f2 = fopen(f2name) |
---|
71 | n/a | if not f1 or not f2: |
---|
72 | n/a | return 0 |
---|
73 | n/a | |
---|
74 | n/a | a = f1.readlines(); f1.close() |
---|
75 | n/a | b = f2.readlines(); f2.close() |
---|
76 | n/a | for line in difflib.ndiff(a, b): |
---|
77 | n/a | print(line, end=' ') |
---|
78 | n/a | |
---|
79 | n/a | return 1 |
---|
80 | n/a | |
---|
81 | n/a | # crack args (sys.argv[1:] is normal) & compare; |
---|
82 | n/a | # return false iff a problem |
---|
83 | n/a | |
---|
84 | n/a | def main(args): |
---|
85 | n/a | import getopt |
---|
86 | n/a | try: |
---|
87 | n/a | opts, args = getopt.getopt(args, "qr:") |
---|
88 | n/a | except getopt.error as detail: |
---|
89 | n/a | return fail(str(detail)) |
---|
90 | n/a | noisy = 1 |
---|
91 | n/a | qseen = rseen = 0 |
---|
92 | n/a | for opt, val in opts: |
---|
93 | n/a | if opt == "-q": |
---|
94 | n/a | qseen = 1 |
---|
95 | n/a | noisy = 0 |
---|
96 | n/a | elif opt == "-r": |
---|
97 | n/a | rseen = 1 |
---|
98 | n/a | whichfile = val |
---|
99 | n/a | if qseen and rseen: |
---|
100 | n/a | return fail("can't specify both -q and -r") |
---|
101 | n/a | if rseen: |
---|
102 | n/a | if args: |
---|
103 | n/a | return fail("no args allowed with -r option") |
---|
104 | n/a | if whichfile in ("1", "2"): |
---|
105 | n/a | restore(whichfile) |
---|
106 | n/a | return 1 |
---|
107 | n/a | return fail("-r value must be 1 or 2") |
---|
108 | n/a | if len(args) != 2: |
---|
109 | n/a | return fail("need 2 filename args") |
---|
110 | n/a | f1name, f2name = args |
---|
111 | n/a | if noisy: |
---|
112 | n/a | print('-:', f1name) |
---|
113 | n/a | print('+:', f2name) |
---|
114 | n/a | return fcompare(f1name, f2name) |
---|
115 | n/a | |
---|
116 | n/a | # read ndiff output from stdin, and print file1 (which=='1') or |
---|
117 | n/a | # file2 (which=='2') to stdout |
---|
118 | n/a | |
---|
119 | n/a | def restore(which): |
---|
120 | n/a | restored = difflib.restore(sys.stdin.readlines(), which) |
---|
121 | n/a | sys.stdout.writelines(restored) |
---|
122 | n/a | |
---|
123 | n/a | if __name__ == '__main__': |
---|
124 | n/a | args = sys.argv[1:] |
---|
125 | n/a | if "-profile" in args: |
---|
126 | n/a | import profile, pstats |
---|
127 | n/a | args.remove("-profile") |
---|
128 | n/a | statf = "ndiff.pro" |
---|
129 | n/a | profile.run("main(args)", statf) |
---|
130 | n/a | stats = pstats.Stats(statf) |
---|
131 | n/a | stats.strip_dirs().sort_stats('time').print_stats() |
---|
132 | n/a | else: |
---|
133 | n/a | main(args) |
---|