-
Notifications
You must be signed in to change notification settings - Fork 9
/
diff.py
executable file
·46 lines (36 loc) · 1.49 KB
/
diff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# "THE JUICE-WARE LICENSE" (Revision 42)
#
# <[email protected]> wrote this file. As long as you
# keep this notice, you can do whatever you want with this stuff.
# If we meet someday, and you think this stuff is worth it, you
# can buy me a glass of juice in return. Yegor Derevenets
import argparse, difflib, os, sys, time
def main():
parser = argparse.ArgumentParser(description='Compares two text files line by line.')
parser.add_argument('-u', action='store_true', help='Produce unified diff.')
parser.add_argument('--lines', type=int, default=3, help='Show so many lines of context.')
parser.add_argument('file', nargs=2, help='File.')
args = parser.parse_args()
fromfile, tofile = args.file # as specified in the usage string
fromdate = time.ctime(os.stat(fromfile).st_mtime)
todate = time.ctime(os.stat(tofile).st_mtime)
fromlines = open(fromfile, 'U').readlines()
tolines = open(tofile, 'U').readlines()
if args.u:
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,
fromdate, todate, n=args.lines)
else:
diff = difflib.context_diff(fromlines, tolines, fromfile, tofile,
fromdate, todate, n=args.lines)
differ = False
for line in diff:
sys.stdout.write(line)
differ = True
if differ:
sys.exit(1)
else:
sys.exit(0)
if __name__ == '__main__':
main()