Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 53 lines (39 sloc) 1.32 KB
#!/usr/bin/env python2
from collections import defaultdict
def make_dump():
return defaultdict(lambda: set())
def load(dumpfile):
with open(dumpfile) as dump:
text =
lines = text.split('\n')
lines = [line.split() for line in lines]
dump_dict = make_dump()
for line in lines:
if line == []: # this might happen at the end of the file.
ident = line.pop()
line.pop() # get rid of the '--' delimiter
dump_dict[ident] = set(line)
return dump_dict
def dump(dump_dict, filename):
f = open(filename, 'w')
for key in dump_dict.keys():
f.write(' '.join(list(dump_dict[key]) + ['--', key, '\n']))
def merge(ancestor, left, right):
result = make_dump()
keys = set()
for dump in (ancestor, left, right):
for key in dump.keys():
for key in keys:
added = left[key].union(right[key]).difference(ancestor[key])
in_both = left[key].intersection(right[key])
result[key] = added.union(in_both)
return result
if __name__ == '__main__':
import sys
if len(sys.argv) != 5:
sys.stderr.write("usage: '%s <ancestor> <left> <right> <result>\n" % sys.argv[0])
dump(merge(*list(map(load, sys.argv[1:4]))), sys.argv[4])