-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncdiff.py
88 lines (79 loc) · 3.65 KB
/
syncdiff.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import difflib
import argparse
def main():
usage = "usage: %prog fromfile tofile"
parser = argparse.ArgumentParser(usage)
parser.add_argument('fromfile', type=str, nargs='+', default=None)
parser.add_argument('tofile', type=str, nargs='+', default=None)
args = parser.parse_args()
files = [args.fromfile[0], args.tofile[0]]
fromlines = open(files[0], 'U').readlines()
tolines = open(files[1], 'U').readlines()
diff = difflib.unified_diff(fromlines, tolines, files[0], files[1], n=5)
diff = [l for l in diff if ('Version:' == l[1:9] or 'Package:' == l[1:9])]
added_packages = {}
removed_packages = {}
versioned_packages = {}
current_package = None
current_package_mod = None
current_version = None
current_version_mod = None
for line in diff:
if 'Package:' == line[1:9]:
current_package = line[line.index(':') + 2:-1]
current_package_mod = line[0]
elif 'Version:' == line[1:9]:
current_version = version(line[line.index(':') + 2:-1])
current_version_mod = line[0]
if current_package_mod is ' ':
if current_package not in versioned_packages:
versioned_packages[current_package] = [None, None]
if current_version_mod == '-':
versioned_packages[current_package][0] = current_version
elif current_version_mod == '+':
versioned_packages[current_package][1] = current_version
else:
pass # Diff static, Version not changed, ignore
elif current_package_mod is '+':
if current_version_mod is not '+':
raise Exception(current_package + " add package odd ver")
if current_package in removed_packages:
temp = [current_version, removed_packages[current_package]]
del removed_packages[current_package]
versioned_packages[current_package] = temp
versioned_packages[current_package].sort()
else:
added_packages[current_package] = current_version
elif current_package_mod is '-':
if current_version_mod is not '-':
raise Exception(current_package + " rem package odd ver")
if current_package in added_packages:
temp = [current_version, added_packages[current_package]]
del added_packages[current_package]
versioned_packages[current_package] = temp
versioned_packages[current_package].sort()
else:
removed_packages[current_package] = current_version
else:
raise Exception("Erronious character: " + current_package_mod)
else:
raise Exception("Erronious line found: " + line)
print "\nPackages Added: "
for line in sorted(added_packages):
print line, ':', added_packages[line]
print "\nPackages Removed: "
for line in sorted(removed_packages):
print line, ':', removed_packages[line]
print "\nPackages Updated: "
for line in sorted(versioned_packages):
print_package = versioned_packages[line]
if len(print_package) == 2 and print_package[0] != print_package[1]:
print line, ':', print_package[0], '->', print_package[1]
def version(full_string):
sectioned = full_string.split('-', 2)
version_number = sectioned[0]
if sectioned[1].isdigit():
version_number = version_number + '-' + sectioned[1]
return version_number
if __name__ == '__main__':
main()