Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/ndiff.py @ 17322:bd110eadc19a legacy-trunk
One-liner critical fix from Jim Fulton: Eric's string-method crusade
got the order backwards in a line (for .find()).
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Thu, 05 Apr 2001 22:26:23 +0000 |
| parents | 7de5043827f7 |
| children | 50fb4683a461 |
| rev | line source |
|---|---|
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
15497
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
3 # Module ndiff version 1.6.0 |
|
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
4 # Released to the public domain 08-Dec-2000, |
|
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
5 # by Tim Peters (tim.one@home.com). |
| 10486 | 6 |
| 7 # Provided as-is; use at your own risk; no warranty; no promises; enjoy! | |
| 8 | |
| 9 """ndiff [-q] file1 file2 | |
| 10487 | 10 or |
| 11 ndiff (-r1 | -r2) < ndiff_output > file1_or_file2 | |
| 10486 | 12 |
| 13 Print a human-friendly file difference report to stdout. Both inter- | |
| 10487 | 14 and intra-line differences are noted. In the second form, recreate file1 |
| 15 (-r1) or file2 (-r2) on stdout, from an ndiff report on stdin. | |
| 10486 | 16 |
| 10487 | 17 In the first form, if -q ("quiet") is not specified, the first two lines |
| 18 of output are | |
| 10486 | 19 |
| 20 -: file1 | |
| 21 +: file2 | |
| 22 | |
| 23 Each remaining line begins with a two-letter code: | |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
| 10486 | 25 "- " line unique to file1 |
| 26 "+ " line unique to file2 | |
| 27 " " line common to both files | |
| 28 "? " line not present in either input file | |
| 29 | |
| 30 Lines beginning with "? " attempt to guide the eye to intraline | |
|
15367
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
31 differences, and were not present in either input file. These lines can be |
|
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
32 confusing if the source files contain tab characters. |
| 10486 | 33 |
| 34 The first file can be recovered by retaining only lines that begin with | |
| 10487 | 35 " " or "- ", and deleting those 2-character prefixes; use ndiff with -r1. |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 |
|
15367
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
37 The second file can be recovered similarly, but by retaining only " " and |
|
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
38 "+ " lines; use ndiff with -r2; or, on Unix, the second file can be |
| 10487 | 39 recovered by piping the output through |
| 40 | |
| 10486 | 41 sed -n '/^[+ ] /s/^..//p' |
| 42 | |
| 43 See module comments for details and programmatic interface. | |
| 44 """ | |
| 45 | |
|
15367
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
46 __version__ = 1, 5, 0 |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 # SequenceMatcher tries to compute a "human-friendly diff" between |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 # two sequences (chiefly picturing a file as a sequence of lines, |
| 10486 | 50 # and a line as a sequence of characters, here). Unlike e.g. UNIX(tm) |
| 51 # diff, the fundamental notion is the longest *contiguous* & junk-free | |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 # matching subsequence. That's what catches peoples' eyes. The |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 # Windows(tm) windiff has another interesting notion, pairing up elements |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 # that appear uniquely in each sequence. That, and the method here, |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 # appear to yield more intuitive difference reports than does diff. This |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 # method appears to be the least vulnerable to synching up on blocks |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 # of "junk lines", though (like blank lines in ordinary text files, |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 # or maybe "<P>" lines in HTML files). That may be because this is |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 # the only method of the 3 that has a *concept* of "junk" <wink>. |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 # |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 # Note that ndiff makes no claim to produce a *minimal* diff. To the |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 # contrary, minimal diffs are often counter-intuitive, because they |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 # synch up anywhere possible, sometimes accidental matches 100 pages |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 # apart. Restricting synch points to contiguous matches preserves some |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 # notion of locality, at the occasional cost of producing a longer diff. |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 # |
| 10486 | 67 # With respect to junk, an earlier version of ndiff simply refused to |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 # *start* a match with a junk element. The result was cases like this: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 # before: private Thread currentThread; |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 # after: private volatile Thread currentThread; |
| 10486 | 71 # If you consider whitespace to be junk, the longest contiguous match |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 # not starting with junk is "e Thread currentThread". So ndiff reported |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 # that "e volatil" was inserted between the 't' and the 'e' in "private". |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 # While an accurate view, to people that's absurd. The current version |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 # looks for matching blocks that are entirely junk-free, then extends the |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 # longest one of those as far as possible but only with matching junk. |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 # So now "currentThread" is matched, then extended to suck up the |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 # preceding blank; then "private" is matched, and extended to suck up the |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 # following blank; then "Thread" is matched; and finally ndiff reports |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 # that "volatile " was inserted before "Thread". The only quibble |
| 10486 | 81 # remaining is that perhaps it was really the case that " volatile" |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 # was inserted after "private". I can live with that <wink>. |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 # |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 # NOTE on junk: the module-level names |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 # IS_LINE_JUNK |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 # IS_CHARACTER_JUNK |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 # can be set to any functions you like. The first one should accept |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 # a single string argument, and return true iff the string is junk. |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 # The default is whether the regexp r"\s*#?\s*$" matches (i.e., a |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 # line without visible characters, except for at most one splat). |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 # The second should accept a string of length 1 etc. The default is |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 # whether the character is a blank or tab (note: bad idea to include |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 # newline in this!). |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 # |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 # After setting those, you can call fcompare(f1name, f2name) with the |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 # names of the files you want to compare. The difference report |
| 10486 | 97 # is sent to stdout. Or you can call main(args), passing what would |
| 98 # have been in sys.argv[1:] had the cmd-line form been used. | |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
15497
diff
changeset
|
100 from difflib import SequenceMatcher |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
15497
diff
changeset
|
101 |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 import string |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 TRACE = 0 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 # define what "junk" means |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 import re |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 def IS_LINE_JUNK(line, pat=re.compile(r"\s*#?\s*$").match): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 return pat(line) is not None |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 def IS_CHARACTER_JUNK(ch, ws=" \t"): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 return ch in ws |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 del re |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 # meant for dumping lines |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 def dump(tag, x, lo, hi): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 for i in xrange(lo, hi): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 print tag, x[i], |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 def plain_replace(a, alo, ahi, b, blo, bhi): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 assert alo < ahi and blo < bhi |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 # dump the shorter block first -- reduces the burden on short-term |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 # memory if the blocks are of very different sizes |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 if bhi - blo < ahi - alo: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 dump('+', b, blo, bhi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 dump('-', a, alo, ahi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 else: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 dump('-', a, alo, ahi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 dump('+', b, blo, bhi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 # When replacing one block of lines with another, this guy searches |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 # the blocks for *similar* lines; the best-matching pair (if any) is |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 # used as a synch point, and intraline difference marking is done on |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 # the similar pair. Lots of work, but often worth it. |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 def fancy_replace(a, alo, ahi, b, blo, bhi): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 if TRACE: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 print '*** fancy_replace', alo, ahi, blo, bhi |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 dump('>', a, alo, ahi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 dump('<', b, blo, bhi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 # don't synch up unless the lines have a similarity score of at |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 # least cutoff; best_ratio tracks the best score seen so far |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 best_ratio, cutoff = 0.74, 0.75 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 cruncher = SequenceMatcher(IS_CHARACTER_JUNK) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 eqi, eqj = None, None # 1st indices of equal lines (if any) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 # search for the pair that matches best without being identical |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 # (identical lines must be junk lines, & we don't want to synch up |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 # on junk -- unless we have to) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 for j in xrange(blo, bhi): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 bj = b[j] |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 cruncher.set_seq2(bj) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 for i in xrange(alo, ahi): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 ai = a[i] |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 if ai == bj: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 if eqi is None: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 eqi, eqj = i, j |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 continue |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 cruncher.set_seq1(ai) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 # computing similarity is expensive, so use the quick |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 # upper bounds first -- have seen this speed up messy |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 # compares by a factor of 3. |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 # note that ratio() is only expensive to compute the first |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 # time it's called on a sequence pair; the expensive part |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 # of the computation is cached by cruncher |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 if cruncher.real_quick_ratio() > best_ratio and \ |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 cruncher.quick_ratio() > best_ratio and \ |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 cruncher.ratio() > best_ratio: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 best_ratio, best_i, best_j = cruncher.ratio(), i, j |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 if best_ratio < cutoff: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 # no non-identical "pretty close" pair |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 if eqi is None: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 # no identical pair either -- treat it as a straight replace |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 plain_replace(a, alo, ahi, b, blo, bhi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 return |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 # no close pair, but an identical pair -- synch up on that |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 best_i, best_j, best_ratio = eqi, eqj, 1.0 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 else: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 # there's a close pair, so forget the identical pair (if any) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 eqi = None |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 # a[best_i] very similar to b[best_j]; eqi is None iff they're not |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 # identical |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 if TRACE: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 print '*** best_ratio', best_ratio, best_i, best_j |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 dump('>', a, best_i, best_i+1) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 dump('<', b, best_j, best_j+1) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 # pump out diffs from before the synch point |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 fancy_helper(a, alo, best_i, b, blo, best_j) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 # do intraline marking on the synch pair |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 aelt, belt = a[best_i], b[best_j] |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 if eqi is None: |
|
15497
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
197 # pump out a '-', '?', '+', '?' quad for the synched lines |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 atags = btags = "" |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 cruncher.set_seqs(aelt, belt) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 for tag, ai1, ai2, bj1, bj2 in cruncher.get_opcodes(): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 la, lb = ai2 - ai1, bj2 - bj1 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 if tag == 'replace': |
|
15497
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
203 atags += '^' * la |
|
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
204 btags += '^' * lb |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 elif tag == 'delete': |
|
15497
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
206 atags += '-' * la |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 elif tag == 'insert': |
|
15497
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
208 btags += '+' * lb |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 elif tag == 'equal': |
|
15497
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
210 atags += ' ' * la |
|
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
211 btags += ' ' * lb |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 else: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 raise ValueError, 'unknown tag ' + `tag` |
|
15497
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
214 printq(aelt, belt, atags, btags) |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 else: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 # the synch pair is identical |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 print ' ', aelt, |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 # pump out diffs from after the synch point |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 def fancy_helper(a, alo, ahi, b, blo, bhi): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 if alo < ahi: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 if blo < bhi: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 fancy_replace(a, alo, ahi, b, blo, bhi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 else: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 dump('-', a, alo, ahi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 elif blo < bhi: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 dump('+', b, blo, bhi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 |
|
15367
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
231 # Crap to deal with leading tabs in "?" output. Can hurt, but will |
|
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
232 # probably help most of the time. |
|
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
233 |
|
15497
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
234 def printq(aline, bline, atags, btags): |
|
15367
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
235 common = min(count_leading(aline, "\t"), |
|
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
236 count_leading(bline, "\t")) |
|
15497
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
237 common = min(common, count_leading(atags[:common], " ")) |
|
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
238 print "-", aline, |
|
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
239 if count_leading(atags, " ") < len(atags): |
|
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
240 print "?", "\t" * common + atags[common:] |
|
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
241 print "+", bline, |
|
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
242 if count_leading(btags, " ") < len(btags): |
|
f3bf5f082a3f
SF bug 124051: ndiff "?" lines can be confusing. Well, they still can, but
Tim Peters <tim.peters@gmail.com>
parents:
15367
diff
changeset
|
243 print "?", "\t" * common + btags[common:] |
|
15367
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
244 |
|
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
245 def count_leading(line, ch): |
|
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
246 i, n = 0, len(line) |
|
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
247 while i < n and line[i] == ch: |
|
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
248 i += 1 |
|
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
249 return i |
|
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
250 |
| 10487 | 251 def fail(msg): |
| 252 import sys | |
| 253 out = sys.stderr.write | |
| 254 out(msg + "\n\n") | |
| 255 out(__doc__) | |
| 256 return 0 | |
| 257 | |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 # open a file & return the file object; gripe and return 0 if it |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 # couldn't be opened |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 def fopen(fname): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 try: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 return open(fname, 'r') |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 except IOError, detail: |
| 10487 | 264 return fail("couldn't open " + fname + ": " + str(detail)) |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 # open two files & spray the diff to stdout; return false iff a problem |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 def fcompare(f1name, f2name): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 f1 = fopen(f1name) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 f2 = fopen(f2name) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 if not f1 or not f2: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 return 0 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 a = f1.readlines(); f1.close() |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 b = f2.readlines(); f2.close() |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 cruncher = SequenceMatcher(IS_LINE_JUNK, a, b) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 for tag, alo, ahi, blo, bhi in cruncher.get_opcodes(): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 if tag == 'replace': |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 fancy_replace(a, alo, ahi, b, blo, bhi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 elif tag == 'delete': |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 dump('-', a, alo, ahi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 elif tag == 'insert': |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 dump('+', b, blo, bhi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 elif tag == 'equal': |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 dump(' ', a, alo, ahi) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 else: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 raise ValueError, 'unknown tag ' + `tag` |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 return 1 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 |
| 10486 | 291 # crack args (sys.argv[1:] is normal) & compare; |
| 292 # return false iff a problem | |
| 293 | |
| 294 def main(args): | |
| 295 import getopt | |
| 296 try: | |
| 10487 | 297 opts, args = getopt.getopt(args, "qr:") |
| 10486 | 298 except getopt.error, detail: |
| 10487 | 299 return fail(str(detail)) |
| 10486 | 300 noisy = 1 |
| 10487 | 301 qseen = rseen = 0 |
| 10486 | 302 for opt, val in opts: |
| 303 if opt == "-q": | |
| 10487 | 304 qseen = 1 |
| 10486 | 305 noisy = 0 |
| 10487 | 306 elif opt == "-r": |
| 307 rseen = 1 | |
| 308 whichfile = val | |
| 309 if qseen and rseen: | |
| 310 return fail("can't specify both -q and -r") | |
| 311 if rseen: | |
| 312 if args: | |
| 313 return fail("no args allowed with -r option") | |
| 314 if whichfile in "12": | |
| 315 restore(whichfile) | |
| 316 return 1 | |
| 317 return fail("-r value must be 1 or 2") | |
| 10486 | 318 if len(args) != 2: |
| 10487 | 319 return fail("need 2 filename args") |
| 10486 | 320 f1name, f2name = args |
| 321 if noisy: | |
| 322 print '-:', f1name | |
| 323 print '+:', f2name | |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 return fcompare(f1name, f2name) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 |
| 10487 | 326 def restore(which): |
| 327 import sys | |
| 328 tag = {"1": "- ", "2": "+ "}[which] | |
| 329 prefixes = (" ", tag) | |
| 330 for line in sys.stdin.readlines(): | |
| 331 if line[:2] in prefixes: | |
| 332 print line[2:], | |
| 333 | |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 if __name__ == '__main__': |
| 10486 | 335 import sys |
| 336 args = sys.argv[1:] | |
| 10487 | 337 if "-profile" in args: |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 import profile, pstats |
| 10487 | 339 args.remove("-profile") |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 statf = "ndiff.pro" |
| 10486 | 341 profile.run("main(args)", statf) |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 stats = pstats.Stats(statf) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 stats.strip_dirs().sort_stats('time').print_stats() |
| 10487 | 344 else: |
| 345 main(args) |
