ยปCore Development>Code coverage>Tools/scripts/ndiff.py

Python code coverage for Tools/scripts/ndiff.py

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