-
Notifications
You must be signed in to change notification settings - Fork 0
/
changelog.py
executable file
·500 lines (477 loc) · 14.1 KB
/
changelog.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/env python3
#
# Classes to hold RPM .changes and debian.changelog information and to do
# conversions with them.
#
# Ensure that you have a POSIX or en_US.UTF-8 locale when using this.
#
# (c) Kurt Garloff <[email protected]>, 1/2018
# License: CC-BY-SA 3.0
#import os
import sys
import datetime
import pytz
from six import print_
plineno = 0
def wrap(txt, indent, maxln):
"Amazingly complex code to wrap text"
strg = ''
idx = 0
while len(txt[idx:]) > maxln-indent:
# Handle preformatted text
lf = txt[idx:idx+maxln-indent+1].rfind('\n')
if lf > 0 and txt[idx+lf+1] == ' ':
strg += txt[idx:idx+lf+1]
idx += lf+1
strg += ' '*indent
while txt[idx] == ' ':
idx+=1
continue
# Reformatting (wrapping) necessary
sep = txt[idx:idx+maxln-indent].rfind(' ')
sep2 = txt[idx:idx+maxln-indent].rfind('-')
if sep == -1 and sep2 == -1:
strg += txt[idx:idx+maxln-indent] + '\n' + ' '*indent
idx += maxln-indent
elif sep2 > sep and not txt[sep2+1].isdigit():
strg += txt[idx:idx+sep2+1] + '\n' + ' '*indent
idx += sep2+1
else:
strg += txt[idx:idx+sep] + '\n' + ' '*indent
idx += sep+1
strg += txt[idx:]
#print_(strg)
return strg
def mycapwd(txt):
"Custom version of capwords()"
#import string
ans = ''
for ix in range(0, len(txt)):
if ix == 0 or txt[ix-1] == '.' or txt[ix-1] == '-':
ans += txt[ix].upper()
else:
ans += txt[ix]
return ans
def guessnm(email):
(acct, dom) = email.split('@')
# Try firstname.lastname@domain
acct = mycapwd(acct)
nms = acct.split('.')
if len(nms) > 1:
return ' '.join(nms)
# Try [email protected]
dom = mycapwd(dom)
doms = dom.split('.')
return acct + ' ' + doms[0]
def tzsearchlist(email):
mylist = ['Europe/Amsterdam', 'Europe/Kiev', 'Europe/London', 'Europe/Moscow', 'America/New_York', 'America/Chicago', 'America/Denver', 'America/Los_Angeles', 'America/Sao_Paulo', 'Asia/Seoul', 'Asia/Tokyo', 'Asia/Shanghai', 'Australia/Sydney', 'Africa/Johannesburg']
mylist.extend(list(pytz.common_timezones_set))
# CST = China Std Time and Central Std Time
if email and email.split('.')[-1] == 'cn':
mylist.insert(0, 'Asia/Shanghai')
return mylist
def findtz(tznm, date, email = ''):
"Find timezone by abbreviation, use heuristics"
mylist = tzsearchlist(email)
for tz in mylist:
tzi = pytz.timezone(tz)
if tzi.tzname(date) == tznm:
return tzi
print_("WARNING: Could not parse TZ %s" % tznm, file=sys.stderr)
return pytz.utc
def findtzoff(offstr, date, email = ''):
"Find timezone by UTC offset, use heuristics"
mylist = tzsearchlist(email)
sgn = -1 if offstr[0] == '-' else 1
off = datetime.timedelta(0, sgn*60*(60*int(offstr[1:3])+int(offstr[3:5])))
# Need naive datetime
dt = datetime.datetime(date.year, date.month, date.day, date.hour, date.minute, date.second)
for tz in mylist:
tzi = pytz.timezone(tz)
if tzi.utcoffset(dt) == off:
return tzi
print_("WARNING: Could not parse TZ %s" % tznm, file=sys.stderr)
return pytz.utc
def increl(prevver):
"Incr. -release string by one"
pver = prevver.split('-')
try:
pver[-1] = str(int(pver[-1])+1)
except:
pass
return '-'.join(pver)
class ParseError(ValueError):
"Exceptions to throw when we fail to parse RPM/DEB changelog"
def __init__(self, errstr, ln = plineno):
ValueError.__init__(self, errstr + ' (line ~ %i)' % ln)
RPMSEP = '-------------------------------------------------------------------'
RPMHDR = '- '
RPMSUB = ' * '
RPMTMF = '%a %b %d %H:%M:%S %Z %Y'
DEBHDR = ' * '
DEBSUB = ' - '
DEBTMF = '%a, %d %b %Y %H:%M:%S %z'
class logitem:
"Class to hold one item"
def __init__(self, head=None, subitems=[]):
self.head = head
self.subitems = subitems
def genout(self, hdr, sub, lnln):
"return generic output string"
hln = len(hdr)
sln = len(sub)
strg = hdr + wrap(self.head, hln, lnln)
for it in self.subitems:
strg += '\n' + sub + wrap(it, sln, lnln)
return strg # + '\n'
def rpmout(self):
"RPM formatted output"
return self.genout(RPMHDR, RPMSUB, 68)
def debout(self):
"DEB formatted output"
return self.genout(DEBHDR, DEBSUB, 70)
def genparse(self, txt, hdst, subst, joinln = False, tolerant = False, subcnt = None):
"Parse one log item, consisting of head entry and (optionally) subitems"
txtln = txt.count('\n')
hdln = len(hdst)
subln = len(subst)
if not subcnt:
subcnt = ' '*subln
subcln = len(subcnt)
if not txt[0:hdln] == hdst:
raise ParseError('should start with "%s", got "%s"' % (hdst, txt[0:hdln]), plineno-txtln-2)
ishead = True
self.head = ''
self.subitems = []
sub = ''
lnno = 0
for ln in txt.splitlines():
#print_(ln)
lnno += 1
if ishead:
if ln[0:subln] == subst:
ishead = False
sub = ln[subln:]
elif ln[0:hdln] == ' '*hdln:
if joinln:
self.head += ln[hdln-1:]
else:
self.head += '\n' + ln
elif ln[0:hdln] == hdst:
self.head += ln[hdln:]
else:
raise ParseError('unexpected line start "%s"' % ln[0:subln], plineno-txtln-2+lnno)
else:
if ln[0:subcln] == subcnt:
if joinln:
sub += ln[subcln-1:]
else:
sub += '\n' + ln
elif ln[0:subln] == subst:
self.subitems.append(sub)
sub = ln[subln:]
else:
raise ParseError('unexpected subitem line start "%s"' % ln[0:subln], plineno-txtln-2+lnno)
if sub:
self.subitems.append(sub)
return self
def rpmparse(self, txt, joinln = False, tolerant = False):
return self.genparse(txt, '- ', ' * ', joinln, tolerant)
def debparse(self, txt, joinln = False, tolerant = False):
return self.genparse(txt, ' * ', ' - ', joinln, tolerant)
def debparse_misssub(self, txt, joinln = False, tolerant = False):
return self.genparse(txt, ' * ', ' ', joinln, tolerant, ' ')
def contains(self, slist):
for strg in slist:
if strg in self.head:
return True
for sit in self.subitems:
if strg in sit:
return True
return False
class logentry:
"Class to hold one changelog entry data"
import re
verrgx = re.compile(r'\-([0-9]*\.[^ :]*):')
ver1rgx = re.compile(r'\-([0-9]*\.[^ :]*)')
ver2rgx = re.compile(r'[uU]pdate to [ a-zA-Z-]*([0-9]*\.[^ :]*)')
ver3rgx = re.compile(r'[vV]ersion[: ]*([0-9]*\.[^ :]*)')
ver4rgx = re.compile(r'[rR]elease[: ]*([0-9]*\.[^ :]*)')
emerkwds = ('emergency',)
highkwds = ('CVE', 'exploit')
medkwds = ('security', 'vulnerability', 'leak', 'major', ' critical')
def __init__(self, date=None, email=None, authnm=None, pkgnm=None, vers=None, dist='stable', urg='', emaildb = None, items=[]):
self.date = date
self.email = email
self.authnm = authnm
self.pkgnm = pkgnm
self.ver0rgx = logentry.re.compile(r'%s[- ]([0-9]*\.[^ :]*)' % pkgnm)
self.vers = vers
self.dist = dist
self.urg = urg
self.emaildb = emaildb
self.items = items
def rpmout(self):
"Return string with RPM formatted changelog"
strg = RPMSEP + '\n'
idx = len(strg)
strg += self.date.strftime(RPMTMF)
if strg[idx+8] == '0':
strg = strg[0:idx+8]+' '+strg[idx+9:]
#strg[idx+8] = ' '
strg += " - %s\n\n" % self.email
for ent in self.items:
strg += ent.rpmout() + '\n'
return strg + '\n'
def debout(self, prevver = ''):
"Return string with DEB formatted changelog"
vers = self.vers
if not vers and prevver:
vers = increl(prevver)
strg = '%s (%s) %s; urgency=%s\n\n' % (self.pkgnm, vers,
self.dist, self.urg)
for ent in self.items:
strg += ent.debout() + '\n'
strg += '\n -- %s <%s> ' % (self.authnm, self.email)
idx = len(strg)
strg += self.date.strftime(DEBTMF)
if strg[idx+5] == '0':
strg = strg[0:idx+5]+' '+strg[idx+6:]
#strg[idx+6] = ' '
strg += '\n\n'
return strg
def guess_ver_nm(self):
"Try to determine pkg version and name from changelog text"
for ent in self.items:
ln = ent.head
m = logentry.verrgx.search(ln)
if not m:
m = self.ver0rgx.search(ln)
if not m:
m = logentry.ver1rgx.search(ln)
if not m:
m = logentry.ver2rgx.search(ln)
if not m:
m = logentry.ver3rgx.search(ln)
if m:
#self.vers = m.group(0)[1:].rstrip('.')
self.vers = m.group(1).rstrip('.')
if not self.pkgnm:
idx = ln.find(self.vers)
pidx = ln[0:idx].rfind(' ')
self.pkgnm = ln[pidx+1:idx-1]
self.ver0rgx = logentry.re.compile(r'%s[- ]([0-9]*\.[^ :]*)' % self.pkgnm)
if self.vers.find('-') == -1:
self.vers += '-1'
return
def guess_urg(self):
"Guess urgency"
for ent in self.items:
if ent.contains(logentry.emerkwds):
self.urg = 'emergency'
return
if ent.contains(logentry.highkwds):
self.urg = 'high'
return
if ent.contains(logentry.medkwds):
self.urg = 'medium'
if not self.urg:
self.urg = 'low'
def rpmparse(self, txt, joinln = False, tolerant = False):
"Parse one RPM changelog entry section"
txtln = txt.count('\n')
self.email= ''
self.items = []
buf = ''
procln = 0
for ln in txt.splitlines():
procln += 1
# Handle separator lines
if ln == RPMSEP:
if self.email:
break
else:
continue
# Handle header
if not self.email:
try:
(datestr, email) = ln.split(' - ')
except ValueError as exc:
raise ParseError('Could not split date - email in "%s"' % ln, plineno-txtln+procln)
self.email = email
tznm = datestr.split(' ')[-2]
date = datetime.datetime.strptime(datestr, RPMTMF)
self.date = findtz(tznm, date, email).localize(date)
if not self.date:
raise ParseError("No such timezone %s" % tznm, plneno-txtln+procln)
if not self.authnm:
if self.emaildb:
try:
# Need dict-like iface (__getitem__)
self.authnm = self.emaildb[email]
except KeyError:
self.authnm = guessnm(email)
print_("WARN: No name found for email %s, guess %s" % (email, self.authnm), file=sys.stderr)
else:
self.authnm = guessnm(email)
continue
# Handle empty line
if not ln:
if buf:
#print_("EMPTY: " + buf)
le = logitem().rpmparse(buf, joinln, tolerant)
self.items.append(le)
buf = ''
continue
else:
continue
# Handle new log item
if ln[0:2] == RPMHDR and buf:
#print_("NEW: " + buf)
le = logitem().rpmparse(buf, joinln, tolerant)
self.items.append(le)
buf = ''
buf += ln + '\n'
if buf:
#print_("END: "+ buf)
le = logitem().rpmparse(buf, joinln, tolerant)
self.items.append(le)
if not self.vers:
self.guess_ver_nm()
if not self.urg:
self.guess_urg()
return self
#return procln
def debparse(self, txt, joinln = False, tolerant = False):
"Parse one DEB changelog entry section"
txtln = txt.count('\n')
self.urg = ''
self.items = []
buf = ''
procln = 0
for ln in txt.splitlines():
procln += 1
# Handle separator lines
if ln and ln[0] != ' ':
if self.email:
break
# Handle header
if not self.pkgnm:
(self.pkgnm, vers, dist, urg) = ln.split(' ')
self.vers = vers[1:-1]
self.dist = dist[0:-1]
self.urg = urg[8:]
continue
# Handle empty line
if not ln:
if buf:
#print_("EMPTY: " + buf)
le = logitem().debparse(buf, joinln, tolerant)
self.items.append(le)
buf = ''
continue
else:
continue
# Handle new log item
if ln[0:4] == DEBHDR and buf:
#print_("NEW: " + buf)
le = logitem().debparse(buf, joinln, tolerant)
self.items.append(le)
buf = ''
# Handle footer
if ln[0:4] == " -- ":
idx = ln.find('<')
if idx < 0:
raise ParseError("No email address in footer %s" % ln, plineno-txtln+procln)
idx2 = ln.find('>')
self.authnm = ln[4:idx-1]
self.email = ln[idx+1:idx2]
self.date = datetime.datetime.strptime(ln[idx2+3:], DEBTMF)
tzi = findtzoff(ln[-5:], self.date, self.email)
if tzi.zone != 'UTC':
self.date = self.date.astimezone(tzi)
break
# Normal line, process ...
buf += ln + '\n'
if buf:
#print_("END: "+ buf)
le = logitem().debparse(buf, joinln, tolerant)
self.items.append(le)
return self
#return procln
class changelog:
"Container for full changelog"
def __init__(self, pkgnm=None, authover=None, distover='stable', urgover='', initver = '?-0', emaildb = None, entries=[]):
self.pkgnm = pkgnm
self.authover = authover
self.distover = distover
self.urgover = urgover
self.initver = initver
self.emaildb = emaildb
self.entries = entries
def rpmout(self):
"output RPM changelog as string"
strg = ''
for ent in self.entries:
strg += ent.rpmout()
return strg
def fixupdebver(self):
"fill in missing versions by guessing ..."
lastver = self.initver
lastpkg = None
for idx in range(len(self.entries)-1, -1, -1):
#print_(sys.stderr, lastver)
if not self.entries[idx].vers:
self.entries[idx].vers = increl(lastver)
if lastpkg and not self.entries[idx].pkgnm:
self.entries[idx].pkgnm = lastpkg
elif not lastpkg:
lastpkg = self.entries[idx].pkgnm
lastver = self.entries[idx].vers
def debout(self):
"output DEB changelog as string"
self.fixupdebver()
strg = ''
for ent in self.entries:
strg += ent.debout()
return strg
def rpmparse(self, fd, joinln = False, tolerant = False, maxent = 0):
"Parse full RPM changelog"
global plineno
buf = ''
ent = 0
for ln in fd:
plineno += 1
if ln == RPMSEP+'\n':
if buf:
#print_(buf)
self.entries.append(logentry(authnm = self.authover, pkgnm = self.pkgnm, dist = self.distover, urg = self.urgover, emaildb = self.emaildb).rpmparse(buf, joinln, tolerant))
buf = ''
ent += 1
if maxent and ent > maxent:
break
buf += ln
if buf:
#print_(buf)
self.entries.append(logentry(authnm = self.authover, pkgnm = self.pkgnm, dist = self.distover, urg = self.urgover, emaildb = self.emaildb).rpmparse(buf, joinln, tolerant))
return self
def debparse(self, fd, joinln = False, tolerant = False, maxent = 0):
"Parse full DEB changelog"
global plineno
buf = ''
ent = 0
for ln in fd:
plineno += 1
if ln != '\n' and ln[0] != ' ':
if buf:
#print_(buf)
self.entries.append(logentry(authnm = self.authover, pkgnm = self.pkgnm, dist = self.distover, urg = self.urgover).debparse(buf, joinln, tolerant))
buf = ''
ent += 1
if maxent and ent > maxent:
break
buf += ln
if buf:
#print_(buf)
self.entries.append(logentry(authnm = self.authover, pkgnm = self.pkgnm, dist = self.distover, urg = self.urgover).debparse(buf, joinln, tolerant))
return self