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

Python code coverage for Tools/scripts/diff.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a""" Command line interface to difflib.py providing diffs in four formats:
3n/a
4n/a* ndiff: lists every line and highlights interline changes.
5n/a* context: highlights clusters of changes in a before/after format.
6n/a* unified: highlights clusters of changes in an inline format.
7n/a* html: generates side by side comparison with change highlights.
8n/a
9n/a"""
10n/a
11n/aimport sys, os, difflib, argparse
12n/afrom datetime import datetime, timezone
13n/a
14n/adef file_mtime(path):
15n/a t = datetime.fromtimestamp(os.stat(path).st_mtime,
16n/a timezone.utc)
17n/a return t.astimezone().isoformat()
18n/a
19n/adef main():
20n/a
21n/a parser = argparse.ArgumentParser()
22n/a parser.add_argument('-c', action='store_true', default=False,
23n/a help='Produce a context format diff (default)')
24n/a parser.add_argument('-u', action='store_true', default=False,
25n/a help='Produce a unified format diff')
26n/a parser.add_argument('-m', action='store_true', default=False,
27n/a help='Produce HTML side by side diff '
28n/a '(can use -c and -l in conjunction)')
29n/a parser.add_argument('-n', action='store_true', default=False,
30n/a help='Produce a ndiff format diff')
31n/a parser.add_argument('-l', '--lines', type=int, default=3,
32n/a help='Set number of context lines (default 3)')
33n/a parser.add_argument('fromfile')
34n/a parser.add_argument('tofile')
35n/a options = parser.parse_args()
36n/a
37n/a n = options.lines
38n/a fromfile = options.fromfile
39n/a tofile = options.tofile
40n/a
41n/a fromdate = file_mtime(fromfile)
42n/a todate = file_mtime(tofile)
43n/a with open(fromfile) as ff:
44n/a fromlines = ff.readlines()
45n/a with open(tofile) as tf:
46n/a tolines = tf.readlines()
47n/a
48n/a if options.u:
49n/a diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
50n/a elif options.n:
51n/a diff = difflib.ndiff(fromlines, tolines)
52n/a elif options.m:
53n/a diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
54n/a else:
55n/a diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
56n/a
57n/a sys.stdout.writelines(diff)
58n/a
59n/aif __name__ == '__main__':
60n/a main()