-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchangelog-merge
executable file
·81 lines (72 loc) · 2.29 KB
/
changelog-merge
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
#!/usr/bin/python
from __future__ import print_function
import sys
import re
from dateutil.parser import parse
class Entry(object):
def __init__(self, filenum, fileindex):
self.lines = []
self.filenum = filenum
self.fileindex = fileindex
self.tstamp = None
def hash(self):
hash(self.join())
def body(self):
return "".join(self.lines[1:])
def join(self):
return "".join(self.lines)
def scanlines(f1, f2):
entries1 = []
entries2 = []
def scanlines(tgt, f, fileidx):
curr = None
curridx = 0
for line in f.readlines():
if re.match(r'^-+$', line.strip()):
if curr is not None:
tgt.append(curr)
curr = None
curr = Entry(0, curridx)
curridx += 1
if curr is not None:
curr.lines.append(line)
if curr is not None and curr.tstamp is None:
m = re.match(r'^(.*)\s+-\s+.*', line)
if m:
curr.tstamp = parse(m.group(1))
if curr is not None:
if curr.tstamp is None:
print("Entry {}:{} has no timestamp".format(curr.filenum, curr.fileindex))
tgt.append(curr)
curr = None
scanlines(entries1, f1, 1)
scanlines(entries2, f2, 2)
return entries1, entries2
def merge(entries1, entries2):
for e in entries1:
h = e.hash()
entries2 = [f for f in entries2 if e.tstamp != f.tstamp or f.hash() != h]
entries = []
for f in entries1:
keep = True
fh = f.body()
for g in entries2:
gh = g.body()
if fh == gh:
if g.tstamp > f.tstamp:
keep = False
if keep:
entries.append(f)
entries.extend(entries2)
entries.sort(key=lambda e: e.tstamp)
entries.reverse()
for e in entries:
print(e.join(), end="")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: {} <changelog1> <changelog2>".format(sys.argv[0]), file=sys.stderr)
sys.exit(1)
with open(sys.argv[1], "rt") as log1:
with open(sys.argv[2], "rt") as log2:
entries1, entries2 = scanlines(log1, log2)
merge(entries1, entries2)