-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdiff.py
More file actions
executable file
·50 lines (39 loc) · 1.17 KB
/
Copy pathdiff.py
File metadata and controls
executable file
·50 lines (39 loc) · 1.17 KB
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
47
48
49
50
#!/usr/bin/env python3
"""Simple diff based on LCS solution"""
import sys
from functools import partial
from lcs import lcslen
# Print without newline because input files already have it
_print = partial(print, end='')
def print_diff(c, x, y, i, j):
"""Print the diff using LCS length matrix by backtracking it"""
if i < 0 and j < 0:
return ""
elif i < 0:
print_diff(c, x, y, i, j-1)
_print("+ " + y[j])
elif j < 0:
print_diff(c, x, y, i-1, j)
_print("- " + x[i])
elif x[i] == y[j]:
print_diff(c, x, y, i-1, j-1)
_print(" " + x[i])
elif c[i][j-1] >= c[i-1][j]:
print_diff(c, x, y, i, j-1)
_print("+ " + y[j])
elif c[i][j-1] < c[i-1][j]:
print_diff(c, x, y, i-1, j)
_print("- " + x[i])
def diff(x, y):
c = lcslen(x, y)
return print_diff(c, x, y, len(x)-1, len(y)-1)
def usage():
print("Usage: {} <file1> <file2>".format(sys.argv[0]))
def main():
if len(sys.argv) != 3:
usage()
sys.exit(1)
with open(sys.argv[1], 'r') as f1, open(sys.argv[2], 'r') as f2:
diff(f1.readlines(), f2.readlines())
if __name__ == '__main__':
main()