Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/ndiff.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | ab588ce9e71f |
| children | 6b18a095c2e7 |
| 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 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
3 # Module ndiff version 1.7.0 |
|
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
|
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 | |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
9 # ndiff.py is now simply a front-end to the difflib.ndiff() function. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
10 # Originally, it contained the difflib.SequenceMatcher class as well. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
11 # This completes the raiding of reusable code from this formerly |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
12 # self-contained script. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
13 |
| 10486 | 14 """ndiff [-q] file1 file2 |
| 10487 | 15 or |
| 16 ndiff (-r1 | -r2) < ndiff_output > file1_or_file2 | |
| 10486 | 17 |
| 18 Print a human-friendly file difference report to stdout. Both inter- | |
| 10487 | 19 and intra-line differences are noted. In the second form, recreate file1 |
| 20 (-r1) or file2 (-r2) on stdout, from an ndiff report on stdin. | |
| 10486 | 21 |
| 10487 | 22 In the first form, if -q ("quiet") is not specified, the first two lines |
| 23 of output are | |
| 10486 | 24 |
| 25 -: file1 | |
| 26 +: file2 | |
| 27 | |
| 28 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
|
29 |
| 10486 | 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 | |
| 35 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
|
36 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
|
37 confusing if the source files contain tab characters. |
| 10486 | 38 |
| 39 The first file can be recovered by retaining only lines that begin with | |
| 10487 | 40 " " 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
|
41 |
|
15367
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
42 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
|
43 "+ " lines; use ndiff with -r2; or, on Unix, the second file can be |
| 10487 | 44 recovered by piping the output through |
| 45 | |
| 10486 | 46 sed -n '/^[+ ] /s/^..//p' |
| 47 """ | |
| 48 | |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
49 __version__ = 1, 7, 0 |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
51 import difflib, sys |
|
15367
8275d8643e79
Hack ndiff to display lines w/ leading tabs more intuitively. This synchs
Tim Peters <tim.peters@gmail.com>
parents:
10487
diff
changeset
|
52 |
| 10487 | 53 def fail(msg): |
| 54 out = sys.stderr.write | |
| 55 out(msg + "\n\n") | |
| 56 out(__doc__) | |
| 57 return 0 | |
| 58 | |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 # 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
|
60 # couldn't be opened |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 def fopen(fname): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 try: |
|
33666
ab588ce9e71f
Read the text files to be compared in universal-newline mode.
Tim Peters <tim.peters@gmail.com>
parents:
29105
diff
changeset
|
63 return open(fname, 'U') |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 except IOError, detail: |
| 10487 | 65 return fail("couldn't open " + fname + ": " + str(detail)) |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 # 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
|
68 def fcompare(f1name, f2name): |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 f1 = fopen(f1name) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 f2 = fopen(f2name) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 if not f1 or not f2: |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 return 0 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 a = f1.readlines(); f1.close() |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 b = f2.readlines(); f2.close() |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
76 for line in difflib.ndiff(a, b): |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
77 print line, |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 return 1 |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 |
| 10486 | 81 # crack args (sys.argv[1:] is normal) & compare; |
| 82 # return false iff a problem | |
| 83 | |
| 84 def main(args): | |
| 85 import getopt | |
| 86 try: | |
| 10487 | 87 opts, args = getopt.getopt(args, "qr:") |
| 10486 | 88 except getopt.error, detail: |
| 10487 | 89 return fail(str(detail)) |
| 10486 | 90 noisy = 1 |
| 10487 | 91 qseen = rseen = 0 |
| 10486 | 92 for opt, val in opts: |
| 93 if opt == "-q": | |
| 10487 | 94 qseen = 1 |
| 10486 | 95 noisy = 0 |
| 10487 | 96 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") | |
|
29105
19c414186b9e
Prevent script from allowing '-r12'
Andrew M. Kuchling <amk@amk.ca>
parents:
20045
diff
changeset
|
104 if whichfile in ("1", "2"): |
| 10487 | 105 restore(whichfile) |
| 106 return 1 | |
| 107 return fail("-r value must be 1 or 2") | |
| 10486 | 108 if len(args) != 2: |
| 10487 | 109 return fail("need 2 filename args") |
| 10486 | 110 f1name, f2name = args |
| 111 if noisy: | |
| 112 print '-:', f1name | |
| 113 print '+:', f2name | |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 return fcompare(f1name, f2name) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
116 # read ndiff output from stdin, and print file1 (which=='1') or |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
117 # file2 (which=='2') to stdout |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
118 |
| 10487 | 119 def restore(which): |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16551
diff
changeset
|
120 restored = difflib.restore(sys.stdin.readlines(), which) |
|
20045
7b8c11739714
Generalize file.writelines() to allow iterable objects.
Tim Peters <tim.peters@gmail.com>
parents:
20039
diff
changeset
|
121 sys.stdout.writelines(restored) |
| 10487 | 122 |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 if __name__ == '__main__': |
| 10486 | 124 args = sys.argv[1:] |
| 10487 | 125 if "-profile" in args: |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 import profile, pstats |
| 10487 | 127 args.remove("-profile") |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 statf = "ndiff.pro" |
| 10486 | 129 profile.run("main(args)", statf) |
|
8228
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 stats = pstats.Stats(statf) |
|
9d642a735a13
Adding Tim Peters' ndiff utility.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 stats.strip_dirs().sort_stats('time').print_stats() |
| 10487 | 132 else: |
| 133 main(args) |
